【问题标题】:How can I specify a failure message for an NSubstitute .Received call?如何为 NSubstitute .Received 调用指定失败消息?
【发布时间】:2014-10-20 14:59:48
【问题描述】:

在 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");
}

为了比较,在起订量中,您可以这样做:

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

然后您会在测试运行程序中将该消息作为测试失败消息的一部分。这有助于使测试失败更容易阅读/诊断。 NSubstitute 有类似的吗?

【问题讨论】:

    标签: c# nsubstitute


    【解决方案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 并抛出您自己的信息。

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多