Mocking with RhinoMocks

I have recently started getting into TDD, and have begun using mocking (specifically RhinoMocks http://ayende.com/projects/rhino-mocks.aspx). Mocking is a mechanism by which you can effectively force dependencies required by a specific piece of code to behave in a certain way, thus effectively meaning that your tests are just testing that specific piece of code (a more detailed explanation can be found here.

To allow a dependant object on a class to be mocked, an interface is normally defined for the dependant object, and an instance of this is usually made a member of the class (to ensure loose coupling). An additional constructor can then be added to to the class that allows an instance of the dependant object interface to be passed in (thus allow for a new implementation of that dependant object). This principle is known as Dependency Injection.

Note that as mocking frameworks effectively override the behaviour of a method in an object, the object being mocked must either be an interface, or have methods that are declared as virtual.

Example : Mocking Data Access

public class Foo
{
    private IDataAccessLayer dal;

    public Foo()
    {
        this.dal = new ConcreteDal();
    }

    public Foo(IDataAccessLayer dataAccessLayer)
    {
        this.dal = dataAccessLayer;
    }

    public int Bar()
    {
        // Call database to get count of something
        var count = dal.GetCountOfSomething(“SpecificGroupOrSomething”);

        // Perform a calculation (no specific reason for 12)
        var result = count * 12;

        return result;
    }
}

public interface IDataAccessLayer
{
    int GetCountOfSomething(string key);
}

public class ConcreteDal : IDataAccessLayer
{
    public int GetCountOfSomething(string key)
    {
        // Perform a call to the database and do something complicated!

        // 99 would be the count value returned from the database
        return 99;
    }
}

Performing the test

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RhinoMocksExample1;
using Rhino.Mocks;

namespace TestProject1
{
    [TestClass]
    public class FooTests
    {
        [TestMethod]
        public void TestMethod1()
        {
            var mockRepository = new MockRepository();
            var dal = mockRepository.StrictMock(typeof(IDataAccessLayer), null) as IDataAccessLayer;
            int result;

            using (mockRepository.Record())
            {
                Expect.Call(dal.GetCountOfSomething(string.Empty)).IgnoreArguments().Return(10);
            }

            using (mockRepository.Playback())
            {
                var foo = new Foo(dal);
                result = foo.Bar();
            }

            Assert.AreEqual(120, result);
        }
    }

 

Notes:

If when referencing the Rhino.Mocks.dll, and running your tests you get an error of the form “The location of the file or directory is not trusted”, navigate to the dll in windows explorer, right click and select properties and on the general tab click the unblock button. Restart visual studio after this.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>