我知道我可以这样做:
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
如果不是IDateTimeFactory.Now是一个属性,它是一个方法IDateTimeFactory.GetNow(),我该怎么做同样的事情呢?
按照Judah的建议,我已经重写了我的SetDateTime辅助方法,如下所示:
private void SetDateTime(DateTime dt) {
Expect.Call(_now_factory.GetNow()).Repeat.Any();
LastCall.Do((Func<DateTime>)delegate() { return dt; });
}
但它仍然会抛出“ICurrentDateTimeFactory.GetNow();的结果已经设置。”
发布于 2018-05-30 12:21:20
根据以前使用Do()的答案,如果在Rhino Mocks中使用AAA,则可以使用稍微更清洁(IMO)的方式。
[Test]
public void TestDoStuff()
{
var now = DateTime.Now;
var dtf = MockRepository.GenerateStub<IDateTimeFactory>();
dtf
.Stub(x => x.GetNow())
.Return(default(DateTime)) //need to set a dummy return value
.WhenCalled(x => x.ReturnValue = now); //close over the now variable
DoStuff(dtf); // dtf.Now can be called arbitrary number of times, will always return the same value
now = now + new TimeSpan(0, 1, 0); // 1 minute later
DoStuff(dtf); //ditto from above
}
private void DoStuff(IDateTimeFactory dtf)
{
Console.WriteLine(dtf.GetNow());
}
https://stackoverflow.com/questions/-100000138
复制相似问题