Rhino Mocks 使用存根函数的输入参数来创建 return 值
Rhino Mocks use input parameters of stubbed function to create return value
我有两个接口:
interface ITimeframe
{
DateTime beginTime {get;}
DateTime endTime {get;}
}
interface ITimeframeFactory
{
ITimeframe Create(Datetime beginTime, DateTime endTime);
}
我的测试对象使用 ITimeframeFactory 来创建多个 ITimeframes。为了测试我的 TestObject,我给它一个模拟的 TimeframeFactory。因为我的测试对象处理创建的时间帧,模拟的 TimeframeFactory 需要创建 return 正确值的模拟时间帧。
Whosebug: 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();' 需要 return 值或抛出异常。”
似乎 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);
}
}
我有两个接口:
interface ITimeframe
{
DateTime beginTime {get;}
DateTime endTime {get;}
}
interface ITimeframeFactory
{
ITimeframe Create(Datetime beginTime, DateTime endTime);
}
我的测试对象使用 ITimeframeFactory 来创建多个 ITimeframes。为了测试我的 TestObject,我给它一个模拟的 TimeframeFactory。因为我的测试对象处理创建的时间帧,模拟的 TimeframeFactory 需要创建 return 正确值的模拟时间帧。
Whosebug: 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();' 需要 return 值或抛出异常。”
似乎 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);
}
}