2014-10-20 74 views
1

在NSubstitute中,是否可以指定在接收失敗時應該拋出的消息?像下面這樣:NSubstitute的失敗消息。接收到的調用

[Test] 
public void Should_execute_command() 
{ 
    var command = Substitute.For<ICommand>(); 
    var something = new SomethingThatNeedsACommand(command); 

    something.DoSomething(); 

    command.Received() 
     .Execute() 
     .Because("We should have executed the command that was passed in"); 
} 

爲了進行比較,在起訂量,你可以這樣做:

command.Verify(c => c.Execute, "We should have executed the command that was passed in"); 

然後你得到的消息在您的測試運行的測試失敗消息的一部分。這可以幫助使測試失敗更容易閱讀/診斷。 NSubstitute中有類似的東西嗎?

回答

1

無法更改您在ReceivedCallException中收到的消息;它是直接從硬編碼字符串建於NSubstitute.Core.ReceivedCallsExceptionThrowerThrow方法:

public void Throw(ICallSpecification callSpecification, IEnumerable<ICall> matchingCalls, IEnumerable<ICall> nonMatchingCalls, Quantity requiredQuantity) 
{ 
    StringBuilder stringBuilder = new StringBuilder(); 
    stringBuilder.AppendLine(string.Format("Expected to receive {0} matching:\n\t{1}", requiredQuantity.Describe("call", "calls"), callSpecification)); 
    this.AppendMatchingCalls(callSpecification, matchingCalls, stringBuilder); 
    if (requiredQuantity.RequiresMoreThan<ICall>(matchingCalls)) 
    { 
     this.AppendNonMatchingCalls(callSpecification, nonMatchingCalls, stringBuilder); 
    } 
    throw new ReceivedCallsException(stringBuilder.ToString()); 
} 

除非你準備在NSubstitute代碼潛水,對於現在最好的選擇將趕上ReceivedCallsException並拋出自己的消息。

相關問題