Testing rails
I was working on creating functional tests for some of my code today, a task made ridiculously easy by rails. To add extra value, I added an assertion (from Scott Raymond) to validate my markup against the w3c online validator:
1 2 3 4 5 6 7 8 9 10 |
def assert_valid_markup(markup=@response.body) if ENV["TEST_MARKUP"] require "net/http" response = Net::HTTP.start("validator.w3.org") do |w3c| query = "fragment=" + CGI.escape(markup) + "&output=xml" w3c.post2("/check", query) end assert_equal "Valid", response["x-w3c-validator-status"] end end |
The ENV test means it isn’t run by default since it slows down my tests considerably, but I don’t want to move markup checks out of the functional tests because that’s where they belong. Next step is to validate locally, which I’ve heard you can do with HTML Tidy.
Another problem is testing code that relies on DateTime.now
, since this is a singleton call and not easily mockable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def pin_time time = DateTime.now DateTime.class_eval <<-EOS def self.now DateTime.parse("#{time}") end EOS yield time end # Usage pin_time do |test_time| assert_equal test_time, DateTime.now sleep 2 assert_equal test_time, DateTime.now end |
I haven’t found a neat way of resetting the behaviour of now
. Using load 'date.rb'
works but produces warnings for redefined constants. I couldn’t get either aliasing the original method, undefining the new one, or even just calling Date.now
to work.
UPDATE: Ah, how young I was. A better way to do this is to use a library like mocha