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.
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:
And the exception is now more descriptive:
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.