Isolating Unit Tests
#
IntroductionUnit tests are used to test the functionality of a class in isolation. So if a class depends on another we will need to mock any interactions with that class. We are using dependency injection so any class which needs to interact with another will be passed an interface, which the depended on class subscribes to, in the constructor. In the tests, we can then just pass in a mock implementation of this interface into the constructor instead.
Read some advice for mocking and writing unit tests in the .NET documentation.
#
Setting up and using mocksYou can use mocks to return a specific value for a given input. For example a test for the following code:
might look like..
Or you can assert that they were called with the correct inputs. For example a test for the following code..
might look like..
These examples are just a couple of common use cases for using Moq. Check out the moq documentation for more examples and use cases!