.NET logo

MockGen - C# mock object code generator

home | software | mockgen

using the tool

MockGen generates mock implementations of interfaces which are designed to be used by unit tests. The tool integrates with VisualStudio.NET, and is very simple to use.

MockGen may be downloaded and installed as described below. To generate a mock implementation, select the interface to mock in the Solution Explorer. Set the "Custom Tool" property to "MockGen", as shown:

Screenshot showing the integration of MockGen into the Visual Studio .NET IDE

generated classes

MockGen parses the interface file source code, and may produce one or more of the following:

  1. A mock implementation of the interface, in the sub-namespace 'Tests', with public members:
  2. Strongly-typed EventCounter classes for any delegates defined in the source file
  3. Support for inheritance of other interfaces

example output

See a side-by-side example of the generated code. This page outlines the interface through which you may communicate with the generated mock class.

static vs dynamic mocks

When starting out with mock objects, many of us write them by hand (static mocks). This soon grows tiresome, and unsurprisingly many tools exist. NMock is a very popular dynamic mocking tool, written by some friends I worked with at ThoughtWorks. As a dynamic mocking tool, it creates a mock instance of a type at runtime, and assertions can be stated regarding the types members via their names as string literals.

MockGen generates static mocks that are compiled with your source. This gives compile-time checking. For example, when a member name is changed the compiler warns you, whereas with dynamic mocking you must update the string literals in your test code manually (any you miss should appear as failing unit tests). The static mocks generated by MockGen also give you intellisense, which is helpful when authoring your tests.

limitations

MockGen only mocks interfaces (though it also creates EventCounter classes from delegates). With some additional work, the tool could be extended to create mock implementations of virtual/abstract members from concrete/abstract classes.

At present, MockGen only generates mock implementations of types for which the source is in your solution.

MockGen (at present) only integrates with visual studio, though it would be trivial to include a command line version.

download

Integration with Visual Studio is achieved in the manner discussed here. This version only supports VS.NET 2003, though with a small amount of effort using BaseCodeGeneratorWithSite.zip (included), the code can be re-compiled to support VS.NET 2000.

NOTE Unfortunately a manual step is required to register MockGen with VisualStudio.NET. After running the MSI, navigate to the installation folder (by default Program Files\Drew Noakes\MockGen) and run install.cmd. If you are able to automate this step in the installer project, I'd be most grateful (I've tried, but was unable to do so).

submitting feedback

Please feel free to provide feedback on this class . Bug reports are most appreciated when accompanied by a failing NUnit test case.