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?