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
0 votes
Hi,

We are facing a typical issue while faking the behaviour of a static method. To illustrate our issue we are providing a sample scenario here

public class SampleClass
    {
        public int Sum(int a, int b)
        {
            return _Sum(a, b);
        }

        private static int _Sum(int a, int b)
        {
            return a + b;
        }
    }

/// <summary>
    /// Summary description for SampleClassTest
    /// </summary>
    [TestClass]
    public class SampleClassTest
    {
        [TestMethod]
        public void TestSum()
        {
            SampleClass fakeSampleClass = Isolate.Fake.Instance<SampleClass>(Members.CallOriginal);
            Isolate.NonPublic.WhenCalled(typeof(SampleClass), "_Sum").WillReturn(5);
            int result = fakeSampleClass.Sum(2, 3);
            Assert.AreEqual(result, 5);
        }

        [TestMethod]
        public void TestSumPrivate()
        {
            var result = Isolate.Invoke.Method(typeof(SampleClass), "_Sum", 5, 5);
            Assert.AreEqual(result, 10);
        }
    }


the second unit test method 'TestSumPrivate()' always fails & if the order of test execution is changed then both the test methods pass.

Please let us know what is that we are missing here.
asked by msmadhankumar (600 points)

1 Answer

0 votes
Hello,

What you are probably missing here is cleanup between tests. Cleanup is handled automatically by Isolator when you decorate the tests or test class with the [Isolated] attribute. Isolator will clean up behaviors and expectations between Isolated tests. Try modifying your test class declaration to this:
[TestClass, Isolated]
public class SampleClassTest


Please let me know if this works for you.

Doron
Typemock Support
answered by igal (5.7k points)
...