![]() You can then use the callback method to set local variables in your Unit Test method and make assertions in a "clean" manner. You can see here that the Callback has overrides that match the arguments supplied to the mocked method. Say you want to verify that Status = "Success", the following looks like it would work, but it doesn't: mockLogger.Setup(p => p.Save(It.Is(q => q.Status = "Success"))).Verifiable() Ī better way to directly make assertions on the log value by using a Callback to pluck out the arguments into local variables in your unit test: LogEntry spiedLogEntry = null Īssert.AreEqual("Success", spiedLogEntry.Status) The Save method is taking our logEntry as an argument, so in theory we should be able to evaluate it! With Moq, the common attempt is to use It.Is and specify arguments that match. Var result = // do something that gets logged You simply mark the Mock setup with Verifiable and failure to execute will cause an exception. With these two tools, we can verify that methods were called and pluck out the variables that were used when making the call to make Assertions on them. Using the Moq framework, we can achieve spying with Verifiable and Callback. moq Mocking properties Auto stubbing properties Sometimes you want to mock a class or an interface and have its properties behave as if they were simple getters and setters. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |