Wednesday, April 11, 2007

Ruby: Default method arguments to instance variables

Several projects ago I worked with Brent Cryder. One night over dinner, he told me that on occasion he would pass instance variables to instance methods of the same class to improve testability. His assertion was that by passing in the variable you could test the method without depending on the state of the object (instance variables). Below is a contrived example to demonstrate the idea.
class Radio
def add_battery(battery)
@battery = battery
end
def on
@battery.on
end
end

class Battery
def on
@on = true
end
def on?
@on
end
end

require 'test/unit'
class RadioTest < Test::Unit::TestCase
def test_on_turns_battery_on
battery = Battery.new
radio = Radio.new
radio.add_battery(battery)
radio.on
assert_equal true, battery.on?
end
end
In the above test, you are required to add the battery before you can test that the on method delegates to the battery. The following example demonstrates how you can test the same thing without requiring a call to the add_battery method.
class Radio
..
def on(battery=@battery)
battery.on
end
end

class Battery
def on
@on = true
end
def on?
@on
end
end

require 'test/unit'
class RadioTest < Test::Unit::TestCase
def test_on_turns_battery_on
battery = Battery.new
radio = Radio.new
radio.on(battery)
assert_equal true, battery.on?
end
end
This is a fairly common idea; however, I like that Ruby allows me to specify a value for the test, but in the production code I would still call radio.on with no parameters and it would use the battery instance variable.
Post a Comment