assert_ajax

I envision a WikiTest case with an assertion like this:

rjs.click 'link_node:test_uncle_wiggily'

rjs.assert_ajax /^\/wiki\/find_faults.*test_uncle_wiggily/ do
  rjs.assert '$("link_node:test_uncle_wiggily").style.backgroundColor == "Pink"'
end

rjs.assert_ajax takes a regular expression and a block. When the regular expression matches a URI in an Ajax command, after it completes, the block will call, and assert the effects of that Ajax event. We assert that it turned the inner Wiki's test node Pink. (Recall that the inner Wiki tests an imaginary web site that does not exist yet, and never will.)

Rendered in YAML, inside our test resource set_wiki_test_up(), that new test case looks like this:

      - test_WikiTestPage:
        - !omap
          - color: green
          - page: /wiki/WikiTestPage
          - script: |-
              rjs.click 'link_node:test_uncle_wiggily'
              rjs.assert_ajax /^\\/wiki\\/find_faults.*test_uncle_wiggily/ do
                rjs.assert '$("link_node:test_uncle_wiggily").style.backgroundColor
== "Pink"'
              end
              # something tests uncle wiggily

TODO better typography for that broken line

Ajax.Responders

assert_ajax depends on the Responders system inside prototype.js. Its simplest configuration looks like this:

Ajax.Responders.register({
  onComplete: function() {
    //  your code goes here
  }
});

The register function takes a JSON hash that maps keys to functions. We need to map onComplete to an anonymous function, and then we need to call unregister with the same map, to remove it from the list of handlers.

The Ruby code to wrap that JavaScript ...

Get Test Driven Ajax (on Rails) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.