Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I doing something fundamentally wrong in my unit tests?

After reading an interesting article about unit testing behavior instead of state, I came to realize that my unit tests often are tightly coupled to my code because I am using mocks. I cannot image writing unit tests without mocks but the fact is that these mocks are coupling my unit test very much to my code because of the expect andReturn calls.

For example when I create a test that uses a mock, I record all calls to the specific mock and assign return values. Now when I change the implementation of the actual code for whatever reason, a lot of tests break because that call was not expected by the mock, forcing me to update the unit test also, and effectively forcing me to implement every change twice... This happens a lot.

Is this issue intrinsic to using mocks, and should I learn to live with it, or am I doing something fundamentally wrong? Please enlighten me :) Clear examples coming with the explanation are most welcome of course.

like image 884
nkr1pt Avatar asked Aug 30 '10 13:08

nkr1pt


2 Answers

when I create a test that uses a mock, I record all calls to the specific mock and assign return values

It sounds like you may be over-specifying expectations.

Try to build as little setup code as possible into your tests: stub (rather than expect) all behavior that doesn't pertain to the current test and only specify return values that are absolutely needed to make your test work.

This answer includes a concise example (as well as an alternative, more detailed explanation).

like image 60
Jeff Sternal Avatar answered Sep 27 '22 17:09

Jeff Sternal


My experience is to use mocks only at the bounderies of (sub)systems. If I have two classes that are strongly related I do not mock them appart but test them together. An example might be an composite and a visitor. If I test a concrete visitor I do not use a mock for the composite but create real composites. One might argue that this is not a unit test (depends on the definition of what is a unit). But that doesn't matter that much. What I try to achieve is:

  1. Write readable tests (tests without mocks are most of the time more easy to read).
  2. Test only a focused area of code (in the example the concreate visitor and the relevant part of the composite).
  3. Write fast tests (as long as I instantiate only a few classes, in the example the concrete composites, this is not a concern ... watch for transitive creations).

Only if I encounter the boundary of a subsystem I use mocks. Example: I have a composite that can render itself to a renderer I would mock out the renderer if I test the render logic of the composite.

Testing behavior instead of state looks promosing at first but in general I would test state as the resulting tests are easiear to maintain. Mocks are a cannon. Don't crack a nut with a sledgehammer.

like image 33
Arne Deutsch Avatar answered Sep 27 '22 17:09

Arne Deutsch