模拟对象何时进入重播状态? [英] When does a mock object enter the replay state?

查看:48
本文介绍了模拟对象何时进入重播状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行此代码的第二行时,Rhino Mocks会引发InvalidOperationException并显示消息当模拟对象处于重播状态时此操作无效"

When executing the second line of this code Rhino Mocks throws an InvalidOperationException with a message "This action is invalid when the mock object is in replay state"

var mockScanner = MockRepository.GenerateMock<PortScanner>(null);
        mockScanner.Expect((scanner => { scanner.Scan(null, null); }));

在调试器中逐步执行代码,可以看到调试器运行该类中定义的方法,并且在控制权离开该方法后立即发生异常.

Stepping through the code in a debugger one can see the debugger run the method defined in the class and directly after control leaves this method the exception occurs.

另一个测试中的类似代码确实可以正常工作

This similar code in another test does work without issue

var mockView = MockRepository.GenerateMock<IScanView>(null);
        mockView.Expect(view => { view.Close(); });
        var controller = new ScanController(mockView);
        controller.Exit();
        mockView.VerifyAllExpectations();

我能想到的唯一区别可能是这两个测试之间的任何后果:Exit是接口上的成员,而Scan是类上的虚拟成员

The only difference that I can think of that might be of any consequense between theese two tests is that Exit is a member on an interface while Scan is a virtual member on a class

我想念什么?

更新
进一步的研究表明,这与Rhino处理虚拟方法的方式有关.我现在将重点放在此处的文档研究上

Update
Further exploration has indicated that this is related to the way Rhino handles virtual methods. I am focusing mmy study of the documentation here now

推荐答案

发生此异常是因为Rhino Mocks没有必要的访问权限,无法正确模拟该类型.使用InternalsVisible授予内部对Rhino Mocks装配体的访问权限以解决该问题.

The exception was caused because Rhino Mocks did not have the required level of access to the type in order to mock it properly. Granting internal access to the Rhino Mocks assembly using InternalsVisibleTo solved the problem.

值得注意的是,这不会影响接口.我相信这是因为模拟框架需要覆盖在接口上没有任何接口的类上的实现.

It's noteworthy that this does not affect interfaces. I believe the reason for this is because the mocking framework needs to override the implementation on a class where there is none on an interface.

这篇关于模拟对象何时进入重播状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆