我有一TimeMachine
门课,它为我提供了当前的日期/时间值。该类如下所示:
public class TimeMachine
{
public virtual DateTime GetCurrentDateTime(){ return DateTime.Now; };
public virtual DateTime GetCurrentDate(){ return GetCurrentDateTime().Date; };
public virtual TimeSpan GetCurrentTime(){ return GetCurrentDateTime().TimeOfDay; };
}
我想TimeMachine
在我的测试中以这样的方式使用存根,即我只存根该GetCurrentDateTime
方法并让其他两种方法使用存根GetCurrentDateTime
方法,这样我就不必存根所有三种方法。我试着写这样的测试:
var time = MockRepository.GenerateStub<TimeMachine>();
time.Stub(x => x.GetCurrentDateTime())
.Return(new DateTime(2009, 11, 25, 12, 0, 0));
Assert.AreEqual(new DateTime(2009, 11, 25), time.GetCurrentDate());
但是测试失败了。GetCurrentDate
返回default(DateTime)
而不是在内部使用GetCurrentDateTime
存根。
有什么方法可以用来实现这种行为,或者它只是我目前没有掌握的 RhinoMocks 的一些基本概念特征?我知道我可以摆脱这两个GetDate
/Time
方法并内联.Date
/.TimeOfDay
用法,但我想了解这是否可能。