[rspec-users] How To Drive Out AJAX Functionality in a Rails View

Phlip phlip2005 at gmail.com
Tue May 12 09:42:04 EDT 2009

Lee Longmore wrote:

> I am new to RSpec and have just started to drive out my first Rails view 
> using a spec. I have used the The RSpec Book (beta) to do the basic 
> stuff like testing for the presence of a field but I am unsure where to 
> start for driving out some AJAX functionality.

To test the onchange='' of the field, start by pulling in the Test::Unit 
versions of the assertions:

   Spec::Runner.configure do |c|
     c.include Test::Unit::Assertions

Now use assert_xhtml to grab your edit field and return it as a Nokogiri node. 
This line does it for a <a href='#'>All</a>:

     a = assert_xhtml{|x|  x.a 'All', :href => '#'  }
     assert{ a[:onclick] =~ /new Ajax.Request.*xhr_run_all/ }

.send_js_to is supposed to work in the second line, but I have not yet 
researched out how get rkelly to parse the Ajax.Request line.

The point of using rkelly inside send_js_to (aka assert_rjs_) is it's a real 
JavaScript lexer, not a Regexp, so when I get it working it will be very accurate!


More information about the rspec-users mailing list