c# - 为什么 Rhino Mocks 会在 stub 上出错,但不会在与模拟完全相同的东西上出错?

标签 c# rhino-mocks

我是 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));

useage of SetupResult

usage of AAA

关于c# - 为什么 Rhino Mocks 会在 stub 上出错,但不会在与模拟完全相同的东西上出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3930519/

相关文章:

c# - 数据访问对象模式如何提高性能?

c# - 业务层或不在 MVC 中?

c# - c#中的模拟文件IO静态类

c# - 如何绕过 RhinoMocks 模拟中方法的执行?

unit-testing - 犀牛模拟 : "Verify" vs. "Assert"

c# - 如何从每次运行的相似项目中选择最后一个值?

c# - 尝试反序列化 JSON 时获取 NullReferenceException

c# - 在 C# 中保存包含所有样式和图像的 HTML 页面?

c# - 人们在使用 Rhino Mocks 的 ASP.NET MVC 中将哪些资源用于 TDD?

asp.net-mvc - 如何使用犀牛模拟在 MVC RC1 中 stub HttpSessionState?