C# 如何为NSSubstitute.Received呼叫指定失败消息?
在NSubstitute中,是否可以指定在接收失败时应抛出的消息?如下所示:C# 如何为NSSubstitute.Received呼叫指定失败消息?,c#,nsubstitute,C#,Nsubstitute,在NSubstitute中,是否可以指定在接收失败时应抛出的消息?如下所示: [Test] public void Should_execute_command() { var command = Substitute.For<ICommand>(); var something = new SomethingThatNeedsACommand(command); something.DoSomething(); command.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");
}
然后,您将在测试运行程序中获得该消息作为测试失败消息的一部分。这有助于使测试故障更易于读取/诊断。NSubstitute中是否有类似的内容?无法更改您在
ReceivedCallException
中收到的消息;它直接从NSubstitute.Core.receivedcallsexceptionRower
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());
}
public void Throw(ICallSpecification callSpecification、IEnumerable matchingCalls、IEnumerable nonMatchingCalls、Quantity required Quantity)
{
StringBuilder StringBuilder=新的StringBuilder();
stringBuilder.AppendLine(string.Format(“预期接收{0}匹配:\n\t{1}”,requiredQuantity.Descripte(“调用”,“调用”),调用规范));
此.AppendMatchingCalls(callSpecification、matchingCalls、stringBuilder);
if(requiredQuantity.requiresMore(匹配调用))
{
此.AppendNonMatchingCalls(调用规范、nonMatchingCalls、stringBuilder);
}
抛出新的ReceivedCallsException(stringBuilder.ToString());
}
除非您已经准备好深入NSubstitute代码,否则目前最好的办法是捕获ReceivedCallsException
并抛出您自己的消息
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());
}