This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
Original Post: Ruby: Default method arguments to instance variables
Feed Title: Jay Fields Thoughts
Feed URL: http://feeds.feedburner.com/jayfields/mjKQ
Feed Description: Blog about Ruby, Agile, Testing and other topics related to software development.
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.