What is the point of testing whether a mocked method has been called?

Viewed 152

Okay, so this might be a dangerous question to ask. I have been doing unit testing for a while, but for some reason, I woke up this morning and I asked myself this question.

Say I have an interface UserFactory, and it has the method CreateUser.

At some point I need to create a user right? So I create a test checking whether CreateUser was called for the UserFactory at the appropriate place.

Now, unit tests are pretty coupled to the actual code - which is fine. But maybe a bit too much? As in, the only way to break the test is by not calling the call to CreateUser. We are not checking its implementation etc. but just checking the interface has been called. But whoever does remove that call, would have a failing test and ultimately remove the verify statement from the step to verify the CreateUser was called.

I have seen this happen over and over again.

Could someone bring the light back to me and explain why is it beneficial to verify mocked objects' methods have been called? I can see why it may be useful to set them up, say CreateUser should return a dummy user for later part of the code, but in places where we are simply and only verify if they have been called is the part that got me.

Thanks!

4 Answers
Related