Checking received calls - several calls for the same method with different arguments

1,675 views
Skip to first unread message

Maxim Cherednik

unread,
Oct 9, 2014, 9:46:43 AM10/9/14
to nsubs...@googlegroups.com
Hi everyone

was trying to do something like this:

public interface ICalculator
    {
        void Do(Person person);
    }
 
    public class Person
    {
        public int Age { getset; }
    }



[Test]
        public async Task Test()
        {
            var calculator = Substitute.For<ICalculator>();
            var person = new Person
            {
                Age = 1
            };
 
            calculator.Do(person);
 
            person.Age = 2;
 
            calculator.Do(person);
            Received.InOrder(() =>
            {
                calculator.Received().Do(Arg.Is<Person>(x => x.Age == 1));
                calculator.Received().Do(Arg.Is<Person>(x => x.Age == 2));
            });
 
        }


But this fails. It seems that: calculator.Received() remembers just last call, which is x.Age == 2

I know that can track the number and the values of the arguments myself, but was looking for a built-in functionality like this.

I know that NSub stores the references to the arguments. That's why it fails here - I am using the same Person object. For instance, the next one is working just fine:

[Test]
        public void Tes()
        {
            var calculator = Substitute.For<ICalculator>();
            var person = new Person
            {
                Age = 1
            };
 
            calculator.Do(person);
 
            var person1 = new Person
            {
                Age = 2
            };
 
            calculator.Do(person1);
            calculator.Received(2).Do(Arg.Any<Person>());
            Received.InOrder(() =>
            {
                calculator.Received().Do(Arg.Is<Person>(x => x.Age == 1));
                calculator.Received().Do(Arg.Is<Person>(x => x.Age == 2));
            });
 
        }

So I guess it's not really possible, cause NSub does the check as a last step against stored arguments. Or is there a way?



David Tchepak

unread,
Oct 9, 2014, 8:51:32 PM10/9/14
to nsubs...@googlegroups.com
No built in way - we need to track varying properties ourselves. There is some support for doing that tracking.

e.g.
var agesUsedForCalculations = new List<int>();
calculator.WhenForAnyArgs(x => x.Do(null)).Do(x => agesUsedForCalculations.Add(x.Arg<Person>().Age));
// or: calculator.Do(Arg.Do<Person>(x => agesUsedForCalculations.Add(x.Age));
...
agesUsedForCalculations.ShouldBe(new [] { 1, 2 });

But in terms of `Received()`, NSubstitute only considers the references used for arguments.

Hope this helps.
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...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at http://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages