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.

5 comments:

  1. Do you worry about modifying application code for the sole purpose of your tests?

    ReplyDelete
  2. @bryan - bah! Worry is for management :)

    ReplyDelete
  3. Anonymous6:49 AM

    Bryan, Yes, I always feel uneasy about modifying application code simply for testing. But, when it is necessary I'll do it without hesitation to ensure that I have a better tested application.

    I was recently taking with John Hume about this issue and he said that if I needed this technique I probably had a design issue in my class. He's probably right, and you should start there before using this trick. However, if you determine your design is sound, I think this is a nice trick to know.

    ReplyDelete
  4. Anonymous8:30 AM

    Are you testing what you think you're testing?

    You appear to be testing the behaviour of 'on(an_arg)', but your app actually uses 'on()'.

    This aspect of mock based testing is where I start to get uncomfortable; you're making assertions about both the 'front' and the 'back' of the method, and you just made your application that much more brittle because of it.

    ReplyDelete
  5. Anonymous8:55 AM

    I am testing what I think I'm testing; however, I've only shown one test. I would also have another test that ensured that the instance variable was used when 'on()' was called.

    This isn't a pattern to be used all the time. The example I gave tests the state of battery, but it could just as easily be a variable that's a Fixnum. And, using the class normally that might mean that the variable could only be positive, but I want to test my method with a negative number. You could use instance_variable_set, but that doesn't seem cleaner than this option since it's strongly relying on the internals of the class.

    ReplyDelete

Note: Only a member of this blog may post a comment.