views:

241

answers:

2

My test method look like this:

[Test]
    public void Generated_CaseNumber_should_be_set_as_LastCaseNumber_in_PropertiesManager()
    {           

        String generatedCaseNumber = _sut.NextCaseNumber();

    }

_sut object has a dependency to mocked object of type IPropertiesManager. Method _sut.NextCaseNumber() should generate unique CaseNumber, store it in IPropertiesManager (using UpdateLastCaseNumber() method) and then return generated CaseNumber.

How do I assert that _sut.NextCaseNumber() called UpdateLastCaseNumber() on IPropertiesManager type and passed in the same parameter that it then returned to the caller of NextCaseNumber()?

A: 

You need to get your _sut to use a mock of IPropertiesManager.

One way to do this is to change the / create anew constructor of _sut to take an IPropertiesManager.

When creating _sut for the test send in a mock of IPropertiesManager. When using it in the real code either send in the real object or use a different constructor that creates an IPropertiesManager.

Shiraz Bhaiji
+1  A: 

Adding something like this should work.

using (mocks.Record())
{
    Expect.On(mockObj).Call(mockObj.UpdateLastCaseNumber(param)).Return(param);
}
using (mocks.Playback())
{
    var expected = param;
    var actual = _sut.NextCaseNumber(param);
    Assert.AreEqual(expected, actual);
}

(mocks = new MockRepository, mockObj = IPropertiesManager)

Martin