Let’s say we have the following, one class named
Implementor which has an
DoWork of the class, we call the interface’s method, but we also call another method, which comes by casting the dependency to a derived interface.
All, good, we go ahead and create a unit test project, adding the Moq package as well, in order to mock dependencies.
Code is as follows:
Now, both interfaces are simple, they all have a void method without any incoming parameters.
This interface innherits from
IParentInterface, but it has its own contract as well.
That poses a problem, because we want to make some unit tests that mock the dependency and verify the call to the underlying methods. How are we going to test this? If we don’t mock both we are going to receive an error, which is going to break our tests.
Solution to this problem is the .
As<T>() method of the Moq library. You can find more in the Github Wiki.
As<T> is used to implement multiple interfaces into a single mock.
Our setup in the
TestFixture (I am using NUnit as testing framework in the example, but you are free to use any of your choice) is like this:
I just create a mock instance of the
IParentInterface, the one that I am going to inject into the class.
Let’s see this in action in an actual test, I am going to test the
DoWork() method and verify that
ParentWork() as well as
ChildWork() are called once:
The unit test method follows the AAA pattern, as you can see by the comments, in order to maintain a clean visual representation of the unit test.
In the actual implementation now, first I setup the
ParentWork() method and mark it as Verifiable, which means that when I call the
Verify method on that mock object, I will be able to tell if that was called or not.
In order to test the derived interface method, I used the .
As<IChildInterface>, which essentially is implementing the
IChildInterface into the
_parentMock object. I mark this as verifiable, then I instantiate the
Implementor, passing the
In the Assert part, I verify that these methods were actually called. In order to check that the
ChildWork was called, I need to implement the
IChildInterface to the mock object again, calling the
Verify method next.
If your mock object can cast to something else, like a derived interface, and you need to test this implementation within your Act, the .As<T>() method of Moq library is your friend.
Code from this post can be found in this Github repository.