Hi,
The reason the original test seems to return wrong values due to a mix between Arrange & Act sections. As a solution, you can set a sequence once that precedes the Act, for example:
Isolate.WhenCalled(() => DateTime.UtcNow).WillReturn(DateTime.Parse("2000-01-01 08:00:00"));
Isolate.WhenCalled(() => DateTime.UtcNow).WillReturn(DateTime.Parse("2000-01-01 08:00:40"));
Isolate.WhenCalled(() => DateTime.UtcNow).WillReturn(DateTime.Parse("2000-01-01 08:01:20"));
var d1 = DateTime.UtcNow;
var d2 = DateTime.UtcNow;
var d3 = DateTime.UtcNow;
Assert.AreEqual(DateTime.Parse("2000-01-01 08:00:00"), d1);
Assert.AreEqual(DateTime.Parse("2000-01-01 08:00:40"), d2);
Assert.AreEqual(DateTime.Parse("2000-01-01 08:01:20"), d3);
If it is necessary to control the behavior during the act, you can do it using the DoInstead feature. In order to do so, you can set a local variable in the test that hols the current required value and point to it with the DoInstead. During the execution you can change the value of that variable and control the current behavior. For example:
var f1 = DateTime.Parse("2000-01-01 08:00:00");
var f2 = DateTime.Parse("2000-01-01 08:00:40");
var f3 = DateTime.Parse("2000-01-01 08:01:20");
DateTime fakeTime = new DateTime();
Isolate.WhenCalled(() => DateTime.UtcNow).DoInstead(c => fakeTime);
fakeTime = f1;
var d1 = DateTime.UtcNow;
fakeTime = f2;
var d2 = DateTime.UtcNow;
fakeTime = f3;
var d3 = DateTime.UtcNow;
Assert.AreEqual(DateTime.Parse("2000-01-01 08:00:00"), d1);
Assert.AreEqual(DateTime.Parse("2000-01-01 08:00:40"), d2);
Assert.AreEqual(DateTime.Parse("2000-01-01 08:01:20"), d3);
Please let me know if it resolves the issue.
Regards,
Alex,
Typemock Support