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.
Do you worry about modifying application code for the sole purpose of your tests?
ReplyDelete@bryan - bah! Worry is for management :)
ReplyDeleteBryan, 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.
ReplyDeleteI 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.
Are you testing what you think you're testing?
ReplyDeleteYou 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.
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.
ReplyDeleteThis 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.