REMARK :
- I use term mocking as a general term meaning all kind of test substitutes inluding spies, fakes, mock, stubs and all the rest.
- In writing my question I have only speak of "use cases" in clean architecture, but my question also concerns "Domain Services" in DDD.
Let's go :
I am currently trying to implement DDD and Clean Architecture principles in a new project. However, it's been 1 week that I have been stuck on ---> How to write my unit test for my use case
Here is my problem:
In Clean Architecture, when we create a use case (or a Domain service in DDD), it will depend in most cases on a certain number of entities + the rest (repository, api ...)
To write my unit test of my use case, I start with:
- Mock "the rest" of dependencies that interact with the outside (repositories, API ...)
- But next, what should I do with the entities dependencies in my unit test?
Here are the solutions I thought of :
- Solution 1: I'm injecting fake entities
- However, through my reading about unit test best practices, I understand that we should avoid creating mocks as much as possible because they are "Code Smells" and a good design should make it possible to do without them.
- Indeed, mocking my entities implies that I weaken my test. The test will be tightly coupled to my mocked entities.
- In addition, recreating the structure of my entities seems meaningless to me ...
* If my use case uses multiple entity methods: then I should have to recreate the return value of each of those methods.
* If the structure of my entities is complex I end up with complicated fakes to write, therefore my test loses a lot of reliability and there is a more chance that my fake is wrong, rather than my original entity)
* Even worse, if I use a factory, then I will have to make a fake of the factory -> and that fake will have to build a fake entity ...
- Solution 2: I don't mock entities.
- On the other hand, if I do not mock my entities, then I take the way in my opinion into integration tests: testing the interactions between the different entities ...
- Also as specified by some mocking supporters: If I don't mock my dependencies, then even if my tested unit is valid, the test will fail if my dependency causes a bug. This will cause a false alarm signal on my test ...
- Solution 3: Refactoring the production code
- By reading several articles some offer solutions to limit the coupling (Isolate the side effects from the rest of the logic, Isolate the logic from I/O, Use pure functions, Dependency injections, ...) But even by applying all this, a use case will irremediably need these entities to work ... Therefore it is not a solution.
But then how to do? Am I missing something?
How to do a GOOD unit test on a use case (or a service domain in DDD)? : how to manage the entities in a unit test in which the tested unit has entity depenencies ?
Exemple :
To illustrate my question, and to better understand your answers, here is a fictitious example of the problem:
Imagine that I have the following entity:
+class HorseEntity()
+constructor(name,age,health, ...)
+equipSaddle()
+makeShoeing()
+checkHealth()
I want to create a use case to add a horse to my stable:
+class addHorseUseCase()
+execute(requestDto,HorseRepo,HorseEntity, ...)
This usecase goes:
- Create a horse entity
- Check the health of the horse
- Shoe the horse
- Equip the horse with a saddle
- And add it to the stable.
When I create my test, what I should to do with the "HorseEntity" dependency?
Summary of my questions:
- How to write a GOOD unit test for a use case and how to handle entity dependencies in my test ?
- In general, what should I do with my entity dependencies in my unit tests?
- In this example above how to write a good unit test for "addHorseUseCase"?
Thank you for your future answers.
PS: I translate this question from French to English, if you do not understand the wording of one of my sentences do not hesitate to tell me so that I can edit it.