问题描述
Moq 允许开发人员模拟受保护的成员.我在 Rhino.Mocks 中寻找相同的功能,但是找不到它.
Moq allows developers to mock protected members. I was looking for the same functionality in Rhino.Mocks but fail to find it.
以下是 Moq快速入门页中的示例,该示例如何模拟受保护的方法.
Here's an example from Moq Quick Start page how to mock protected method.
// at the top of the test fixture
using Moq.Protected()
// in the test
var mock = new Mock<CommandBase>();
mock.Protected()
.Setup<int>("Execute")
.Returns(5);
// if you need argument matching, you MUST use ItExpr rather than It
// planning on improving this for vNext
mock.Protected()
.Setup<string>("Execute",
ItExpr.IsAny<string>())
.Returns(true);
让我知道我是否正在追逐无法退出的东西.
Let me know if I'm chasing something that doesn't exit.
推荐答案
我相信Rhino Mocks中不存在此功能.
I believe this functionality does not exist in Rhino Mocks.
您为什么要嘲笑受保护的成员?为什么不仅仅测试整个班级呢?另外,您可以创建测试类的子类,并手动创建模拟"受保护的方法.
Why are you trying to mock protected members? Why not just test the class as a whole? Alternatively you can create a subclass of your test class and create "mocked" protected methods manually.
这篇关于如何使用Rhino.Mocks模拟受保护的虚拟成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!