RJS Assertions

Posted by paul
Tuesday, April 25, 2006 16:21:00 GMT

For example, previously I was testing that when I entered the postcode for Buckingham Palace, the address would be filled for me:

1
2
3
4
5
6
7
8
9
10
def test_when_selecting_address_address_fields_populated
  xhr :post, :fill_address, :selected_address => 1

  assert_response :success
  assert_equal "text/javascript", 
    @response.headers["Content-Type"]
  assert /Buckingham Palace/.match(
    @response.body
  )
end

Unfortunately, this relies on regular expression matching, there’s no guarantee that the address that’s recovered actually replaces the text in the address first line text box. Any number of templating problems could cause it not to be rendered correctly, and I’d end up with a broken app that wouldn’t necessarily have tests failing.

At the time I wondered about whether it would be possible to write code for the RJS emitted script as I did with the rest of Rails’ functional tests—using `assert_tag` etc.

Well thanks to RJS assertions I can now. The previous test now looks as follows:

def test_when_selecting_address_address_fields_populated
  xhr :post, :fill_address, :selected_address => 1
end
assert_response :success
assert_equal "text/javascript; charset=UTF-8",
  @response.headers["Content-Type"]
assert_rjs_tag :tag => "input",
  :rjs => {:block => 'originAddressEntry' },
  :tag => "input",
  :attributes => {
    :id => "from_address_line_1",
    :value => "Buckingham Palace" 
    }

Much better, it’s far more communicative and the intent is clear. It’s also much more natural when writing test first now—“I want some RJS script that’s going to update this block and I’m looking for a tag that looks as follows…”

In the above test I’m asserting that I’m replacing the `originAddressEntry` division with the emitted RJS script, and that I’m looking for an `input` tag, with the id of `from_address_line_1` that will have it’s value set to Buckingham Palace.

I really encourage anyone doing any RJS work to use the additional assertions, and check out Brasten’s blog post on the plugin. There’s no reason not to test your RJS controller code thoroughly now!

Comments

Leave a response

  1. brastenApril 28, 2006 @ 05:10 PM
    Hey, thanks for the mention! I'm glad you found it useful! Idealy something like RJS Assertions will be built into Rails core and my plugin will be unneccessary. But until then...
  2. Your nameJuly 15, 2006 @ 02:31 PM
    Your name
  3. PillangószívSeptember 05, 2006 @ 06:30 PM
    nice, thank you!
  4. PillangószívSeptember 14, 2006 @ 06:21 PM
    wow:)
  5. PillangószívSeptember 21, 2006 @ 09:15 PM
    Thanks!
  6. PillangószívSeptember 24, 2006 @ 12:12 PM
    Its realy helpful, thank you!
  7. csikungDecember 06, 2006 @ 10:59 AM

    thank you the write up!

  8. luckyFebruary 25, 2008 @ 07:14 AM

    hi .i want help for test rjs code functionaltetsing. page.inserthtml :bottom, ‘meallist’, :partial => ‘mealplans’, :locals => {:mealplan => @duplicate_mealplan }

    how to write the test case for above rjs code tahnks in advance

  9. rnbFebruary 25, 2008 @ 04:50 PM

    Thanks for the nice read, keep up the interesting posts.

Comment