Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDD, DDD and Encapsulation

Tags:

After several years of following the bad practice handed down from 'architects' at my place of work and thinking that there must be a better way, I've recently been reading up around TDD and DDD and I think the principles and practices would be a great fit for the complexity of the software we write.

However, many of the TDD samples I have seen call a method on the domain object and then test properties of the object to ensure the behaviour executed correctly.

On the other hand, several respected people in the industry (Greg Young most noticeably so with his talks on CQRS) advocate fully encapsulating each domain object by removing all the 'getters'.

My question therefore is: How does one test the functionality of a domain object if it is forbidden to retrieve its state?

I believe I am missing something fundamental so please feel free to call me an idiot and enlighten me - any guidance would be greatly appreciated.

like image 685
Justin Avatar asked Jul 17 '09 20:07

Justin


People also ask

What is the difference between TDD and DDD?

Ultimately though TDD is about creating code that can be tested. DDD is a far more abstract philosophy and set of design patterns that addresses how to design a large, scalable, and maintainable system.

What is DDD in testing?

DDD places an emphasis on minimizing the translation between business people (domain experts) and software developers. By having more understanding of the domain, we can embed that knowledge as tests in our code. This implies that our code must somehow match how the business works.


1 Answers

What you're describing is state verification wherein you Assert on the state of the domain object. There's a branch of TDD that is called behavior verification that utilizes Mock objects.

Behavior verification allows you to specify which methods should be called and if you want, which methods aren't called.

Look into this article by Martin Fowler for more details: Mocks Aren't Stubs.

like image 144
Gavin Miller Avatar answered Dec 02 '22 21:12

Gavin Miller