Tuesday, July 26, 2011

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:
class Weather
attr_accessor :temperature, :humidity
def initialize another_weather
self.temperature = another_weather.temperature
self.humidity = another_weather.humidity
end
end
view raw weather.rb hosted with ❤ by GitHub

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.