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
I have a unit test that fails if I try to mock a single method, because it seems like WhenCalled actually creates a mock, and in doing so, destroys the state of my object.

Here is a minimal example to demonstrate the problem:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;

namespace TestMe
{
    public class TestMe
    {
        private static TestMe _singletonInstance;

        public static TestMe Instance
        {
            get
            {
                if (_singletonInstance==null)
                {
                    _singletonInstance = new TestMe();
                }
                return _singletonInstance;
            }
        }

        public string SomeString { get; set; }

        public void DoStuff() { }
    }

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    [Isolated]
    public class UnitTest1
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void TestMethod1()
        {
            TestMe.Instance.SomeString = "foo";

            // The following mocks the DoStuff() method but does not change Instance
            TestMe instance = TestMe.Instance;
            Isolate.WhenCalled(() => instance.DoStuff()).IgnoreCall();

            // This assert succeeds: The value of SomeString was retained
            Assert.AreEqual("foo", TestMe.Instance.SomeString);
        }

        [TestMethod]
        public void TestMethod2()
        {
            TestMe.Instance.SomeString = "foo";

            // The following line changes the object returned by the Instance method
            // In doing so, any state on the object is lost.
            Isolate.WhenCalled(() => TestMe.Instance.DoStuff()).IgnoreCall();

            // This assert fails: The value of SomeString is null
            Assert.AreEqual("foo", TestMe.Instance.SomeString);
        }
    }
}


Is this the intended behavior of TypeMock? Or is a side-effect of chaining? Or is it a bug?
asked by MobyDisk (3.6k points)

1 Answer

0 votes
Hi,

You're correct, this is a bug. Isolator overwritten the value of the property inside the chain. Thank you for noticing.

We will fix this bug in an upcoming version.

Best Regards,
Elisha
Typemock Support Team
answered by Elisha (12k points)
...