views:

43

answers:

1

My object under test has two dependency objects of the same type. Sometimes when a test has a failed expectation, it's not clear which dependency object set that expectation. Is there some way to give the dependency objects names that will appear in the error messages so that I can tell them apart?

Here's an example:

        MockRepository mocks = new MockRepository();
        var xAxis = mocks.StrictMock<IAxis>();
        var yAxis = mocks.StrictMock<IAxis>();
        Ball ball;

        using (mocks.Record())
        {
            Expect.Call(xAxis.Velocity).Return(100);
            Expect.Call(yAxis.Velocity).Return(0);
        }
        using (mocks.Playback())
        {
            ball = new Ball(xAxis, yAxis);
            ball.Bounce();
        }

Now if there's something wrong with the Bounce code, I might get a message like this:

Rhino.Mocks.Exceptions.ExpectationViolationException : IAxis.get_Velocity(); Expected #1, Actual #0.

I can't easily tell which axis got missed.

+2  A: 

I found a solution, but it's not quite what I had hoped for. You can add a message to each expectation. My example becomes:

        Expect.Call(xAxis.Velocity).Return(100).Message("x axis");
        Expect.Call(yAxis.Velocity).Return(0).Message("y axis");

And the exception is now more descriptive:

Rhino.Mocks.Exceptions.ExpectationViolationException : Message: x axis IAxis.get_Velocity(); Expected #1, Actual #0.

The only down side is that I have to add a message for every expectation. I was hoping to just name the mock object so that that name would appear in all messages.

Don Kirkby