# \[ANN\] assert{ 2.0 } 0.4.8 unit-tests Ajax using assert\_rjs syntax + assert\_xhtml

**URL:** <https://www.ruby-forum.com/t/ann-assert-2-0-0-4-8-unit-tests-ajax-using-assert-rjs-syntax-assert-xhtml/164764>\
**Category:** Rails\
**Created:** [April 5, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/ann-assert-2-0-0-4-8-unit-tests-ajax-using-assert-rjs-syntax-assert-xhtml/164764 "2009-04-05T14:43:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [April 5, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/ann-assert-2-0-0-4-8-unit-tests-ajax-using-assert-rjs-syntax-assert-xhtml/164764/1 "2009-04-05T14:43:09Z")

</div>

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’, ‘

This replaces  
person\_45
’  
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 . (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](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
