Moq - mcbride-clint/DeveloperCurriculum GitHub Wiki
Moq is a 3rd party framework designed to make testing easier. It has the ability to create short-lived instances of an interface to reduce the amount of manual class creations. When creating these short-lived instances, you can specify only the methods that you need to implement for your testing purposes and have full control over what kind of output these methods will have. This can greatly help you to isolate dependency behaviors from the class that you actually trying to test.
ILoveThisLibrary lovable = Mock.Of<ILoveThisLibrary>(l =>
l.DownloadExists("2.0.0.0") == true);
// Exercise the instance returned by Mock.Of by calling methods on it...
bool download = lovable.DownloadExists("2.0.0.0");
// Simply assert the returned state:
Assert.True(download);
// If you really want to go beyond state testing and want to
// verify the mock interaction instead...
Mock.Get(lovable).Verify(library => library.DownloadExists("2.0.0.0"));
The .Verify
method gives you the ability to check that methods are called as planned and can even check that the correct number of occurrences was called.
See Also
- Github - Moq - https://github.com/moq/moq4
- Pluralsight Course - Mocking with Moq - https://app.pluralsight.com/library/courses/mocking-moq-xunit/table-of-contents