On May 12, 2009, at 8:36 AM, Lee Longmore wrote:

I am new to RSpec and have just started to drive out my first Rails view using a spec. I have used the The RSpec Book (beta) to do the basic stuff like testing for the presence of a field but I am unsure where to start for driving out some AJAX functionality.

Check out Adam McCrea's fork of webrat:

http://github.com/adamlogic/webrat/tree/master

It includes a submit_form_via_ajax method that I use to do this. It basically just forces Rails into treating the form request as asking for a .js response. There are limitations on what you can do with the response (e.g. inspecting the entire page is no longer possible), but you are able to look at the response body of the call and match it against regular expressions. So, I use features like:

    When I go to the homepage
    And I use a search term of "foo"
    And I press "search"
    And the edit_search_form is submitted via ajax
    Then the search results should have 2 bars

with the critical step like:

When /^the (.+) is submitted via ajax$/ do |form|
  submit_form_via_ajax form
end

I've left out a couple of things here (in particular, a step that fills in form fields to mimic javascript action), but this basic idea makes it possible to have acceptance tests for ajax based features that are written in the language of the customer and yet operate quickly (i.e. you don't need to have selenium or something else like that involved to run this).

The drawback of course is that it's not really exercising the full end- to-end system. While I understand that this is frowned upon (and for good reason), in my case, I have decided that the speed of the tests (and thus my willingness to use them) makes up for the drawbacks involved. To limit my exposure, I'm working on including javascript unit tests that will ensure that the behavior I stub out above actually works as expected.

Mike Doel


_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to