Tech - November 2006 Archives

Metaprogramming Unit Tests

Here's how a lot of my tests look right after I've created my model. I got this pattern from Rick Olson's restful_authentication generator plugin.

class UserTest < Test::Unit:TestCase
  creator :first_name => "Joe", :last_name => "Schmo", :email => "joe.schmo@example.com"
  params  :first_name,          :last_name,            :email,                           :date_of_birth
end

It's a nice clear pattern, but I inevitably make typing errors, especially in the creation method. This problem is exacerbated when I'm writing essentially the same test for tons of different models. Here's how I'd love it to look: