【问题标题】:NSubstitute does not print out NUnit assertionNSubstitute 不打印出 NUnit 断言
【发布时间】: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


    【解决方案1】:

    日志类型澄清后更新:

    除了使用incomplete plumbing mentioned below,对于Func&lt;string&gt;,我会捕获使用的参数并稍后检查它们。

    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..."
      -----------^
    */
    

    原答案:

    我们通常把它写成一个普通的Received() 断言,它将显示预期值和实际值。

    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..."*)
    */
    

    如果您想使用断言库进行更具描述性的匹配,则 NSubstitute 中存在一些不完整的管道,以便通过一些工作来实现这一点。有an example in this issue

    【讨论】:

    • 你的例子不适合这里,第二个参数不是字符串,它是Func&lt;string&gt;。而且我想这行不通,因为 NSubstitute 不会评估我的第二个,它只会说收到了一个不匹配的调用,其中第二个参数是不同的函数。 log.Received ().Log (LogLevel.Error, () =&gt; "expected");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多