Testing Symfony 5 applications - Unittests: theory and reality

Article Index

Unittests: theory and reality

Doing unit tests by the book would mean to test a class stand-alone, without any other classes, i.e. by mocking all other needed classes. The theory behind this is, that a failing unit test points exactly to the one place in the unit where the behaviour has changed, and you can fix the problem really quickly. In reality, this will usually cause several tests including integration tests to fail, and it might even mean that only some integration tests fail because the unit test does not cover the case in which the cooperation between two classes fails.

I do not write unit tests by mocking all dependencies, because this generates a lot of extra work without any real benefit. That is why I for example use the real database in unit tests. I only use mock objects when I need to change the behaviour of an external class to generate a special state in my test class,  e.g. to generate some kind of special error state.