Moq允许开发人员嘲笑受保护的成员。我在Rhino.Mocks寻找相同的功能,但未能找到它。如何用Rhino.Mocks模拟受保护的虚拟成员?
这里是一个例子,从Moq Quick Start页面如何模拟受保护的方法。
// 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);
让我知道,如果我追逐的东西,不退出。
你可能想看看这个链接http://geekswithblogs.net/MattRobertsBlog/archive/2008/12/16/how-to-make-a-quotprotectedquot-method-available-for-quotpartialquot-mocking-and-again.aspx – juharr 2011-03-04 20:11:47
这不是一个真正合适的解决方案 - 不得不改变你的原始类,以在其他类m中内部公开方法你是否正在改变你的设计以迎合测试。难闻的气味对我来说。 – 2011-11-21 08:34:36