2

Can I write the following assert on the mock object, in a way that it shows "UnBookFlight was not called with proper parameters or not even called" in case the assert fails?

mockBookingService
              .AssertWasCalled(ms=>ms.UnBookFlight(Arg<DateTime>.Is.Equal(dummyDate)));
pencilCake
  • 51,323
  • 85
  • 226
  • 363

2 Answers2

3

According to this article, you can specify a message in the method options passed to AssertWasCalled():

mockBookingService.AssertWasCalled(
    ms => ms.UnBookFlight(Arg<DateTime>.Is.Equal(dummyDate)),
    options => {
        options.Message("UnBookFlight was not called with proper parameters or not even called");
    });
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Are you sure about the syntax? the line with options=> starts after what exactly? – pencilCake Dec 28 '11 at 08:22
  • @pencilCake, sorry, forgot a comma. Answer fixed, thanks for the heads-up :) – Frédéric Hamidi Dec 28 '11 at 08:23
  • Thanks! I was not aware of that SetupConstraint possibility. On the other hand, to have the ability to set a return value on the Asserting stage looks a bit odd to me. I am curious about the API Diet, Ayende wrote for the upcoming 4.0 version. – pencilCake Dec 28 '11 at 08:34
  • Do you have any idea on my other question, Frederic? http://stackoverflow.com/questions/8653354/can-i-test-method-call-order-with-aaa-syntax-in-rhino-mocks-3-6 – pencilCake Dec 28 '11 at 08:35
0

As far as I know Rhino doesn't have a way of specifying your own messages.

You could write your own method which catches Rhino's ExpectationViolationException, then prints out the message you want.

A rough example of this would be:

public static class RhinoExtensions
{
    public static void AssertWasCalledWithMessage<T>(this T mock, Expression<Func<T, object>> action)
    {
        try
        {
            mock.AssertWasCalled(action.Compile());
        }
        catch (ExpectationViolationException)
        {
            Console.WriteLine(string.Format("{0} was not called with proper parameters or was not called.", (action.Body as MethodCallExpression).Method.Name));
            throw;
        }
    }
}

The usage would then be:

mockBookingService.AssertWasCalledWithMessage(ms=>ms.UnBookFlight(Arg<DateTime>.Is.Equal(dummyDate)));
David_001
  • 5,703
  • 4
  • 29
  • 55