[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() ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With