How to mock protected virtual members with Rhino.Mocks?

六月ゝ 毕业季﹏ 提交于 2019-12-10 13:09:22

问题


Moq allows developers to mock protected members. I was looking for the same functionality in Rhino.Mocks but fail to find it.

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.


回答1:


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.




回答2:


We create the protected method as internal, and then expose the internals to the unit testing project and rhino mocks by adding the following lines to your AssemblyInfo:

[assembly: InternalsVisibleTo("YourNamespace.TestProjectName")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

works a treat for us



来源:https://stackoverflow.com/questions/2581052/how-to-mock-protected-virtual-members-with-rhino-mocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!