Should I make fun of all the dependencies in unit testing?

My class has a dependency that I mocked at my unit tests. I got an exception with a null reference in a place where it did not make much sense to me.

I just realized that this was because I had not set up my bullied addiction. This dependency is tested, but it does not connect to anything like a file system or data sources.

I just wanted to test my new code in this new class, but I think in this case itโ€™s better not to make fun of it at all.

Is this conclusion correct?

+6
source share
3 answers

Right. You must mock things that depend on something permanent or external in order to prevent checking dependency on something permanent or external.

If the dependency does not depend on anything permanent or external, the only advantage you get from ridicule is that the test will work correctly, even if the dependency is incorrect, but it is assumed that the layout works correctly. To do this, you need to either:

  • Write a layout that completely mimics addiction.

  • Write a layout that emulates the dependency for specific cases that will be used in the test.

The first option is frankly ridiculous - why should your layout be better than the original dependency? In the end, it would be safe to assume that much more effort was put into the original dependency than to the layout ...

The second option means that your layout knows the exact details of the implementation, otherwise you wonโ€™t know how the implementation uses the dependency, so you donโ€™t know how to simulate these specific uses. This means that the test does not serve one of the main goals of unit tests - checking the correct operation of the code after changes in the implementation.

The disadvantages of ridicule are too great, and the advantages are too small - especially considering that you can always run dependency tests to see if they work correctly ...

+2
source

It seems like it works in your case, but overall it's not that the only reason for a stub or layout is to get external data sources and such from your test hair. Other reasons include

  • the method can be slow on its own
  • it may be difficult to come up with the right parameters to switch to another class method so that it can return the value needed to test your method.
  • the method you are testing needs an instance of another class that changes a lot and you do not want your caller tests to be interrupted when the caller changes
  • the method you depend on is complex enough to require its own tests, fully testing your method without interruption or bullying, fully testing the methods it calls (as part of your methodโ€™s own tests), and this leads to duplication between tests of your class and tests of the methods that it calls.
  • you are doing TDD outside, and you havenโ€™t written a review yet, you just developed its interface!
+5
source

It depends: can you consider the dependency to really just be a private element of the implementation? If so, then mocking him only makes your test more fragile.

However, if this is a dependency that is actually injected into your SUT , then it should absolutely be replaced with a test double in unit test.

SUT should be interesting in your test. Everything else should be clear, boring and constant, so that your SUT works in optimal conditions (for the test scenario).

+1
source

All Articles