Kash Farooq's software development blog

.NET Developer

Rhino Mocks and AssertWasCalled MethodOptions

Posted by Kash Farooq on September 3, 2009

I always seem to struggle to find examples of how to use MethodOptions in Rhino Mocks, so here is some code.

Assume you have the following dependency:

public interface IDependency {
   void SomeMethod(int number);
   void SomeMethod(ComplexType complexType);
}

public class ComplexType {
   public string Name { get; set; }
}

And your system under test calls this dependency like this:

public class SystemUnderTest {
   private readonly IDependency dependency;
   public SystemUnderTest(IDependency dependency) {
      this.dependency = dependency;
   }

   public void DoSomething() {
       dependency.SomeMethod(9);
       dependency.SomeMethod(3);
   }

   public void DoSomethingElse() {
       dependency.SomeMethod(new ComplexType{Name = "London"});
   }
}

You can use MethodOptions to check that the dependency was called correctly.

First, let’s check that SomeMethod was called twice with integers greater than 1:

[Test]
public void EnsureDependencyCalledTwiceWithAnIntegerGreaterThanOne() {
  var dependency = MockRepository.GenerateStub<IDependency>();
  var systemUnderTest = new SystemUnderTest(dependency);
  systemUnderTest.DoSomething();

  dependency.AssertWasCalled(x => x.SomeMethod(0), 
                                                 options => 
                                                 {
                                                     options.Repeat.Twice();
                                                     options.Constraints(Is.GreaterThan(1));
                                                     options.Message("SomeMethod should have been called twice with integers greater than 1");
                                                 });
}

Next, let’s check that the correct ComplexType was sent to SomeMethod

[Test]
public void EnsureDependencyCalledWithCorrectComplexType() {
  var dependency = MockRepository.GenerateStub<IDependency>();

  var systemUnderTest = new SystemUnderTest(dependency);
  systemUnderTest.DoSomethingElse();

  Func<ComplexType, bool> checkArgs = arg => arg.Name == "London";
  //The above line can be written with an anonymous method instead:
  //Func<ComplexType, bool> checkArgs = delegate(ComplexType arg) { return arg.Name == "London"; };

  dependency.AssertWasCalled(x => x.SomeMethod(new ComplexType()), 
                                                        options => {
                                                           options.Callback(checkArgs);
                                                           options.Message("Name sent was not London");
                                                        });
}
Advertisements

One Response to “Rhino Mocks and AssertWasCalled MethodOptions”

  1. russelleast said

    Thanks, I have spent ages try to figure out how to assertwascalled “repeat.times(x)”

Sorry, the comment form is closed at this time.

 
%d bloggers like this: