Thursday, February 25, 2010

Return from sequence in FakeItEasy

I added a nifty little feature in FakeItEasy today, not 100% sure about the naming though so if you have any ideas pleas tell me. The idea is that when you configure a call you can specify several values and each time a call is made to the configured member (property or method) the next value from the collection will be returned. Here’s what it looks like in action:

public interface ICurrentTimeProvider
{
    DateTime CurrentTime { get; }
}
 
[Test]
public void Example()
{
    var timeProvider = A.Fake<ICurrentTimeProvider>();
 
    A.CallTo(() => timeProvider.CurrentTime).ReturnsNextFromSequence(
        DateTime.Parse("2000-01-01 01:00"),
        DateTime.Parse("2000-01-01 01:01"),
        DateTime.Parse("2000-01-01 01:02"));
 
    Console.WriteLine(timeProvider.CurrentTime); // Writes 2000-01-01 01:00
    Console.WriteLine(timeProvider.CurrentTime); // Writes 2000-01-01 01:01
    Console.WriteLine(timeProvider.CurrentTime); // Writes 2000-01-01 01:02
}