Check number of calls

266 views
Skip to first unread message

Libor Blaheta

unread,
Oct 22, 2016, 7:14:31 AM10/22/16
to NSubstitute
Hello,
could you help me with the following newbie problem. My code under test may or may not call a specific function one or more times with different parameters.
I need to check that the function was called only once with specific paramaeter set and I need to fail the test if the function was called with different params or if it was called more than once. I used Recieved and Recieved(1) to check the function was called but a second call to the function did not trigger exception so the test passed. See the code below please

using System;
using NSubstitute;

namespace ConsoleApplication1
{
    public interface ICallbacks
    {
        void GetData(int a, int b);
    }

    public class TestClass
    {
        public static void DoSomething(ICallbacks callbacks)
        {
            callbacks.GetData(1, 2);
            callbacks.GetData(1, 3);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var c = Substitute.For<ICallbacks>();

            TestClass.DoSomething(c);

            c.Received().GetData(1, 2);
            // TODO GetData called twice how do I check for the second call? if GetData is called more than once make if fail....how?

            Console.Read();
        }
    }
}

Should I use RecievedCalls and check number of calls and if it is different than one then throw an exception or is there more obvious way of doing the check I have missed?
Thanks,
Libor

David Tchepak

unread,
Oct 22, 2016, 7:43:10 AM10/22/16
to nsubs...@googlegroups.com
Hi Libor,
NSubstitute isn't really designed for strict mocks like this, but I think we can achieve what you're after with two separate assertions: one on the call with the arguments you want, and the other on the total number of calls with any arguments.

    c.Received().GetData(1,2);
    c.ReceivedWithAnyArgs(1).GetData(0,0); // will throw if more than 1 call is received.

There are a few other ways of hacking in this behaviour, but I think this one will suit this case best.

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.

Libor Blaheta

unread,
Oct 22, 2016, 9:34:00 AM10/22/16
to NSubstitute
Yes, I used to use RhinoMocks and Strict mocks and lately I have moved to NSubstitute so the second call which did fail the test took me by surprise.
I will do it like you suggested, any chance of having some kind of "strict" call checking feature implemented right in NSubstitute?


Dne sobota 22. října 2016 13:43:10 UTC+2 David Tchepak napsal(a):
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.

Libor Blaheta

unread,
Oct 23, 2016, 7:10:49 AM10/23/16
to NSubstitute
ok, no need to answer :) found discussions about NSubstiture + strick mocks on google and here in the group

Dne sobota 22. října 2016 15:34:00 UTC+2 Libor Blaheta napsal(a):

raavi.moh...@gmail.com

unread,
Oct 24, 2016, 4:31:11 AM10/24/16
to NSubstitute
If my memory servers me right, the NSubtitute documentation do provide a way to check number of times the calls were made.

Libor Blaheta

unread,
Oct 24, 2016, 5:19:27 AM10/24/16
to NSubstitute
that seems to be one of the ways how to do what I need

Dne pondělí 24. října 2016 10:31:11 UTC+2 raavi.moh...@gmail.com napsal(a):
Reply all
Reply to author
Forward
0 new messages