Codelog

foreach(Snippet aSnippet in CodeLog){ aSnippet.GetSolution(); }

Settings expectations in NMock2

without comments

One of our previous articles would have given an introduction to NMocks. In here, we will try out various methods of setting expectations when we do a unit test.

note — this article considers that you know the basics of unit testing. check out our previous article if you would like to know the basics of nmock2

In NMock2, you have a wide variety of setting expectations on mocked objects.

Stubs

Stubs are expectations whose occurrences are unknown. We use stubs when we are not sure the number of times it might be called. So generally Stubs are used when expectations needs to be set 0 or more times

Stub.On(myMockedObject).Method("GetName");  //Use this only when you are unaware of the occurrences,
                                            //otherwise you end up writing a bad UT

Expect

We use the Expect class when the number of calls to the method or property of the mock objects are known.

For example,

If number of occurrences are 7 on the method called, “GetName“, then use,

Expect.Exactly(7).On(myMockObject).Method("GetName")...

If the number of occurrence is 1 on the method called, “GetName“, then use,

Expect.Once.On(myMockObject).Method("GetName")...

or

Expect.Exactly(1).On(myMockObject).Method("GetName")...

You can even set negative expectations. For example,
If you are pretty sure that the method “GetName“, will not/should not be called in your unit test, then use,

Expect.Never.On(myMockObject).Method("GetName")...

Expectation With Parameters

If you are unit testing a method that makes a call to an interface which inturn takes two params and if you know the values of those params, you could set expectation on the mock object for the method with the known params.

For example,

If “GetName” method takes two params and if the param values are “foo” and “bar” according to the unit test case, then we can write an expectation as,

Expect.Once.On(myMockObject).Method("GetName").With("foo", "bar")...

Expectation On Events

You can add expectation on events of the mock object with the EventAdd method.

For example,

public class Foo
{
  public event EventHandler ObjectChanged;
  ...
}
public class FooBarBaz
{
  private Foo myFoo;
  ...
  private void SomeMethod()
  {
    myFoo.ObjectChanged += new EventHandler(Foo__ObjectChanged);
    ...
  }
}

If you are writing a unit test for the method SomeMethod, then you shall add an expectation of the following form,

Expect.Once.On(myMockObject).EventAdd("ObjectChanged", Is.Anything);

Expecting Exceptions

You could also expect exceprions by adding the attribute, ExpectedException on top of your unit test.

For example,

[Test(Description="Test case method that will test whether the ArgumentNullException is raised")]
[ExpectedException(typeof(ArgumentNullException))]
public void TestExceptionalScenario()
{
  ...
  //call the method that is supposed to throw the ArgumentNullException
}

These are the commonly used expectations. If you have used something really weird, I expect you share it with us     :D        :D

Written by sudarsanyes

September 3rd, 2009 at 2:24 pm

Posted in C#, Testing, unittest

Tagged with , , ,

Leave a Reply