2014-10-20 3 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"); 
} 

Для сравнения, в Moq, вы можете сделать это:

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 и бросить свое сообщение.

Смежные вопросы