2011-12-28 26 views

回答

3

this article,您可以传递给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"); 
    }); 
+0

你确定语法吗?具有选项=>的行在什么后面开始? – pencilCake 2011-12-28 08:22:55

+0

@pencilCake,对不起,忘了逗号。回答固定,谢谢你的提问:) – 2011-12-28 08:23:55

+0

谢谢!我没有意识到SetupConstraint的可能性。另一方面,为了能够在Asserting阶段设置返回值,对我来说看起来有点奇怪。我对API饮食感到好奇,Ayende为即将到来的4.0版本写信。 – pencilCake 2011-12-28 08:34:42

0

据我所知犀牛没有指定自己的消息的方式。

你可以编写自己的方法来捕获Rhino的ExpectationViolationException,然后打印出你想要的信息。

这方面的一个粗略的例子是:

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; 
     } 
    } 
} 

的使用将被:

mockBookingService.AssertWasCalledWithMessage(ms=>ms.UnBookFlight(Arg<DateTime>.Is.Equal(dummyDate))); 
相关问题