我在测试运行中第二次调用mock时出现问题,所以我将双调用移到了测试方法中。我有这个:
RefBundle mockIRefBundle = mocks.StrictMock<IRefBundle>();
Expect.Call(mockIRefBundle.MaxTrackItems).Return( 6 ).Repeat.Any();
int q = mockIRefBundle.MaxTrackItems;
int z = mockIRefBundle.MaxTrackItems;
当我进行第二次调用以设置“z”并且异常意味着该方法已被调用时,它失败了:
错误讯息:
System.InvalidOperationException: Previous method
'IRefBundle.get_MaxTrackItems();
'requires a return value or an exception to throw..
和Stack
Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation invocation,
...
第二次调用似乎不尊重Repeat.Any()
我错过了什么?
答案 0 :(得分:3)
您必须使用新语法:
RefBundle mockIRefBundle = MockRepository.GenerateMock<IRefBundle>();
mockIRefBundle.Expect(X => x.MaxTrackItems).Return(6).Repeat.Any();
int q = mockIRefBundle.MaxTrackItems;
int z = mockIRefBundle.MaxTrackItems;
或者你需要在开始使用你的模拟之前调用ReplayAll()
:
RefBundle mockIRefBundle = MockRepository.GenerateMock<IRefBundle>();
mockIRefBundle.Expect(X => x.MaxTrackItems).Return(6).Repeat.Any();
mocks.ReplayAll();
int q = mockIRefBundle.MaxTrackItems;
int z = mockIRefBundle.MaxTrackItems;