Why is my RhinoMocks PartialMock method call not being mocked?

1.2k views Asked by At

I'm creating a partial mock to test a protected helper method of a base class. I'm not too interested in the debate on whether or not the protected method should be there or if it should be an injected dependency because I'm really interested in seeing the process below work.

EnumerationServiceBase_Accessor is the VSTS 2010 generated private access object. Everything below works well except for the fact that line 17 does not effectively set up an expectation that intercepts the call to CreateNewContextResponse(request), which is the protected method that is being called by partialTarget.EnumerateOp(request) during playback. Instead the actual implementation on the base class is being called. What am I doing wrong here?

1  PrivateObject p = new PrivateObject(mocks.PartialMock<EnumerationServiceBase>(contextManager, requestValidator, configProvider, faultProvider, logger));
2  EnumerationServiceBase_Accessor partialTarget = mocks.PartialMock<EnumerationServiceBase_Accessor>(p);
3
4  EnumerateOpRequest request = new EnumerateOpRequest()
5  {
6   Enumerate = new Enumerate()
7   {
8       Item = new EnumerateNewContext()
9   }
10 };
11 
12 using (mocks.Record())
13 {
14   requestValidator.Expect(r => r.ValidateEndTo(request));
15   requestValidator.Expect(r => r.ValidateMaxElements(request, allowNulls: true));
16   partialTarget.Expect(t => t.EnumerateOp(request)).CallOriginalMethod(OriginalCallOptions.CreateExpectation); 
17   partialTarget.Expect(t => t.CreateNewContextResponse(request)).Return(null);
18   contextManager.Expect(t => t.RemoveExpiredContexts());
19 }
20
21 using (mocks.Playback())
22 {
23   partialTarget.EnumerateOp(request);
24 }

And this is EnumerateOp(request) as implemented in the EnumerationServiceBase.cs

1  public virtual EnumerateOpResponse EnumerateOp(EnumerateOpRequest request)
2  {
3      EnumerateOpResponse response = null;
4  
5      if (request.Enumerate.Item is EnumerateNewContext)
6      {
7          try
8          {
9              _contextManager.RemoveExpiredContexts();
10         }
11         catch (Exception ex)
12         {
13             _logger.Warn("We're not cleaning up contexts effectively.", ex);
14         }
15 
16         _requestValidator.ValidateEndTo(request);                
17         _requestValidator.ValidateMaxElements(request, allowNulls: true);
18         response = CreateNewContextResponse(request);
19     }
20     else if (request.Enumerate.Item is EnumerationContextType)
21     {
22         _requestValidator.ValidateMaxElements(request, allowNulls: false);
23         response = CreateEnumerationContextResponse(request);
24     }
25     else
26     {
27         throw _faultProvider.GetItemNotRecognizedFault("The Enumerate.Item value was not of type EnumerateNewContext or EnumerationContextType.");
28     }
29     return response;
30 }

EDIT: Removed unnecessary info.

1

There are 1 answers

0
Amittai Shapira On

The problem is that your CreateNewContextResponse is protected and You can't mock protected methods with Rhino Mocks.