2011-12-28 30 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))); 
相關問題