我是 rhino Mocks 的新手,刚开始在这个项目中使用它。
我正在测试一些调用外部方法来获取“项目”的 IEnumerable 的代码,我已经为它提供了一个接口(interface),以便我可以 stub /模拟它。
在我的单元测试开始时,我测试了一些迭代(或调用 Count(),两者都会导致错误)IENumerable 的代码,我设置了一个 stub 实现
IJobProcess stub = MockRepository.Stub<IJobProcess>();
SetupResult.For(stub.CheckTeamMeetingInLastMonth(null)).IgnoreArguments().Return(true);
SetupResult.For(stub.GetOutstandingActions(null)).IgnoreArguments().Return(
new List<ProjectActionsDomain.DomainObjects.ProjectAction>()
);
然而,这会导致:
PmqccFormTests.GetFlagsReturnsIncompleteFlagWhenIncomplete : FailedSystem.InvalidOperationException : Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.
at Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
at Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at System.Linq.Enumerable.Count(IEnumerable`1 source)
at PmqccDomain.DomainObjects.PmqccForm.GetFlags() in PmqccForm.cs: line 387
at PmqccUnitTests.PmqccFormTests.GetFlagsReturnsIncompleteFlagWhenIncomplete() in PmqccFormTests.cs: line 426
但是,如果我将其更改为
IJobProcess mock = MockRepository.GenerateMock<IJobProcess>();
mock.Expect(x => x.GetOutstandingActions(null)).IgnoreArguments().Return(
new List<ProjectActionsDomain.DomainObjects.ProjectAction>());
mock.Expect(x => x.CheckTeamMeetingInLastMonth(null)).IgnoreArguments().Return(true);
它没有错误,我认为 stub 是一个基本上没有断言其结果的模拟?还是我错了? Rhino Mocks 有哪些不同之处来避免使用模拟时的错误?
最佳答案
正如 Jonny C 强调的那样,我认为 SetupResult 与 MockRepository.GenerarteStub 不兼容。
SetupResult 似乎与 Rhino 在使用 lamba 表达式之前使用的期望记录和播放功能一起使用。
首选方法是使用稍后介绍的 Arrange-Act-Assert 语法,但是在查看 Rhino API 时,很难说出 AAA 使用什么。
我所做的是只 stub 需要返回值的方法/属性,并断言方法在最后被调用。
所以应该做的是...
//Arrange
IJobProcess stub = MockRepository.GenerateStub<IJobProcess>();
stub.Stub(x => x.CheckTeamMeetingInLastMonth(null)).IgnoreArguments().Return(true);
stub.Stub(x => x.GetOutstandingActions(null)).IgnoreArguments().Return(
new List<ProjectActionsDomain.DomainObjects.ProjectAction>()
);
//Act
-- Perform SUT --
//Assert
stub.AssertWasCalled(x => x.CheckTeamMeetingInLastMonth(someExpectedValue));
关于c# - 为什么 Rhino Mocks 会在 stub 上出错,但不会在与模拟完全相同的东西上出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3930519/