Search code examples
unit-testingrhino-mocksrhinopartial-mocks

Why is my PartialMock method ignoring my expectation


I've got a base class that I'm using as a PartialMock as such

1  IContextManager contextManager = mocks.StrictMock<IContextManager>();
2  target = mocks.PartialMock<EnumerationServiceBase>(new object[] { contextManager });
3  Expect.Call(delegate { contextManager.RemoveContext(guid); });
4  mocks.ReplayAll();
5  actual = target.ReleaseOp(request);

target.ReleaseOp(request) has a call to the contextManager.RemoveContext method which I've set an expectation for on line 3, but I still get the following error

Rhino.Mocks.Exceptions.ExpectationViolationException: IContextManager.RemoveContext("e04c757b-8b70-4294-b133-94fd6b52ba04"); Expected #0, Actual #1.

This is the first test in which this hasn't worked (the other 45 or so are fine), but this is also the first one to use A) a partial mock, and B) a mocked method that returns void. Any ideas?


Solution

  • This is the first test in which this hasn't worked (the other 45 or so are fine), but this is also the first one to use A) a partial mock, and B) a mocked method that returns void. Any ideas?

    A) PartialMock means Rhino will intercept method calls only if it has an expectation on it. I think your usage here is fine.

    B) Void methods shouldn't be a problem either.

    Most likely, your problem is in your expectation:

    Expect.Call(delegate { contextManager.RemoveContext(guid); });
    

    The guid in your expectation needs to be the same instance as the guid passed in by target.

    Try this:

    Expect.Call(delegate { contextManager.RemoveContext(guid); }).IgnoreArguments();
    
    // you can also use fluent syntax like this:
    // contextManager.Expect(x => x.RemoveContext(guid)).IgnoreArguments();
    

    If it works, you can be fairly sure your test guid and actual guid used in your class don't match.