NSubstitute的失败消息。已接收的电话

时间:2014-10-20 14:59:49

标签: c# nsubstitute

在NSubstitute中,是否可以指定在Received失败时应该抛出的消息?如下所示:

[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 个答案:

答案 0 :(得分:1)

无法更改ReceivedCallException中收到的消息;它是直接使用NSubstitute.Core.ReceivedCallsExceptionThrower Throw方法中的硬编码字符串构建的:

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并投出自己的信息。