Hi,
This is the intended behavior. WhenCalled() set a sequence behaviors, where the last behavior is the default one. WhenCalled() is intended to be used during the arrange step of the test. In the given example, the arrange and act steps are mixed.
Since this is not the first time users encounter this, I'll add a feature request for extending the API to support it. If you have suggestions on how it should be done (like API, behavior, etc.) you're welcome to post them.
Meanwhile you can use these two solutions:
1. If the number of expected calls is known during the arrange step, you can define a sequence in advance:
[TestMethod]
public void Test()
{
var fake = Isolate.Fake.Instance<ClassToIsolate>();
Isolate.WhenCalled(() => fake.RetournerValeur()).WillReturn(1);
Isolate.WhenCalled(() => fake.RetournerValeur()).WillReturn(1);
Isolate.WhenCalled(() => fake.RetournerValeur()).WillReturn(2);
Isolate.WhenCalled(() => fake.RetournerValeur()).WillReturn(2);
Isolate.WhenCalled(() => fake.RetournerValeur()).WillReturn(3);
Isolate.WhenCalled(() => fake.RetournerValeur()).WillReturn(3);
Assert.AreEqual(1, fake.RetournerValeur());
Assert.AreEqual(1, fake.RetournerValeur());
Assert.AreEqual(2, fake.RetournerValeur());
Assert.AreEqual(2, fake.RetournerValeur());
Assert.AreEqual(3, fake.RetournerValeur());
Assert.AreEqual(3, fake.RetournerValeur());
}
2. If you want to set the value using state during act step, you can define dynamic behavior to the method using DoInstead():
[TestMethod]
public void Test()
{
var fake = Isolate.Fake.Instance<ClassToIsolate>();
int value = 0;
Isolate.WhenCalled(() => fake.RetournerValeur()).DoInstead(context => value);
value = 1;
Assert.AreEqual(1, fake.RetournerValeur());
Assert.AreEqual(1, fake.RetournerValeur());
value = 2;
Assert.AreEqual(2, fake.RetournerValeur());
Assert.AreEqual(2, fake.RetournerValeur());
value = 3;
Assert.AreEqual(3, fake.RetournerValeur());
Assert.AreEqual(3, fake.RetournerValeur());
}
Regards,
Elisha
Typemock Support Team