Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it proper TDD practice to design your model before you write tests, or write tests that design your model?

I am building a DDD system and we have all the requirements on paper for the system already set. There is a disagreement on how we go about building our domain model that I need an opinion on.

My preference is to take the requirements and sketch out a basic domain model with the outline for the classes, their properties and behaviors and relationships on a whiteboard or visio. I then take that and start building unit tests that I use to build and test my model out piece by piece.

My coworkers seem to think this is not good TDD+DDD practice. They think you should not sketch out anything and starting building test, and design your model as you go through the "feel" of the tests.

Which one is considered "Proper" TDD technique when building a DDD model?

like image 958
Jason Avatar asked Oct 21 '10 13:10

Jason


People also ask

Is Test-Driven Development TDD an implementation practice or a design practice?

Although test-driven development is a design practice, to be effectively applied, it requires thinking of test code not only as a verification mechanism but as a dedicated design tool.

What is the recommended first step in Test-Driven Development?

Five steps of test-driven developmentRead, understand, and process the feature or bug request. Translate the requirement by writing a unit test. If you have hot reloading set up, the unit test will run and fail as no code is implemented yet. Write and implement the code that fulfills the requirement.

Which type's of tests do you typically write when doing TDD?

With developer TDD you write a single developer test, sometimes inaccurately referred to as a unit test, and then just enough production code to fulfill that test. The goal of developer TDD is to specify a detailed, executable design for your solution on a JIT basis. Developer TDD is often simply called TDD.


2 Answers

Like any kind of software engineering question, the answer is often "a bit of both".

You can't really write any tests unless you have some idea of what it is you're going to be testing, but you can also use your tests to influence your model design. Maybe this all happens inside your brain, or maybe you document the process, but (in my opinion) you're going to have to use both ideas in the end.

Personally, I would do a couple of use cases, take a stab at the domain model, then write tests for it and see what design problems the tests throw up. Rinse and repeat. This should all be done in collaboration with the rest of the team.

like image 76
Cameron Skinner Avatar answered Nov 09 '22 21:11

Cameron Skinner


I don't see how you can write a test without having any knowledge of the data items you're talking about.

 Thing myThing = getThingById( 87 );
 assert(Thing is Blue);

without some knowledge of the existence of a Thing or that it has an id and a colour you can't write the test. In other words, in writing even the simplest test you have in your head at least an embryonic Data Model. Sketching that informal model down may help you to reason about your tests.

The trick is to avoid getting sucked into detailed implementation before writing the tests, so I can understand why folks are giving the advice to do only Tests.

A question that interests me - given that the same tests could be satisfied by many different Data Models (think about denormalisation) at what stage do we start to build a better Data Model?

like image 28
djna Avatar answered Nov 09 '22 22:11

djna