Matt Roberts

Rails and .NET

  Home  |   Contact  |   Syndication    |   Login
  37 Posts | 0 Stories | 34 Comments | 35 Trackbacks


Article Categories




There are 2 cool things I want to share here...

Partial Mocking

This is a very cool and useful feature of RhinoMocks, which allows you to test part of a class, but have some of it act normally. Its explained very well here:

Making "protected" methods available for mocking.

Partial mocks are great, but what do you do when you need to "mock" out a method that isn't visible to your test class - i.e. its private or protected. Well, it turns out you can use a nifty little attribute called InternalsVisibleTo to make any internal methods visible to other assemblies (i.e. your testing assembly). So, in my code, I had a method called GetRecord, which was protected. I changed this to make it protected internal virtual, and added this to my AssemblyInfo.cs file:

   1: [assembly: InternalsVisibleTo("NUnitTests")] 
   2: [assembly: InternalsVisibleTo(RhinoMocks.NormalName)] 
   3: [assembly: InternalsVisibleTo(RhinoMocks.StrongName)] 

NUnitTests is the name of my assembly that contains the tests for my project. Once this is done and compiled, intellisense lets me mock that protected method. Just for completeness, here is the code that sets up my partial mock:

   1: var mocks = new MockRepository(); 
   2: var globalSearchServiceBase = mocks.PartialMock<ClassToMock>(); 
   3: Expect.Call(globalSearchServiceBase.GetRecord(1)).IgnoreArguments().Return(GetFakeData()); 
   4: mocks.ReplayAll(); 

Thats it. Hope that was at least marginally useful to someone :)

posted on Tuesday, December 16, 2008 11:00 AM


# re: How to make a "protected" method available for "partial" mocking using RhinoMocks 1/7/2009 11:25 AM Neil Mosafi
How does InternalsVisibleTo allow you to see protected or private methods? It's only internal ones

# re: How to make a "protected" method available for "partial" mocking using RhinoMocks 3/1/2009 1:15 PM elliott
internal protected means internal or protected, not protected and internal, so this allows you to access any method marked as internal regardless of weather it's marked as protected or not...


Private is private though, if it's marked private no one can see it. Shouldn't be unit testing private methods anyway :)

# re: How to make a "protected" method available for "partial" mocking using RhinoMocks 3/4/2009 4:16 PM Matt
The point is not whether or not private methods should be unit tested or not. No one here is disputing that. The point is; how do you setup a mock for a private method that your method under test will call?

Yes, yes, "Maybe you should rethink your design if that method is private." Well, what if I do not have control over that, because the private method is called in another assembly due to my class descending from one in said assembly and calling its base implementation at some point? I still need to set an expected result for the private method in order to avoid it actually being called!

# re: How to make a "protected" method available for "partial" mocking using RhinoMocks 1/23/2012 7:13 PM Nilesh Gule
Nice post. This was helpful for me and I modified one of my blogpost based on the inputs from this post.

Post A Comment