Useful tips and tricks when writing unit tests with Moq (Part II)
Some time ago I wrote an article about Moq with some tricks I used while unit testing my code.
In this article you will find some more tips that I found very useful while using the framework.
Reset the Verify counter before every unit test runs
By using the Verify method you can check if a method of your mocked object was called. However, if you have multiple unit tests that verify the call of a method, then the number of calls is added with each call to the method. Use the Clear method of the mocked object inside the Setup method:
Assert for specific values of parameters of mocked methods
When using the Verify logic, I very often want to check if a mocked method was called with specific values. Here is how:
Here is also a StackOverflow question about this kind of check.
Mock classes which have a constructor with parameters
Classes might inject other objects through their constructor, so when you mock these classes, you will have to provide mocked implementation for these objects also. For that, simply add the mocked properties in the constructor when creating the mocked object. For more information, check this StackOverflow question.
Test asynchronous methods with Moq
Simply use the async Task keywords in the declaration of the unit test. You can then use the await keyword inside the body of the method:
Return a Task with value when you setup an async method
When you want to setup an async method of a mocked object, you might want that this method returns a specific result to the caller. The method has to return a Task with the value you define. For that you have two valid options:
- Use the Task.FromResult method (StackOverflow article about it):
- Use the ReturnAsync method, which is my preferred way of returning a mocked result.
Assert for thrown exceptions
When you want to verify that a method under test throws a specific type of exception then you can use the following Assert-check:
For async code use the following snippet and do not forget to define your test-method as async: