Blogging the Art of Software

I have been using Moq for a few weeks now for a current project at work. I have looked at other mock frameworks for unit testing and have pleasantly been enjoying Moq. Let me first describe what Moq is.

What is Moq?Moq is promoted by its creators as easier to learn and use than other Mock Object Frameworks such as Rhino Mocks and TypeMock Isolator. Moq is the only mocking library for .NET developed to take advantage of Linq expression trees and lambda expressions. Thus making it the most productive, type-safe and refactoring-friendly mocking library available.

Moq supports mocking interfaces as well as classes. There are some requirements and limitations on classes. The class can’t be sealed. Also, the method being mocked must be marked as virtual. You cannot mock static methods.

The emphasis on lambdas enables Moq to provide a very clean syntax for representing expectations, parameter constraints, and return values. The name “Moq” comes from a combination of Mock and Linq, even though it truly uses lambdra expressions instead of Linq as you will see below in examples.

Moq uses Castle DynamicProxy internally as the interception mechanism to enable mocking. It’s merged into Moq binaries, so you don’t need to do anything other than referencing Moq.dll.

1. Strong-typed: no strings for expectations, no object-typed return values or constraints
2. Unsurpassed Visual Studio intellisense integration: everything supports full Visual Studio intellisense, from setting expectations, to specifying method call arguments, return values, etc.
3. No Record/Replay idioms to learn. Just construct your mock, set it up, use it and optionally verify calls to it. You may not verify mocks when they act as stubs only, or when you are doing more classic state-based testing by checking returned values from the object under test.
4. Granular control over mock behavior with a simple MockBehavior enumeration. No need to learn what’s the theoretical difference between a mock, a stub, a fake, a dynamic mock, etc.
5. Mock boths interfaces and classes
6. Override expectations: can set default expectations in a fixture setup, and override as need on tests
7. Pass constructor arguments for mocked classes
8. Intercept and raise events on mocks
9. Intuitive support for out/ref arguments

Most of the above features I will demonstrate with example code below.

Now to some code. Assume you have a Product that you want to test against. That Product has an Id and Name and is stored somewhere in a repository. To keep things simple, we will be using Moq and testing against IProduct and IProductRepository.

Notice in WriteOrder it has a type IFileWriter as an input parameter. Let’s define that below.

public interface IFileWriter
{
void WriteLine(string line);
}

Now, let’s start using Moq to test and verify that WriteLine was called in IFileWriter through calling WriteOrder in Order. Confused? Hopefully the code below will make things start clicking. First let’s initialize Order and create a mock for IFileWriter

// verify that the "WriteLine" method was called from the interface with the exact input "1001, 10.53"
// the lambda expression is not really executing
// it is being turned into an expression tree and then analyzed by Moq, not actually being executed
mockFileWriter.Verify(fw => fw.WriteLine("1001, 10.53"), Times.Exactly(1));
// verify that the "WriteLine" method was called from the interface with any input string
mockFileWriter.Verify(fw => fw.WriteLine(It.IsAny<string>()), Times.Exactly(1));
// verify that the "WriteLine" method was called from the interface with input string that starts with "1"
mockFileWriter.Verify(fw => fw.WriteLine(It.IsRegex("^1001")), Times.Exactly(1));
// verify with predicates
// veirfy that the "WriteLine" method was called from the interface with input string that is longer than 3 characters
mockFileWriter.Verify(fw => fw.WriteLine(It.Is<string>(s => s.Length > 3)), Times.Exactly(1));

So far in the above two examples I’ve shown how to verify properties and methods in concrete classes. Now let me show you an example in using Moq to mock a return value for a method signature in an interface. Then using that expected return value in a method to verify a sum. The example below is a TaxProduct that has its price calculated with a Tax Calculator.