Freigeben über


Feeling awkward when writing tests?

I recently observed a discussion where one person hesitated to add new dependencies to one class since it meant all calls to the constructor had to be changed. At first glance this only looks as if the person was very strict about constructor dependency injection. But there is nothing wrong in having a constructor that uses all the default dependencies in your production code.

Or maybe the person felt this about the test code where he couldn't use the default constructor. But in this case we're talking about code duplication in some form. You probably want a common function to set up your test environment. Or maybe the person just thought it was ugly adding yet another dependency creating a constructor parameter list marathon.

Regardless of which the real reason for feeling it's awkward to add new dependencies the annoying answer is: "you're probably doing something wrong". And this applies not only to dependencies but to all kinds of behavior/test writing. As soon as something feels awkward, back-wards or just wrong to write it is a huge warning bell for me. When this happens I try to stop and think about what I'm doing questioning myself to see if there is a simpler way to do what I'm about to do. Sometimes something is wrong in the design and sometimes there is something more simple to test instead of the thing I thought of first.