I have an interface ILogger
that has a LocalTime
property to which one could add time with AddToTime
. To mock it appropriately, I tried the following:
int ltime = 0;
var mlog = new Mock<ILogger>();
mlog.Setup(l => l.AddToTime(It.IsAny<int>())).Callback((int s) => {ltime +=s;});
mlog.Setup(l => l.LocalTime).Returns(ltime);
Although it compiles, i does not work: Probably because Returns
evaluates the expression ltime
at the beginning and so always returns 0
. Any suggestions how one could achieve the indented behaviour?
Edit: I need this to test it with a loop looking like this:
while (log.LocalTime < 1000)
{
log.AddToTime(500);
....
}
As I use the time for logging purposes, it cannot simply replaced by a loop with a local variable. If AddToTime
does nothing, the loop cannot be properly tested.