Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDD: Does it make sense to test that constructors set properties?

[TDD newbie]

I have a class Car with properties Color and Brand.

Does it make sense in TDD to test that the constructor sets those properties? Or do I wait with testing (and implementing) this until I need it?

So, do I build tests like these:

(c#)

public class CarTests
{
  public void Constructor_Should_Set_Color()
  {
    var car = new Car("Green", "Volvo");

    Assert.Equals(car.Color, "Green");
  }
}

Or do I wait until I have a use case scenario in which I must, for example, filter all green cars from a list that was built using the constructor, which will fail because the cars will have null as color ?

Does it really make sense to directly test constructors ? What about Equals() ?

like image 587
Thomas Stock Avatar asked Feb 19 '11 15:02

Thomas Stock


1 Answers

Ideally, I think, you wouldn't even have a Color property until you needed it. At that point, you might just use a setter, or you might add it to the constructor, or add a new constructor. The second option leaves you in a state with two (or more) constructors - and you may find that's helpful; in many circumstances (for both tests and "regular" code) you may not care what color a car is. And that is test-driving your design.

like image 125
Carl Manaster Avatar answered Oct 04 '22 16:10

Carl Manaster