I have a class that has different behavior on the second call to a method than the first, depending upon the interaction with dependent classes.
For the first call, I need to set up mocks of the dependencies. On the second call, I need to override the behavior. Some of the behavior is complex, using DoInstead to determine what the behavior of the mocked method should be. After the first call, I want to reset the behavior. I've found that I cannot seem to override behavior if I have already called DoInstead to establish behavior. A more-forceful reset via Isolate.CleanUp results in unexpected behavior -- mocked classes start calling into the real implementation, calls to Isolate.Verify complain that mock objects are not mock objects, etc.
Is there a way to do this?
[TestClass]
public class UnitTest1
{
public class Dependency
{
public int Method1() { return 0; }
}
public class Test
{
private Dependency _dep;
public Test(Dependency dep)
{
_dep = dep;
}
public int TestMethod()
{
return _dep.Method1();
}
}
[TestMethod]
[Isolated]
public void TestMethod1()
{
Dependency dep = Isolate.Fake.Instance<Dependency>();
Test target = new Test(dep);
Isolate.WhenCalled(() => dep.Method1()).DoInstead(context => 4);
int actual = target.TestMethod();
Assert.AreEqual(4, actual);
Isolate.WhenCalled(() => dep.Method1()).DoInstead(context => 6);
actual = target.TestMethod();
Assert.AreEqual(6, actual);
}
This test will fail as the second call to Method1 returns 4.