Sohan's Blog

Things I'm Learning

Constructors Are Methods, Too!


Yes, if your constructor does something meaningful. This is just like another method and should be treated equally for unit testsing purpose. Here’s an example that should need unit test:

It looks simple, so testing it should be simple, too. I would only skip the default constuctors, as long as they don’t do anything interesting.

Comments

Alexander Beletsky
Sure, the constructors are methods too.. but they should not do anything more except internal initialization.

I had a little habbit to put additional logic inside constructor, to be possible to write something like

= new DoSomethingClass(x, y);

I'm getting rid of that also because of bad testability. Instead of that I write now:

var smth = new DoSomethingClass(x, y);
smth.DoIt();