How to trick Rhino.Mocks protected virtual members?

Moq allows developers to mock protected members. I searched for the same functionality in Rhino.Mocks , but could not find it.

Here is an example from Moq Quick Start to trick a 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 pursue something that does not come out.

+6
moq mocking rhino-mocks
source share
2 answers

I believe this feature 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 subclass your test class and manually create "mocked" protected methods.

+4
source share

We create the protected method as internal , and then set the internal objects for the unit testing and rhinoceros knockout project by adding the following lines to AssemblyInfo:

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

works for us

+2
source share

All Articles