asp.net-mvcentity-frameworkrepository-patternunit-of-workrhino-mocks-3.5

Testing mocked EF context, context and unit of work with RhinoMocks and NUnit


I am having real problems mocking my code to enable me to test my MVC controllers.

My repository implements the following interface

public interface IEntityRepository<T>
{
    IQueryable<T> All { get; }
    IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);
    void Delete(int id);
    T Find(int id);
    void InsertOrUpdate(T entity);
    void InsertOrUpdateGraph(T entity);
}

Like so

public interface IMonkeyRepository : IEntityRepository<Monkey>
{
}

My EF context implements the following interface

public interface IMonkeyContext
{
    IDbSet<Monkey> Monkeys { get; set; }
    DbEntityEntry Entry(object entity);
    IEnumerable<DbEntityValidationResult> GetValidationErrors();
    int SaveChanges();
}

My unit of work interface is defined like so

public interface IUnitOfWork<TContext> : IDisposable
{
    TContext Context { get; }
    int Save();
} 

And implemented

public class MonkeyUnitOfWork : IUnitOfWork<IMonkeyContext>
{

    private readonly IMonkeyContext context;
    private bool disposed;
    public MonkeyUnitOfWork(IMonkeyContext context)
    {
        this.context = context;
    }
    public IMonkeyContext Context
    {
        get
        {
            return this.context;
        }
    }
    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    public int Save()
    {
        var ret = this.context.SaveChanges();
        return ret;
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                ((DbContext)this.context).Dispose();
            }
        }

        this.disposed = true;
    }
}

I have a MonkeyController whos Create action I wish to test. I is defined

        if (this.ModelState.IsValid)
        {
            this.repo.InsertOrUpdate(Mapper.Map<MonkeyModel, Monkey>(monkey));
            this.uow.Save();
            return this.RedirectToAction(MVC.Monkey.Index());
        }

        return this.View(monkey);

In my unit test I am using RhinoMocks and have defined the test

[TestFixture]
public class MonkeyControllerTests
{
    MockRepository mocks = null;

    private IMonkeyRepository monkeyRepository;

    private IMonkeyContext context;

    private MonkeyUnitOfWork unitOfWork;       

    private MonkeyController controller;

    [SetUp]
    public virtual void SetUp()
    {
        TestHelpers.SetupAutoMap();

        this.monkeyRepository = this.Mocks.StrictMultiMock<IMonkeyRepository>(typeof(IEntityRepository<Monkey>));

        this.context = this.Mocks.StrictMock<IMonkeyContext>();

        this.unitOfWork = new MonkeyUnitOfWork(this.context);

        this.controller = new MonkeyController(this.MonkeyRepository, this.unitOfWork);
    }

    [TearDown]
    public virtual void TearDown()
    {
        if (this.mocks != null)
        {
            try
            {
                this.mocks.ReplayAll();
                this.mocks.VerifyAll();
            }
            finally
            {
                this.mocks = null;
            }
        }
    }

    public MockRepository Mocks
    {
        get
        {
            if (mocks == null)
                mocks = new MockRepository();
            return mocks;
        }
    }

    [Test]
    public void MonkeyCreateShouldShouldDoSomeStuff()
    {
        var monkeyModel = ViewModelTestHelpers.CreateSingleMonkey();
        var monkey = Mapper.Map<MonkeyModel, Monkey>(monkeyModel);

        this.monkeyRepository.Expect(action => action.InsertOrUpdate(monkey));

        this.context.Expect(action => action.SaveChanges()).Return(1);
        var result = (RedirectToRouteResult)this.controller.Create(monkeyModel);

        Assert.AreEqual(MVC.Monnkey.ActionNames.Index, result.RouteValues["action"]);
    }
}

When I run my tests I either get the following errror

Previous method 'IMonkeyContext.SaveChanges();' requires a return value or an exception to throw.

Or it complains that the IEntityRepository.InsertOrUpdate expected 1 actual 0

I have tried so many casts, and incantations to get this to work but I am stumped. Does anyone know how to mock these object correctly? Or if I have fundamentaly missed something here?


Solution

  • Well it would seem to be a schoolboy error

    RhinoMocks was correct when it said that IEntityRepository.InsertOrUpdate was not being called.

    the line of code where I map from view model to model in my test

    var monkey = Mapper.Map<MonkeyModel, Monkey>(monkeyModel);
    

    and then use it in the expect

    this.monkeyRepository.Expect(action => action.InsertOrUpdate(monkey));
    

    was of course telling Rhino that the function should be called with this exact instance of monkey.

    The function is of course called in the following way within the action

    this.repo.InsertOrUpdate(Mapper.Map<MonkeyModel, Monkey>(monkey));
    

    Not the same instance.

    I have moved to the aaa syntax now and changed the test code to

    this.monkeyRepository.Stub(r => r.InsertOrUpdate(Arg<Monkey>.Is.Anything));
    

    and assert

    this.monkeyRepository.AssertWasCalled(r => r.InsertOrUpdate(Arg<Monkey>.Is.Anything));
    

    I will now go and hang my head in shame.