【发布时间】:2015-11-20 13:33:15
【问题描述】:
我刚刚开始使用NSubstitute。我主要与Moq 合作,这就是我正在做的事情:
// 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 表达式,字符串消息,Object[] args)
我希望能够将它与 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)
我错过了什么吗?
【问题讨论】:
标签: c# unit-testing nsubstitute