[ANN] assert{ 2.0 } 0.4.8 unit-tests Ajax using assert_rjs syntax + assert_xhtml

1 view
Skip to first unread message


Apr 5, 2009, 8:42:04 AM4/5/09
to rubyonra...@googlegroups.com, rspec...@rubyforge.org

assert{ 2.0 } now contains "alpha" replacements for these assertions from the
"arts" project:

assert_rjs_ :alert, 'This is an alert'
assert_rjs_ :call, 'foo', 'bar', /baz/ # matches foo("bar", "baz")
assert_rjs_ :remove, 'offending_div'
assert_rjs_ :replace, 'person_45', '<div>This replaces person_45</div>'
assert_rjs_ :replace_html, :label_7, /Top_Ranking/

Notice these assertions are not yet drop-in compatible with assert_rjs classic -
hence the trailing _. More replacements are naturally forthcoming.

The improvement is they use racc and rkelly, which form a real JavaScript lexer.
This solution is much better than Regexp (and better than certain lexers I have
tried in the past!). The assertion is more accurate, so it can use both Regexp
and assert_xhtml to pin down each important detail of generated JavaScript.

One common Ajax scenario revolves around sending Element.update() commands
containing the output of View partials. These can be very complex, so testing
them _before_ they get into a real web browser can be mission-critical. The
following test intercepts an Element.update("staff_access", "..."), where the
"..." contains an entire <form>. (Not my idea, but you must admit it gets the
job done!)

Everything inside the do...end blocks is assert_xhtml notation ( described here:
http://assert2.rubyforge.org/svn/README ). The assert_rjs_ intercepted the "..."
payload, interpreted it, and rendered it as HTML. This allows us to test that
secret payload just as freely as we would have tested HTML after a simple GET:

def test_deleting_a_staff_sets_active_flag_to_false_instead_of_destroying_it
aaron = staff(:aaron)
xhr :post, :xhr_delete_staff, :staff_id => aaron.id

assert_rjs_ :replace_html, :staff_list, /quentin/ do
form :name => :staff_access, :verbose! => true do
without!{ tr :id => "staff_#{ aaron.id }" }

assert{ Staff.find_by_id(aaron.id).active? == false }

The test shows :verbose! => true - use it to get a glimpse of your actual HTML
partial, without all the JavaScript delimiters cluttering it up.

The test also shows without!{} testing that aaron did indeed disappear from the

Both :replace and :replace_html use this syntax. If anyone needs any other
"arts" assertion here - or even a new kind of JS assertion - ping me and I will
add it.

Get assert_rjs_ with:

gem install racc rkelly nokogiri assert2

require 'assert2/rjs'

(Note that assert2 contains more than one reusable module, so it does not
automatically require every dependency. This lets you only install the gems you


Reply all
Reply to author
0 new messages