0
我刚开始使用NSubstitute
。我大多用Moq
工作,这是我在做什么:NSubstitute不打印出NUnit声明
// In my unit test on menter code herey mock:
HasLogMessage(Is.EqualTo("expected value"));
private void HasLogMessage(EqualConstraint s)
{
log.Verify(y => y.Error(It.Is<string>(v => Verify(v, s))));
}
private bool Verify(string s, EqualConstraint equalConstraint)
{
Assert.That(s, equalConstraint);
return true;
}
输出运行单元测试时。需要注意的是它告诉我们所期望的和真正的价值是:
Expected string length 14 but was 116. Strings differ at index 0.
Expected: "expected value"
But was: "real value..."
-----------^
at NUnit.Framework.Assert.That(Object actual,
IResolveConstraint表达,字符串消息,
我希望能够使用与NSubstitute
嘲笑,这是我的在尝试的对象[]参数)这样的:
HasLogMessage(Is.EqualTo("Expected value"));
private void HasLogMessage(EqualConstraint s)
{
log.Received().Log(LogLevel.Error, Arg.Is<Func<string>>(x => Verify(x,
}
private bool Verify(Func<string> s, EqualConstraint equalConstraint)
{
Assert.That(s(), equalConstraint);
return true;
}
但这并不输出NUnit
断言错误
NSubstitute.Exceptions.ReceivedCallsException : Expected to receive a call matching:
Log(Error, x => value(IdentityServer3.Saml2Bearer.Tests.Saml2BearerGrantValidatorTest)
.Verify(x, value(IdentityServer3.Saml2Bearer.Tests.Saml2BearerGrantValidatorTest+<>c__DisplayClass21_0).s), <null>,)
Actually received no matching calls.
Received 2 non-matching calls (non-matching arguments indicated
with '*' characters)
我在这里错过了什么吗?日志型澄清后
你的例子不适合在这里,第二个参数不是一个字符串,它是'Func'。我想这样做是行不通的,因为NSubstitute不会评价我的第二个,它只会说接收到一个非maatching的调用,其中第二个param是一个不同的函数。 (LogLevel.Error,()=>“expected”);'Log.Received().Log(LogLevel.Error, –
Darius