Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion assert{ 2.0 } 0.4.8 unit-tests Ajax using assert_rjs syntax + assert_xhtml
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Phlip  
View profile  
 More options Apr 5, 8:42 am
From: Phlip <phlip2...@gmail.com>
Date: Sun, 05 Apr 2009 05:42:04 -0700
Local: Sun, Apr 5 2009 8:42 am
Subject: [ANN] assert{ 2.0 } 0.4.8 unit-tests Ajax using assert_rjs syntax + assert_xhtml
Railsters:

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 }" }
       end
     end

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

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
form.

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
need...)

--
   Phlip


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google