Data driving Ruby tests with meta-programming

Data driving ruby tests with meta-programming is one of the more elegant examples of meta-programming in my opinion. Understanding that class definitions are active, and you can add methods in a loop is very powerful.

class LocationTest < ActiveSupport::TestCase
  
  def setup
    @location = Location.new
  end
  
  {:empty => '', :nil => nil}.each do |key, value|
    test "address can't be " + key  do
      @location.address = value
    
      assert !@location.valid?
    end
  end
end

NUnit has the ability to do the same thing, but attributes and reflection just aren’t as elegant.