ddd-forum
ddd-forum copied to clipboard
[Question] How to test error branches of application services layer?
Thanks alot for this repo + white-label @stemmlerjs , I've learned a ton of how to deal with complexity in enterprise applications.
After applying some of the principles in a personal project, I was wondering how to test the error branches of the app service layer when it depends on a validation/business rule in the domain layer. For example in the createPost application service:
https://github.com/stemmlerjs/ddd-forum/blob/f78ebf0e69cfce8b91db34d04c3a6427bee666f1/src/modules/forum/useCases/post/createPost/CreatePost.ts#L50-L54
Here's the resulting business rules in the domain layer for a PostTitle
:
https://github.com/stemmlerjs/ddd-forum/blob/f78ebf0e69cfce8b91db34d04c3a6427bee666f1/src/modules/forum/domain/postTitle.ts#L22-L41
In order to test that the application service returns the correct result, we must write an integration test as the if statement depends on an broken business rule/validation error occurring in the domain layer. This results in combinatorial explosion where the branch in the domain model is tested twice (once in the domain layer with a unit test, and once in each application service that depends on this rule being broken).
Is there a suggest way to test the application service error branches in isolation? The only way that it seems possible is stubbing the static create method for each domain entity in the test setup to return the correct Result
class, which seems less than optimal.
Hi @StevePavlin! Thank you for the question and your thoughtful message. I mentioned over here the background behind testing in this repo and plans for the very near future; I'm spending some time on this and I'll get back to you.
Hi @StevePavlin. I mocked the domain objects in the unit tests for the application services. But the I realised, when I change something in the domain layer the tests for the application services will still work. But the change in the domain layer can actually break the behaviour of the application services. Now I don't mock the domain objects anymore and the tests for every layer more far away from the domain layer has now the character of an integration test. Wenn I change something in the domain layer the other tests will brake. I consider this as a good behaviour, in my opinion.