0

I have just started using NSubstitute. I mostly worked with Moq, and this is what i was doing:

// 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;
}

Output when unit test is run. Note that it tells what expected and real value is:

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 expression, String message, Object[] args)

I want to be able to use that with NSubstitute mocks, and here's my attempt at this:

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;
}

But this does not output NUnit assertion 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)

Am I missing something here?

Darius
  • 1,150
  • 10
  • 12

1 Answers1

0

Update after Log type clarification:

Other than using the incomplete plumbing mentioned below, for a Func<string> I would capture the arguments used and inspect them later.

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

Original answer:

We'd normally write this as a plain Received() assertion, which will show both the expected and actual values.

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

If you want to use an assertion library for more descriptive matches there is some incomplete plumbing in NSubstitute to allow this with a bit of work. There is an example in this issue.

David Tchepak
  • 9,826
  • 2
  • 56
  • 68
  • your example does not fit here, the second param is not a string, it is `Func`. And I guess this would not work, because NSubstitute won't evalue my second, it would simply say that received a non maatching call, where second param was a different func. `log.Received ().Log (LogLevel.Error, () => "expected");` – Darius Nov 22 '15 at 11:40