C# NSSubstitute不打印NUnit断言
我刚刚开始使用C# NSSubstitute不打印NUnit断言,c#,unit-testing,nsubstitute,C#,Unit Testing,Nsubstitute,我刚刚开始使用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
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;
}
IResolveConstraint表达式、字符串消息、对象[]args)
我希望能够将其用于NSubstitute
mock,以下是我的尝试:
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;
}
我在这里遗漏了什么吗?日志类型澄清后更新:
除了使用之外,对于Func
我将捕获使用的参数,并在以后检查它们
var log = Substitute.For<ILog>();
var loggedErrors = new List<string>();
// Capture the argument passed to Log whenever LogLevel.Error is called
log.Log (LogLevel.Error, Arg.Do<Func<string>>(x => loggedErrors.Add(x())));
log.Log(LogLevel.Error, () => "the real call...");
Assert.That(loggedErrors, Is.EqualTo (new[] { "expected" }));
/*
NUnit.Framework.AssertionException: Expected is <System.String[1]>, actual is <System.Collections.Generic.List`1[System.String]> with 1 elements
Values differ at index [0]
Expected string length 8 but was 16. Strings differ at index 0.
Expected: "expected"
But was: "the real call..."
-----------^
*/
如果您想使用断言库进行更具描述性的匹配,NSubstitute中存在一些不完整的管道,需要做一些工作才能做到这一点。有。您的示例不适合这里,第二个参数不是字符串,而是
Func
。我想这是行不通的,因为NSubstitute不会计算我的第二个参数,它只会说收到了一个非maatching调用,其中第二个参数是一个不同的func<代码>log.Received().log(LogLevel.Error,()=>“预期”)代码>
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)
var log = Substitute.For<ILog>();
var loggedErrors = new List<string>();
// Capture the argument passed to Log whenever LogLevel.Error is called
log.Log (LogLevel.Error, Arg.Do<Func<string>>(x => loggedErrors.Add(x())));
log.Log(LogLevel.Error, () => "the real call...");
Assert.That(loggedErrors, Is.EqualTo (new[] { "expected" }));
/*
NUnit.Framework.AssertionException: Expected is <System.String[1]>, actual is <System.Collections.Generic.List`1[System.String]> with 1 elements
Values differ at index [0]
Expected string length 8 but was 16. Strings differ at index 0.
Expected: "expected"
But was: "the real call..."
-----------^
*/
log.Received ().Log (LogLevel.Error, "expected");
/*
Expected to receive a call matching:
Log(Error, "expected")
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
Log(Error, *"the real call..."*)
*/