Space shuttles aren't built for rocket scientists, they're built for astronauts. The goal isn't the ship, its the moon.
posts - 300, comments - 180, trackbacks - 35

My Links



Post Categories

Published Works

Rhino Mocks Ignore Arguments

I am writing some tests for a project I am working on called Green Fee Broker.

The first thing I am doing is mocking the business layer for the WCF service that I am writing so I can inject it into the service’s constructor and write the service while isolating dependencies:

MockRepository mockRepository = new MockRepository();

IBookingManager manager = mockRepository.DynamicMock<IBookingManager>();

Next, I set an expectation that CreateTeeTime is called on the manager mock, which is injected into the service constructor:

Expect.Call(manager.CreateTeeTime(course, DateTime.Now)).Return(teeTimeStub);

Finally, I ReplayAll to record the mock, call the service and assert that I am getting back a TeeTime object:


BookingService service = new BookingService(manager);

TeeTime teeTime = service.CreateTeeTime(course, DateTime.Now);

Assert.IsTrue(teeTime.ConfirmationNumber == "ABC123");

Unfortunately, there is a bug in the line of code that sets up the expectation. With Rhino Mocks, for an expected behavior to be a match, the parameters must be an exact match. Notice the second parameter uses DateTime.Now. This means that the expectation will have a different parameter than the actual call, even if it is just a few milleseconds of a difference. As a result, the assertion will fail because the return is null.

The solution is to add the IgnoreArguments method call on the expectation:

Expect.Call(manager.CreateTeeTime(course, DateTime.Now)).Return(teeTimeStub).IgnoreArguments();

The IgnoreArguments() call will ensure that the parameters are discarded completely in determining a match.

Now I am green, and good to go!


Print | posted on Friday, June 12, 2009 12:47 AM | Filed Under [ WCF .NET 3.5 TDD ]

Comments have been closed on this topic.

Powered by: