Check if Action is called, more readably

60 views
Skip to first unread message

John B

unread,
May 25, 2018, 1:10:44 PM5/25/18
to NSubstitute
I'm trying to check if a fake Action has been called, or not. Here's how I'm doing it:

[Test]
public void Foo()
{
    var callback = Substitute.For<Action>();
    callback();
    Assert.That(callback.ReceivedCalls().Count(), Is.EqualTo(1));


    var callbackNotCalled = Substitute.For<Action>();
    Assert.That(callbackNotCalled.ReceivedCalls().Count(), Is.EqualTo(0));
}

Is there a more readable solution? I was hoping to use Received() and DidNotReceive().

David Tchepak

unread,
May 25, 2018, 8:44:52 PM5/25/18
to nsubs...@googlegroups.com
Hi John,

You should be able to do:

    callback.Received()();  // i.e. did it receive `callback()`

Or the slightly less confusing:

    callback.Received().Invoke();  // callback() is equivalent to callback.Invoke()

Regards,
David



--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute+unsubscribe@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at https://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

John B

unread,
May 26, 2018, 5:43:12 PM5/26/18
to NSubstitute
Both those give the error, "Cannot convert from 'void' to 'bool'".



On Friday, May 25, 2018 at 6:44:52 PM UTC-6, David Tchepak wrote:
Hi John,

You should be able to do:

    callback.Received()();  // i.e. did it receive `callback()`

Or the slightly less confusing:

    callback.Received().Invoke();  // callback() is equivalent to callback.Invoke()

Regards,
David


On Sat, May 26, 2018 at 3:10 AM, John B <john....@gmail.com> wrote:
I'm trying to check if a fake Action has been called, or not. Here's how I'm doing it:

[Test]
public void Foo()
{
    var callback = Substitute.For<Action>();
    callback();
    Assert.That(callback.ReceivedCalls().Count(), Is.EqualTo(1));


    var callbackNotCalled = Substitute.For<Action>();
    Assert.That(callbackNotCalled.ReceivedCalls().Count(), Is.EqualTo(0));
}

Is there a more readable solution? I was hoping to use Received() and DidNotReceive().

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.

David Tchepak

unread,
May 26, 2018, 10:34:39 PM5/26/18
to nsubs...@googlegroups.com
Maybe you are trying to assert on the result of `callback.Received()()`? This has a void return type so you can't pass it to `Assert.That(...)`. It's an assertion itself -- an additional assertion from your unit testing framework is not required.

This passes for me:

[Test]
public void Foo() {
    var callback = Substitute.For<Action>();
    callback(); // <- commenting out this will cause the test to fail
    callback.Received()();
}

Regards,
David


To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute+unsubscribe@googlegroups.com.

John B

unread,
May 28, 2018, 3:50:06 PM5/28/18
to nsubs...@googlegroups.com
Oh, that's what I need to know. Thanks!
Reply all
Reply to author
Forward
0 new messages