views:

29

answers:

2

As far as I know, RM should mock anything that can be accessed by a derived class. Since a protected virtual member is accessible from a subclass, shouldn't it be mockable?

My understanding is that RM does not support mocking protected virtual members.

+1  A: 

As far as I know, RM should mock anything that can be accessed by a derived class.

No. The idea is that you should be able to mock the behavior of dependencies of the class under test. The word dependencies is used here in the same sense as in dependency injection.

In test driven development, one typically favors composition over inheritance. Dependencies are typically hidden behind interfaces.

My understanding is that RM does not support mocking protected virtual members.

That's correct. There are two concise and type-safe ways to specify which class member you're talking about when setting up behavior: by calling the member in a record mode, or with a lambda expression. Rhino Mocks supports both, but neither can work for protected members.

Wim Coenen
Let me rephrase my first statement to say "Internally, RM creates a mock class that subclasses the class under test (or an interface)". If this is true, my question was since protected members can be accessed via the subclass, why is it that RM cannot mock that member.
Dave Smith
@Dave: because there is no concise and type-safe way to reference such members in your unit test. Invoking a protected member in record mode isn't allowed by the compiler. Using a protected member in a lambda expression isn't allowed by the compiler. Strings aren't type-safe. Therefore, there is no good way to set up behavior for such methods.
Wim Coenen
A: 

If I want to mock a protected member I usually skip over Rhino Mocks and just subclass it with my own fake implementation.

Chris Missal