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

s.ross cwdinfo at gmail.com
Wed May 13 14:22:57 EDT 2009


On May 13, 2009, at 12:47 AM, Lee wrote:

> I found a potential solution from this blog:
> http://www.rubytutorials.net/2008/02/29/small-rspec-revelations-rjs/
> 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:

The one thing I would add is that Rails responds differently when it  
receives a post with XMLHttpHeaders than when these headers are not  
present. I typically put this in my spec_helper.rb and then I can  
include it where necessary.

# spec_helper.rb
module XmlHttpHelper
  def set_xhr_headers
    request.env['HTTP_ACCEPT'] = 'application/json, text/javascript, */ 
    request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'

# things_controller_spec.rb
describe ThingssController do
   include XmlHttpHelper

       it "should provide a not acceptable response if user is not  
appropriately related and it's an xhr" do
         login_as(@unrelated_child.login, 'secret')
         post :slug_collection, :name => 'bob', :collection_name =>  
         response.status.should eql('406 Not Acceptable')

Just a thought...

More information about the rspec-users mailing list