我有两个界面:
interface ITimeframe
{
DateTime beginTime {get;}
DateTime endTime {get;}
}
interface ITimeframeFactory
{
ITimeframe Create(Datetime beginTime, DateTime endTime);
}
我的测试对象使用ITimeframeFactory创建多个ITimeframe。为了测试我的TestObject,我给它一个模拟的TimeframeFactory。因为我的测试对象处理了创建的时间范围,所以模拟的TimeframeFactory需要创建返回正确值的模拟时间范围。
StackOverflow: How to stub a function似乎在暗示这个方向:使用WhenCalled:
var mockRepository = new MockRepository();
ITimeframeFactory mockedTimeframeFactory = mockRepository.Stub<ITimeFrameFactory>();
// when the mocked TimeframeFactory is asked to Create a timeframe,
// let it return a new mockedTimeFrame that returns the proper values for BeginTime and EndTime
using (mockRepository.Record())
{
mockedTimeframeFactory.Stub( (factory) => factory.Create(
Arg<DateTime>.Is.Anything,
Arg<DateTime.Is.Anything))
.WhenCalled( (call) =>
{
DateTime beginTime = (DateTime)call.Arguments[0];
DateTime endTime = (DateTime)call.Arguments[1];
// mock a new ITimeframe;
// this ITimeframe should return beginTime and endtime:
ITimeframe createdTimeframe = mockRepository.Stub<ITimeframe>();
createdTimeframe.Stub((timeframe) => timeframe.BeginTime).Return(beginTime);
createdTimeframe.Stub((timeframe) => timeframe.EndTime).Return(endTime);
call.ReturnValue = createdTimeframe;
});
}
用法:
using (mockRepository.Playback())
{
DateTime beginTime = new DateTime(2020, 1, 1);
DateTime endTime = new DateTime(2019, 2, 2);
ITimeframe createdTimeframe = mockedFactory.Create(beginTime, endTime);
Assert.IsNotNull(createdTimeframe);
DateTime mockedBeginTime = createdTimeframe.BeginTime;
DateTime mockedEndTime = createdTimeframe.EndTime;
Assert.AreEqual(beginTime, mockedBeginTime);
Assert.AreEqual(endTime, mockedEndTime);
}
在mockedFactory.Create(...)期间,我看到WhenCalled
已处理。在WhenCalled期间,它具有正确的beginTime和endTime。
[call.ReturnValue
也有效,因为在调用Create createdTimeframe
之后不为空。
但是,当我要检查createdTimeframe的属性时,出现异常:
System.InvalidOperationException:'先前的方法'ITimeframe.get_BeginTime();'需要返回值或引发异常。'
似乎createdTimeframe.Stub的存根无效?
如何解决这个问题?
解决方案比我想象的要容易:在回放过程中,当创建模拟时间表时,该模拟时间表仍处于记录状态。
我要做的就是通过调用Replay()将其设置为重放状态。
这样,我可以创建几个不同的时间范围,如下面的代码所示。
using (this.mockRepository.Playback())
{
List<ITimeframe> createdTimeframes = new List<ITimeframe>();
DateTime beginTime = new DateTime(2020, 1, 1);
DateTime endTime = new DateTime(2019, 2, 2);
for (int i = 0; i < 10; ++i)
{
Timeframe createdTimeframe = this.mockedFactory.Create(beginTime, endTime);
// This should be a new Timeframe, not returned before
Assert.IsNotNull(createdTimeframe);
Assert.IsFalse(createdTimeframes.Contains(createdTimeframe));
createdTimeframes.Add(createdTimeframe);
// before using the stubs of this timeframe set in in playback:
createdTimeframe.Replay();
DateTime mockedBeginTime = createdTimeframe.BeginTime;
DateTime mockedEndTime = createdTimeframe.EndTime;
Assert.AreEqual(beginTime, mockedBeginTime);
Assert.AreEqual(endTime, mockedEndTime);
// change beginTime and endTime to create a new timeframe
beginTime = beginTime.AddMonths(i);
endTime = endTime.AddMonths(-i);
}
}