Wednesday, 2 October 2013

Overriding protected methods in Moq

Overriding protected methods in Moq

I have a class which implements an abstract class. The abstract class has
a protected virtual method as shown:
protected virtual void OnAction(string action, object result);
The class implementing the abstract class has a method which when
completed calls base.OnAction.
I am trying to override the behaviour of the implementation of OnAction in
my unit tests as I am trying to unit test only, not do integration tests.
My Moq call is setup as follows:
mock.Protected().Setup("OnAction", ItExpr.IsAny<string>,
ItExpr.IsAny<object>());
Breakpointing my base abstract classes OnAction method confirms it is
still being called.
I'm essentially wanting Moq to do the equivalent in it's emitted class:
public override OnAction(string action, object result) { // do nothing }

No comments:

Post a Comment