Var YourClassName= new YourClassName(mockRepo. MockRepo.Setup(repo => repo.CreateUser()) MockRepo.Setup(repo => repo.GetUserNotFound()) Public async Task UserAccountNo_YourClassName_WithUserNotFound() If this is your codes, it will be normally mock as below: Normally, in asp.net core these two method will be inside a service class and then we will inject that service class and call it. If you want to test the GetUserAccountNo, and it will use CreateUser and GetUser method. That means each method will have its own mock data. As far as I know, the unit test should be designed as each method will have its own test. NET Standard 2.1? - JTuto on Multi-targetting. Amer Neely on Installing and using SQLite extensions on macOs (and maybe Windows & Linux too).alex on Pretending that SQLite has Stored Procedures and Functions.MockApplication.SetUpIndexerForKeys("a", "b", "c") Public void TestWhichReliesOnMockedIndexer() MoqStateDictionaryExtension.RemoveDictionary(mockApplication) Private HttpApplicationStateBase exampleUsage If (!FakeStateDictionaries.ContainsKey(mock)) įakeStateDictionaries = new Dictionary>() (name, val) => static void EnsureDictionary(Mock mock) (name, val) => static void SetUpIndexerForKeys(this Mock params string (var key in keys) Public static void SetUpIndexerForKeys(this Mock params string (var key in keys) Private static Dictionary> FakeStateDictionaries= new Dictionary>() Like this: public static class MoqStateDictionaryExtension You can at least abbreviate by shipping out the setup to an extension method so you can do this: mockSessionStateBase.SetUpIndexerForKeys("a", "b", "c") If you're mocking out SessionState or ApplicationState you may want to mock multiple keys even for a single test fixture, but don't want dozens of lines of setup code for each key you use. You can't do this to setup all in one: httpContext.SetupSet(x => x.Session The missing functionality is that you have to setup for each specific key you are going to access. HttpContext.SetupGet(x => x.Session).Returns(value) Verify () asserts that the method call happened as expected with the specified parameters. SetUpProperty() doesn't work but if you only need to moq write/read to one or two keys or you can use this code from a stackoverflow post by seanlinmt for each key: httpContext.SetupSet(x => x.Session = It.IsAny()) When you need to verify that the code under test called a method with the expected parameters, you can mock the method with Moq and use Verify () + It.Is () to check the parameters passed in.Mocking an indexer property with Moq is nearly but not quite straightforward.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |