I know I can do this:
IDateTimeFactory dtf = MockRepository.GenerateStub<IDateTimeFactory>(); dtf.Now = new DateTime(); DoStuff(dtf); // dtf.Now can be called arbitrary number of times, will always return the same value dtf.Now = new DateTime()+new TimeSpan(0,1,0); // 1 minute later DoStuff(dtf); //ditto from above
What if instead of IDateTimeFactory.Now being a property it is a method IDateTimeFactory.GetNow(), how do I do the same thing?
As per Judah’s suggestion below I have rewritten my SetDateTime helper method as follows:
private void SetDateTime(DateTime dt) { Expect.Call(_now_factory.GetNow()).Repeat.Any(); LastCall.Do((Func<DateTime>)delegate() { return dt; }); }
but it still throws ‘The result for ICurrentDateTimeFactory.GetNow(); has already been setup.’ errors.
Plus its still not going to work with a stub….
George,
Using your updated code, I got this to work:
FWIW, I don’t believe you can accomplish this using stubs; you need to use a mock instead.