Rhino Mock Partial Stub 必须是公共方法吗?
我已经使用 Rhino Mocks 编写了一些单元测试,我对结果很满意,除了我必须将底层 Web 服务公开为公共虚拟 (isUserInRoleWebService) 的事实,大概是因为这是我在部分模拟中的存根。 我通常使用反射来避免暴露私有方法,但这不适用于模拟对象。 有人解决这个问题吗?应该是常见场景。
[SetUp]
public void SetUp()
{
_mockRepository = new MockRepository();
_entitlementCache = _mockRepository.PartialMock<EntitlementCache>();
}
[Test]
// simple test to verify membership of a single role
public void Test_SingleRoleMember()
{
(new ReflectedObject(_entitlementCache)).InvokeInstanceMethod(
"setRoleHierachy",
new object[] { hierachy2Level }
);
using (_mockRepository.Record())
{
// I had to convert isUserInRoleWebService to public :-(
_entitlementCache.Stub(x => x.isUserInRoleWebService("user", "Role 1"))
.Repeat
.Once()
.Return(true);
}
using (_mockRepository.Playback())
{
bool res = _entitlementCache.IsUserInRole("user", "Role 1");
Assert.AreEqual(true, res, "user member of 'Role 1'");
}
}
[TearDown]
public void TearDown()
{
_mockRepository.ReplayAll();
_mockRepository.VerifyAll();
}
I've written some Unit Tests using Rhino Mocks and I'm happy with the results except for the fact that I have had to expose the underlying web service as public virtual (isUserInRoleWebService) presumably because this is my stub in the partial mock.
I usually use reflection to avoid exposing private methods but this will not work on the mocked object.
Has anyone got around this ? Must be a common scenario.
[SetUp]
public void SetUp()
{
_mockRepository = new MockRepository();
_entitlementCache = _mockRepository.PartialMock<EntitlementCache>();
}
[Test]
// simple test to verify membership of a single role
public void Test_SingleRoleMember()
{
(new ReflectedObject(_entitlementCache)).InvokeInstanceMethod(
"setRoleHierachy",
new object[] { hierachy2Level }
);
using (_mockRepository.Record())
{
// I had to convert isUserInRoleWebService to public :-(
_entitlementCache.Stub(x => x.isUserInRoleWebService("user", "Role 1"))
.Repeat
.Once()
.Return(true);
}
using (_mockRepository.Playback())
{
bool res = _entitlementCache.IsUserInRole("user", "Role 1");
Assert.AreEqual(true, res, "user member of 'Role 1'");
}
}
[TearDown]
public void TearDown()
{
_mockRepository.ReplayAll();
_mockRepository.VerifyAll();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用部分模拟来覆盖
受保护的内部虚拟
方法。请注意,您需要在测试项目的 AssemblyInfo.cs 中指定[InternalsVisibleTo("YourTestProject")]
。protectedinternal
(或protectedinternal
,如果您愿意的话)是protected
和internal
的联合。因此,internal
+[InternalsVisibleTo]
使该方法对您的测试项目可见,而protected
允许 Rhino 覆盖virtual
代码>方法。You can use partial mocks to override
protected internal virtual
methods. Note that you'll need to specify[InternalsVisibleTo("YourTestProject")]
in the project-under-test's AssemblyInfo.cs.protected internal
(orprotected internal
, if you prefer) is a union ofprotected
andinternal
. So,internal
+[InternalsVisibleTo]
makes the method visible to your test project, andprotected
allows Rhino to override thevirtual
method.通常,我不会测试或模拟私有方法。在这种情况下,似乎让 Web 服务本身作为缓存上的属性可用,然后对其进行模拟可能会更好。例如:
As a rule, I don't test or mock private methods. It seems like it might be better for you in this case to make the web service itself available as property on your cache, and then mock that. For example: