Rhino Mock Partial Stub 必须是公共方法吗?

发布于 2024-10-09 09:59:24 字数 1299 浏览 3 评论 0原文

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2024-10-16 09:59:24

您可以使用部分模拟来覆盖受保护的内部虚拟方法。请注意,您需要在测试项目的 AssemblyInfo.cs 中指定 [InternalsVisibleTo("YourTestProject")]

protectedinternal(或protectedinternal,如果您愿意的话)是protectedinternal的联合。因此,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 (or protected internal, if you prefer) is a union of protected and internal. So, internal+[InternalsVisibleTo] makes the method visible to your test project, and protected allows Rhino to override the virtual method.

滥情稳全场 2024-10-16 09:59:24

通常,我不会测试或模拟私有方法。在这种情况下,似乎让 Web 服务本身作为缓存上的属性可用,然后对其进行模拟可能会更好。例如:

IWebService service = ...
service.Expect(s => s.IsUserInRoleWebService("user", "Role 1")).Return(true);

EntitlementCache cache = ...
cache.Service = service;

bool result = cache.IsUserInRole("user", "Role 1");
Assert.IsTrue(result, "user member of 'Role 1'");

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:

IWebService service = ...
service.Expect(s => s.IsUserInRoleWebService("user", "Role 1")).Return(true);

EntitlementCache cache = ...
cache.Service = service;

bool result = cache.IsUserInRole("user", "Role 1");
Assert.IsTrue(result, "user member of 'Role 1'");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文