Rhino Mocks Tutorial

Rhino Mocks is the mature dynamic mocking framework for .NET unit testing. It create fake/mock implementation of objects behavior at runtime.

Rhino Mocks is open source framework and released under the BSD license.

Rhino Mocks works on Arrange, Act and Assert (AAA) pattern. Rhino Mocks allows both State verification and Behavior verification of objects.

Rhino Mocks can not mock static and sealed methods of a class.

Before starting the tutorial, we must understand the difference between State verification and Behavior based Verification techniques.

State verification vs Behavior Verification

In state verification we performed the operation on an object and then check whether object under test worked correctly by checking the states/variables of object and its associated objects.

For e.g. we have Warehouse object which has 1 product with 5 quantity. In WarehouseOrder class, we have Order method which places the order for the product and decrease the quantity which supplied in an argument. 

In the test, we call the Order method with 2 as quantity parameter. After calling the Order method, we will check the quantity of the Warehouse object. If the quantity is 3 then the test is passed else fail.

In behavior verification is a further step in verification. In behavior verification, we also check whether the sequence of operations in the object was correct and in particular order.

For e.g. in the above Order method of WarehouseOrder class. There are three methods called in the implementation of Order method in below Order.

  1. Warehouse.DecreaseQuantity()
  2. WarehouseOrder.SaveOrder()
  3. Warehouse.NotifyOthers()

Behavior verification verify that these methods are actually called and called in above order.

State based tests only check the results but behavior based tests also test how an object gets the results.

Test Doubles

In Rhino Mocks we create implementation of dependencies that act as stand-in for the real implementations. Such implementation are called Test Doubles.

Test Doubles is an object that is used as fake object in place of dependent object. Test Doubles are never used in final application. These objects are only used in unit testing of an application.

There are various terms related to test doubles.

Dummy Object

These objects are simple objects that stand-in for dependent objects. They usually return a predefined response that are not vary based on input parameters.

Below is the example of dummy object.

public interface IOrder
{
    int GetDiscount(int quantity);
}

public class DummyObject : IOrder
{
    public int GetDiscount(int quantity)
    {
        return 4;
    }
}

In the above example, we directly return 4 as discount without using the quantity parameter. 

Stub Object

Stub object is used for State verification. Stub object vary their response based on input parameters. Below is the example.

public interface IOrder
{
    int GetDiscount(int quantity);
}

public class StubObject : IOrder
{
    public int GetDiscount(int quantity)
    {
        if (quantity < 4)
        {
            return 1;
        }
        else if (quantity < 10)
        {
            return 2;
        }
        else
        {
            return 4;
        }
    }
}

In the above example, we check the quantity parameter and vary the response based on that.

Mock Object

Mock object is used for Behavior verification. Mock object is a step up from Stub object. Mock object are also stand-in for dependent object. They are also pre-programmed with expectations. Mock objects also check the implementation of the test method. It also tracks how many times a particular method was called and in what order a sequence of methods were called.

Rhino Mocks Tutorial

Now we understand state and behavior verification and also test doubles. In the next series of posts, I will go through to basic concepts of Rhino Mocks and how to create simple unit tests with it. In the end posts, I will go through into the more advance features of Rhino Mocks.