Moq allows mocking protected virtual members (see here). Is it possible to do the same in FakeItEasy?
Asked
Active
Viewed 5,334 times
2 Answers
36
It can be done, however it can not be done out of the box. The trick is to implement IFakeObjectCallRule and add it to your fake through Fake.GetFakeManager(foo).AddRule(myRule).
I'm thinking of implementing this feature though, it would be something like this:
A.CallTo(foo).WhereMethod(x => x.Name == "MyProtectedMethod").Returns("whatever");
The syntax is not quite refined yet though.
Edit The feature mentioned above is now implemented:
A.CallTo(foo).Where(x => x.Method.Name == "MyProtectedMethod").WithReturnType<int>().Returns(10);

Patrik Hägne
- 16,751
- 5
- 52
- 60
-
Awesome. Thanks a lot for the new feature. Now I can remove my own rule again. – Daniel Rose Apr 11 '11 at 10:30
-
I tried the code you provided above, but the method MyProtectedMethod is still being called. I would expect it to return the fake return value but not to call the original method. Am I doing something wrong? – stusherwin May 16 '11 at 13:13
-
2@stusherwin Perhaps the method isn't virtual? – Daniel Rose Mar 23 '12 at 07:26
19
In addition to Patrik's answer, I thought it would be relevant in this post to add a tip of how you could mock a protected property member:
A.CallTo(foo).Where(x => x.Method.Name == "get_MyProtectedProperty").WithReturnType<int>().Returns(10);
This is actually how reflection treats 'getter' methods of properties.
Hope it helps :)

Shay Ben-Sasson
- 188
- 2
- 5