[rspec-users] How To Drive Out AJAX Functionality in a Rails View

Lee Lee.Longmore at googlemail.com
Wed May 13 03:47:49 EDT 2009

I found a potential solution from this blog:

In my spec for the view in which I want to include the AJAX
functionality ("new.html.erb_spec.erb"), I have added a couple of
Examples to drive out the some of the AJAX functionality:

  it "should render /Available/ if name does not exist" do
    @name_exists = false
    assigns[:name_exists] = @name_exists
    render "contexts/check.js.rjs"
    response.should have_rjs(:chained_replace_html, 'checkname') do
      response.should have_text(/Available/)

  it "should render /Not available/ if name does not exist" do
    @name_exists = true
    assigns[:name_exists] = @name_exists
    render "contexts/check.js.rjs"
    response.should have_rjs(:chained_replace_html, 'checkname') do
      response.should have_text(/Not available/)

The rendered template "check.js.rjs" contains:

if @name_exists
  page['checkname'].update('Not available')

These examples pass and seem to achieve my objective of using the spec
to drive out the required AJAX behaviour, or at least some of it.
Though I suspect that this approach may have limitations for other
types of AJAX functionality.

At this stage, I am wondering whether it is worth trying to drive out
any AJAX functionality at the point of writing RSpec view specs. Some
replies seem to suggest the use of Cucumber with Culerity/Webrat/
Selenium and I read into this that one would bypass view specs for the
AJAX related steps and just write the necessary view code (e.g.
JavaScript or Rails Prototype helpers) to get the step to pass.

Further thoughts appreciated.

Thanks Lee.

On 12 May, 18:47, Phlip <phlip2... at gmail.com> wrote:
> David Chelimsky wrote:
> > You could just say "be careful not to mock too much," which would be
> > really good advice,
> Sorry; I thought I said that. This industry in general dislikes strong opinions...
> Mock on, everyone!
>  > but instead you make a generalizing and judgmental
> > statement like this. When's the last time you paired with someone in
> > the RSpec community?
> Well it's not for want of trying!
> --
>    Phlip
>    http://flea.sourceforge.net/resume.html
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list