Validate (X)HTML and CSS in Rails functional tests

With a handy plugin, you can validate (X)HTML and CSS in your functional tests. Unfortunately, the plugin broke in Rails 2.3.x. Here's how to fix it. Jan Bromberger am 24. Dezember 2009

With a handy plugin written by Peter Donald, you can validate (X)HTML and CSS in your Rails functional tests.The plugin requires the XmlSimple gem. Make sure to install it:

sudo gem install xml-simple

Then, in your functional tests, you can write:

test "valid markup" do
  get :index
  assert_valid_markup
end

Unfortunately, the plugin broke somewhere around Rails 2.3.x, but I figured out how to fix it pretty fast. Here’s what made it work for me.

At the top of the file vendor/plugins/assert-valid-asset/lib/assert_valid_asset.rb insert:

require 'xmlsimple'

Replace

class Test::Unit::TestCase

with

class ActionController::TestCase

and replace

def process_with_auto_validate(action, parameters = nil, session = nil, flash = nil)
  response = process_without_auto_validate(action,parameters,session,flash)

with

def process_with_auto_validate(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
  response = process_without_auto_validate(action,parameters,session,flash,http_method)

This should get you up and running.

The original error messages were:

/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/module/aliasing.rb:33:in `alias_method': undefined method `process' for class `Test::Unit::TestCase' (NameError)
 from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/module/aliasing.rb:33:in `alias_method_chain'
 from /Users/janbromberger/Documents/Projects/zvg/vendor/plugins/assert-valid-asset/lib/assert_valid_asset.rb:38
 from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
 from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
 from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:158:in `require'
 from /Users/janbromberger/Documents/Projects/zvg/vendor/plugins/assert-valid-asset/init.rb:1:in `evaluate_init_rb'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/plugin.rb:146:in `evaluate_init_rb'
 from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/plugin.rb:142:in `evaluate_init_rb'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/plugin.rb:48:in `load'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/plugin/loader.rb:38:in `load_plugins'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/plugin/loader.rb:37:in `each'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/plugin/loader.rb:37:in `load_plugins'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:348:in `load_plugins'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:163:in `process'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in `send'
 from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in `run'
 from /Users/janbromberger/Documents/Projects/zvg/config/environment.rb:10
 from ./test_helper.rb:2:in `require'
 from ./test_helper.rb:2
 from functional/welcome_controller_test.rb:1:in `require'
 from functional/welcome_controller_test.rb:1

This is fixed by extending class ActionController::TestCase.

ArgumentError: wrong number of arguments (5 for 4)

This is fixed by appending the http_method = ‘GET’ parameter to process_with_auto_validate.

NameError: uninitialized constant ActionController::TestCase::XmlSimple

This is fixed by installing and requiring XmlSimple.

Tags: , , , , , , ,

Schreibe eine Antwort