Rhino Mocks

Dynamic mock object framework for .NET

Its purpose is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing.

What is it doing?

Rhino Mocks will generate fake objects to replace the dependencies that you have, and then allow you to tell them, at runtime, how to behave. This functionality is very powerful, and it means that you can tell your fake objects, for each test, how to behave.

Show me the code

As usual, the best way to explain what a library does is to look at the code using it. Here is an example of verifying that when we execute the “forgot my password” scenario, we remembered to call the Save() method properly:

public void When_user_forgot_password_should_save_user()
{
    var stubUserRepository = MockRepository.GenerateStub<IUserRepository>();
    var stubbedSmsSender = MockRepository.GenerateStub<ISmsSender>();
    var theUser = new User{HashedPassword = "this is not hashed password"};    
    stubUserRepository.Stub(x => x.GetUserByName("ayende")).Return(theUser);
    var controllerUnderTest = new LoginController(stubUserRepository, stubbedSmsSender);
    controllerUnderTest.ForgotMyPassword("ayende");
    stubUserRepository.AssertWasCalled(x => x.Save(user));
}

License

Rhino Mocks is Open Source and released under the BSD license.

Community

Rhino Mocks has a vibrant community, mostly in the mailing list.

Learn

Assert that a method is called with a value in an expected state

When you have a dependency that your system under test uses, you may want to ensure that it is called with a value in an expected state. The following example is looking up a Foo and we’re going to ensure that we’re calling the FooRepository with the Foo’s Name property.

/// <summary>
/// Asserting that an instance inside your system under test is called with a parameter
/// matching a set of expected criteria. This example shows that our FooService should
/// call it's dependency's method with the Name property of the Foo object passed into
/// the LookUpFoo method.
/// </summary>
[Test]
public void How_to_Assert_that_a_method_calls_an_expected_method_with_value()
{
    // Arrange
    var foo = new Foo {Name = "rhino-mocks"};
    var mockFooRepository = MockRepository.GenerateStub<IFooRepository>();
    var fooService = new FooService(mockFooRepository);
    // Act
    fooService.LookUpFoo(foo);
    // Assert
    mockFooRepository.AssertWasCalled(x => x.GetFooByName(Arg<string>.Matches(y => y.Equals(foo.Name))));
}

Mocking a Read-Only Property

If you come into a situation where you need to mock a read-only property on a class, here is how to do so:

/// <summary>
/// When you need to mock a read-only property of a class.
/// </summary>
[Test]
public void How_to_Stub_out_your_own_value_of_a_ReadOnlyProperty()
{
    // Arrange
    var foo = MockRepository.GenerateStub<IFoo>();
    foo.Stub(x => x.ID).Return(123);
    // Act
    var id = foo.ID;
    // Assert
    Assert.That(id, Is.EqualTo(123));
}

This code is fairly simple and straightforward. Using MockRepository.GenerateStub(), you’ll mock an instance of IFoo, then have the ability to control what the return value is for a specific function. After the line declaring and setting the variable foo, you’ll see that I’m using the Stub extension method to pass a lambda expression of the property of which I want to control the return value. Now, my instance of IFoo will return 123 whenever the property ID is retrieved.

This test is not testing any business logic, it just shows how to control the return value of a read-only property. For the entire code example, please check out the RhinoMocks.GettingStarted project.