loading...

Unit Testing Mock Dependency Calls

lbabich profile image lbabich ・2 min read

So me and a colleague are busy going through the unit testing motions. Let me set up 2 scenario's:

Scenario 1

  • There are two services. Service one has a dependency on service two.
  • A method call in service one returns a value but also calls a method from service two, while passing values to that method (Service two does not return any value).

Scenario 2

  • There are two services. Service one has a dependency on service two.
  • A method call in service one returns a value but also calls a method from service two, while passing values to that method.
  • Service two method has a return value that directly affects the return result of service one.

Testing Scenarios

With scenario one I would argue that you would first stub the service two dependency. Then ensure that the value passed to that stub are what you would expect. The reason being you are ensuring that the outside dependency is passed what you expect and called, as you should have no direct knowledge of what it does with it.

However with scenario 2, my argument is you would stub service two dependency. However you would have no need to make sure that the method was called or what it was called with. You would however make sure it was called if it was down a certain branch of logic. However since you are directly determining the return value of the service two method you are testing your scenarios specific to that unit test. If you wanted to know what was being passed into that method and result then you are either looking at the need to refactor your code or you are needing to make a unit test and integration test scenario.

I would just like to get peoples opinions on this and how they approach it.

Posted on by:

Discussion

pic
Editor guide