Hi,
It is possible to set several behaviors on the same method. In this case the last behavior set will be the default after the rest were used. For example:
[TestMethod]
[Isolated]
public void TwoConsecutiveCallsToWillReturn()
{
var fakeProduct = Isolate.Fake.Instance<Product>();
// Calling WhenCalled once cause the value to be the default return value
Isolate.WhenCalled(() => fakeProduct.Price).WillReturn(20.0f);
// this call cause the previous behavior to happen once and this behavior becomes the default returned value
Isolate.WhenCalled(() => fakeProduct.Price).WillReturn(15.0f);
// Calling Price the 1st time return 20
Assert.AreEqual(20.0f, fakeProduct.Price);
// fakeProduct.Price will return 15 for the rest of the calls
Assert.AreEqual(15.0f, fakeProduct.Price);
Assert.AreEqual(15.0f, fakeProduct.Price);
}
In the case you have described where you want to fake the first call and use the original method after you can set two behaviors, for example:
public class Counter
{
private int counter = 100;
public int CountAndIncrement()
{
return counter++;
}
}
[TestMethod]
public void CounterTest()
{
var instance = Isolate.Fake.Instance<Counter>(Members.CallOriginal);
Isolate.WhenCalled(()=>instance.CountAndIncrement()).WillReturn(0);
Isolate.WhenCalled(()=>instance.CountAndIncrement()).CallOriginal();
Assert.AreEqual(0, instance.CountAndIncrement());
Assert.AreEqual(100, instance.CountAndIncrement());
Assert.AreEqual(101, instance.CountAndIncrement());
}
Please note that in this test the Members.CallOriginal is used in the fake creation. If it's omitted then Isolate.WhenCalled(()=>instance.CountAndIncrement()).CallOriginal() will be ignored and the method will always return 0. We're investigating it to see if it's a bug.
Please let me know if it helps.
Best Regards,
Elisha
Typemock Support Team