Basic introduction to writing unit tests with Moq - Part 2

This post is the second part of a series on Mocking with Moq. If you haven't already taken a look at the first post on the basics of Moq, please check it out.

Unit Test

In this post, I hope to cover some other basic aspects of mocking such as Verifying, Exceptions and strict Mock behaviour. There are a lot of awesome aspects of mocking out there, and I still try and experiment with them as much as possible. In the first post, I talked about the advantages of using mocking to simulate different objects and exceptions that we need to work with. For these examples you will need to have nUnit and Moq installed. Let take a look at some of these features.

Exception Handling

Let's say for example you wanted to simulate an exception that your code would throw and you weren't sure how your code would react. This exception might also be a difficult exception to simulate under normal circumstances, but with mocking we can throw this easily and test the results.

Let try an example:

Exception Moq

In the above code, we are setting up our mock object and telling it exactly what it is going to return. So in this instance we are telling it to throw an ArgumentOutOfRangeException, this could be any exception that you might want to test, as well as any custom exceptions you may have. It's as simple as that to start testing for exceptions.

Now we also want to check with nUnit that this exception was actually thrown.

Exception Moq

Verifying

Sometimes you might also want to check that a certain method was called, or even how many times that method was called. An easy way to do this is by using the Verify() method.

Verify Moq

Once you have setup your mock, verifying is an easy task:

Verify

The above code checks that the method GetBasket() was called once. There is also more options to choose from when deciding how many times the method was called, such as AtLeast, AtMost, Exactly, Between, etc.

Moq Times

Strict Mock Behaviour

When we are initially setting up initializing all our variables and setting up our mock objects, we can also set the mock behaviour of that object. For example:

Moq Behavior

Now, if we haven't set up any of the interfaces, Moq will throw a nasty error. If I run my test again with the new strict mock behaviour I get the following error:

Moq.MockException : IBasketDal.GetCustomerName(1) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.

This is because there is another method that I haven't setup correctly. Once the missing method has been setup this will go green!

And finally

Moq Behavior

I hope this post has helped a little with understanding more about mocking and using Moq. Hopefully now you will become a master of mocking like Nelson above. Again, I've included a project with some of the examples that we run through in this post - download here. If you are using MS Test, you might want to update the project to accordingly.