chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

Is there anyway to make mock away repeated calls? As far as I know, one can simply signal a call by using calling the IgnoreCall() method. However, when the method is called twice, or n number of times, is there an effective way to ignore all of the calls?

Here's the code to illustrate my point. I have a simple class
    public class MyLogger
    {
        public int callNumber
        {
            get;
            set;
        }
        public MyLogger()
        {
            callNumber = 0;
        }

        public int IncreaseCallNumber()
        {
            callNumber++;
            return callNumber;
        }
        public void IncreaseCallNumber2()
        {
            callNumber++;
        }
    }


Here's my test class
        [Test]
        public void FutureInstanceVerification()
        {
            MyLogger logger = Isolate.Fake.Instance<MyLogger>(Members.CallOriginal);
            Isolate.Swap<MyLogger>().With(logger);
            Isolate.WhenCalled(() => logger.IncreaseCallNumber2()).IgnoreCall();

            MyLogger loggerReal = new MyLogger();
            loggerReal.IncreaseCallNumber2();

            Isolate.Verify.WasCalledWithAnyArguments(() => logger.IncreaseCallNumber2());
            Assert.AreEqual(0, loggerReal.callNumber);


            MyLogger loggerReal2 = new MyLogger();
            loggerReal2.IncreaseCallNumber2();

            Isolate.Verify.WasCalledWithAnyArguments(() => logger.IncreaseCallNumber2());
            Assert.AreEqual(0, loggerReal2.callNumber);
        }


My test fails at the last time, because the loggerReal2.callNumber is 1, even though the expected value is 0. The reason for this is that the second IncreaseCallNumber2() is not ignored.
________
Mercedes-Benz L-series truck specifications
asked by nsoonhui (59.1k points)

3 Answers

0 votes
Hi,

Actually, the default (and currently only supported) behavior for AAA methods is to repeat always. Your call should always be ignored.

What happened in your test, however, is that you tried to swap a future instance twice without specifying the Swap directive twice - the first instance was swapped when the call to 'new MyLogger()' was made, and on the second time that call was made you received a real object instead of the fake one.
In order to fix the test you need to either call Swap twice, or use the same instance of MyLogger that was swapped with the fake object - which version to use depends on what you would like to test.

Swapping twice looks like this:
MyLogger fake = Isolate.Fake.Instance<MyLogger>();
Isolate.Swap<MyLogger>().With(fake);
Isolate.Swap<MyLogger>().With(fake);
MyLogger real1 = new MyLogger();
MyLogger real2 = new MyLogger();
real1.IncreaseCallNumber2();
real2.IncreaseCallNumber2();
Assert.AreEqual(0, real1.callNumber);
Assert.AreEqual(0, real2.callNumber);


Using the same real instance should look like this:
MyLogger fake = Isolate.Fake.Instance<MyLogger>();
Isolate.Swap<MyLogger>().With(fake);
MyLogger real = new MyLogger();
real.IncreaseCallNumber2();
real.IncreaseCallNumber2();
Assert.AreEqual(0, real.callNumber);


Hope this helps,
Doron
Typemock Support
answered by doron (16.5k points)
0 votes
Hi,

Actually, the default (and currently only supported) behavior for AAA methods is to repeat always. Your call should always be ignored.

What happened in your test, however, is that you tried to swap a future instance twice without specifying the Swap directive twice - the first instance was swapped when the call to 'new MyLogger()' was made, and on the second time that call was made you received a real object instead of the fake one.
In order to fix the test you need to either call Swap twice, or use the same instance of MyLogger that was swapped with the fake object - which version to use depends on what you would like to test.

Swapping twice looks like this:
MyLogger fake = Isolate.Fake.Instance<MyLogger>();
Isolate.Swap<MyLogger>().With(fake);
Isolate.Swap<MyLogger>().With(fake);
MyLogger real1 = new MyLogger();
MyLogger real2 = new MyLogger();
real1.IncreaseCallNumber2();
real2.IncreaseCallNumber2();
Assert.AreEqual(0, real1.callNumber);
Assert.AreEqual(0, real2.callNumber);


Using the same real instance should look like this:
MyLogger fake = Isolate.Fake.Instance<MyLogger>();
Isolate.Swap<MyLogger>().With(fake);
MyLogger real = new MyLogger();
real.IncreaseCallNumber2();
real.IncreaseCallNumber2();
Assert.AreEqual(0, real.callNumber);


Hope this helps,
Doron
Typemock Support


Thanks Doron, but I afraid that both suggestions are not going to solve my problem.

Somehow when I created a fake object, all I want is that all future instances of this type is mocked. I don't know, at test writing time, how many such instances will be initiated, so I can't specify the number of fakes, or the number of fake calls I can call.

Any idea how to solve this problem?
________
Jimny
answered by nsoonhui (59.1k points)
0 votes
Hi,

The AAA syntax doesn't currently support the equivalent of reflective mock's MockAll. But it seems to me that's what you need, so go ahead and use it.

Thanks,
answered by gilz (14.5k points)
...