Mocking Adventures with NMock2: Stubs

NMock2 is a library for assisting test driven development of .NET code by providing a dynamic mock object creation framework.

In this article, I’m going to show you the basics of the new stub feature in NMock2. Note that this feature is currently only available on the development trunk in the subversion repository at https://nmock2.svn.sourceforge.net/svnroot/nmock2/trunk. Therefore, the features discussed here may change for the next official release.

Stubs can be used in scenarios where you have to test an instance of a class (let’s call this object testee) and this testee makes calls to another object (dependency) but you simply do not care what the testee calls on the dependency because it is not relevant for your test case.

Stubs will simply ignore any calls to it and if the call has a return value then the stub provides a default value.

Time for an example:

var mockery = new Mockery();
var mock = mockery.NewMock<IMock>(MockStyle.Stub);

This creates a new dynamic mock for the interface IMock with style stub.

You can now make calls to the mock and it will not answer with exceptions about unexpected invocations, although you have not defined any expectations as you would normally have to with default mocks.

Stubs Return Default Values

Given this IMock interface:

public interface IMock
{
 string String { get; set; }
 int GetInteger();
 List<int> GetList();
 void NoReturnValue();
}

The behaviour is as follows:

  • mock.NoReturnValue() will simply be ignored.
  • mock.String returns an empty string.
  • mock.GetInteger() returns 0 (zero).
  • mock.GetList() returns an empty list of integers.

As you can see, a stub will try to return a meaningful default value for the return type of the property or method.

Stubs Remember Values

Stubs will remember returned values, too. Whatever value the mock returned for a call, will be returned on following calls.

mock.GetList() == mock.GetList() will result in true

If you set the value on a getter and setter property, this value will be returned on calls to the getter. In the following example, url will be set to “planetgeek.ch”.

mock.String = “planetgeek.ch”;
var url = mock.String;

Stubs Can Build Up Hierarchies

Stubs return mocks as return values if the type of the property or method is either an interface or a class and not a string, class implementing IEnumerable, value type or sealed class.

This allows your stubs to build up whole hierarchies.

Let’s consider the following interface and class:

public interface IDependency
{
    Dependency AnotherDependency { get; }
}

public abstract class Dependency
{
    public abstract IDependency AnotherDependency { get; }
    public abstract string Do();
}

We can do now this:

var mock = mockery.NewMock<IDependency>(MockStyle.Stub);

var dependency = mock.AnotherDependency.AnotherDependency.AnotherDependency;

This will create three nested mocks, each with a mock style equal to stub.

Now, you can go on and define expectations on nested mocks, or call other methods:

Expect.Once.On(dependency).Method(“Do”).Will(Return.Value(“cool”));

var result = mock.AnotherDependency.AnotherDependency.AnotherDependency.Do();

You guess right, the result is “cool”.

Stub Naming

Naming of stubs is very important in order to get meaningful exception messages if anything goes wrong in your tests. Therefore, automatically created mocks get there name from the path on which they were first accessed. In the example above, the mock assigned to the variable dependency will be named dependency.AnotherDependency.AnotherDependency.AnotherDependency. The name is created of the name of the root mock – this name can be defined when the mock is created (e.g. mockery.NewNamedMock<IMock>(“myMock”)) – combined with the property or method names of the path of its first access.

Next Time

That’s it for now. In my next post on this topic, we’ll going to have a look at the advanced features regarding stub style in NMock2. Maybe, you find some time until then to take a look at this new feature for yourself or at least, write a comment below.

Happy mocking

About the author

Urs Enzler

7 comments

  • Is there any way to stub a class that doesn’t implement an interface? If I have a class called MyClass, I know that I can use RhinoMocks to create a stubbed version of it. (VB.Net code)
    MockRepository.GenerateStub(Of MyClass)(“MyParam”)

    Does NMock2 give this ability, too? I notice that in your example, we’re still mocking an interface.

  • Hello David
    Yes it is possible to mock classes. See project description on sourceforge:

    NMock2 is a library for assisting test driven development of .NET code by providing a dynamic Mock Object framework for .NET interfaces and classes (virtual and abstract members of classes).

    http://sourceforge.net/projects/nmock2/

    Hope that helps
    Daniel

  • @David Ree
    Hi David

    As Daniel said, you can set expectations on abstract or virtual properties, methods and indexers of classes in the same way as on interfaces. The reason I use interfaces in the samples is that in my opinion, it’s bad design to program against classes directly.

    All open source mocking frameworks (at least all I know: Rhino Mocks, MoQ) are based on DynamicProxies2, therefore they have all more or less the same possibilities. If you are looking for a more powerful (commercial) framework then take a look at Isolator from TypeMock.

    Happy mocking
    Urs

  • Hi,

    Thanks for sharing this preview of the MockStyle.Stub feature! Its exactly was I was looking for when I googled this page, so now I just can’t wait for a release with this included 😉

    I tried to find some information on futre release plans but could not find any. Could anybody help me out? When could we3 expect a new NUnit supporting MockStyle.Stub?

    Cheers!

    Henrik

By Urs Enzler

Recent Posts