Jquery Assertion for Rails

April 10, 2010

When doing functional testing of ajax actions it is nice to be able to test the format of the javascript returned to the browser.

When using rjs I was using the excellent arts testing library, now I have moved to raw jquery instead. The method below provides a short hand way to test simple jquery methods. it can be built upon for sure but this little regex is quite powerful.

  def assert_jquery(effect, id)
    assert_match(/\$\(['"]+##{id}['"]+\).#{effect}\(.*\);/, @response.body)
  end

Consider the jquery

  $("#announcement").hide();
  assert_jquery('hide', 'announcement')

This currently only works for id methods and when jquery isnt in compatibility mode. Also integration testing using something like env.js and harmony is a better way of javascript testing. Things like blue ridge actually integrate the env.js framework into a rails plugin and are a great solution if you have to do some real heavy lifting.

However, this method brings maximum results for minimum effort.