我有以下内容
IdleClass idleSend = MockRepository.GenerateMock<IdleClass >();
SpeedClass speedSend = MockRepository.GenerateMock<SpeeClass >();
idleSend.Expect(x => x.IsUnitInSystem(networkID)).Return(true).Repeat.Any();
speedSend.Expect(x => x.IsUnitInSystem(networkID)).Return(true).Repeat.Any();
原始的 IsUnitInSystem(networkID) 函数(在 IdleClass 和 SpeedClass 的子类中)如下:
public bool IsUnitInSystem(string networkID)
{
DateTime outVal = default(DateTime);
return m_list1.DoesListContainUnit(networkID) ||
m_list2.TryGetValue(networkID, out outVal) ||
m_list3.TryGetValue(networkID, out outVal) ||
m_list4.TryGetValue(networkID, out outVal) ||
m_list5.TryGetValue(networkID, out outVal);
}
我正在 IsUnitInSystem 函数的返回行上恢复 System.NullReferenceException。
堆栈跟踪:
IdleClass.IsUnitInSystem(String networkID)
PendingTest.<ResendClassTest>b__1b(IdleClass x)
RhinoMocksExtension.Expect[T,R](T mock, Function'2 action)
PendingTest.ResendClassTest()
我不明白的是,当我为函数指定了 .Expect 功能时,程序为什么以及如何进入函数?
最佳答案
您尝试模拟的方法必须能够由模拟对象实现,这意味着如果您模拟的是具体类,则必须将其标记为虚拟。
查看以下帖子了解更多信息:Rhino Mocks stubs and mocks are only good for interfaces?
关于c# - 为什么我在模拟测试对象上得到空引用异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13650151/