Wednesday, November 14, 2007

MockingTestFixture makes NMock tests simpler

I just noticed that not everyone is using a helper class when using NMock2. It seriously makes your code cleaner. Here is the one I use.

using System;
using NMock2;
using NUnit.Framework;


namespace Tests.Utilities
{
public abstract class MockingTestFixture
{
private Mockery mockery;

protected IDisposable Ordered
{
get { return mockery.Ordered; }
}

[SetUp]
public void MockSetUp()
{
mockery = new Mockery();
SetUp();
}

protected abstract void SetUp();

[TearDown]
public virtual void MockTearDown()
{
TearDown();
VerifyExpectations();
}

protected virtual void TearDown()
{
}

public T NewMock()
{
return mockery.NewMock();
}

public void VerifyExpectations()
{
mockery.VerifyAllExpectationsHaveBeenMet();
}

protected static void IgnoreReturnValue(object ignored)
{
}
}
}

Derive your test fixture from this class. Override the abstract setup method... there you go. No more need to worry about the Mockery.
Post a Comment

GitHub Projects