Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating an XHR GET request

In my RSpec tests, I need to simulate an AJAX GET request to the index action, and have been using the code as described in both the Rails docs and the RSpec book:

xhr :get, :index

This always fails though, as the test tries to load the show action (without any parameters) rather than the specified index action.

The controller action is:

def index      
  @contacts = Contact.all

  respond_to do |format|
    format.html 
    format.js   { 
      render :update do |page|
        page.replace_html :contact_search_results, :partial => 'contacts'
      end
    }
  end
end

The error thrown by running the spec is (showing the :show action being used):

ActionView::TemplateError in 'ContactsController as an administrator user when
showing the index of contacts' as an AJAX request should render results into the
contact_search_results element'

contact_url failed to generate from {:action=>"show", :controller=>"contacts", 
:id=>#<Contact id: nil, first_name: nil, ....>}

Does anyone know how I can simulate an AJAX call the index action in tests?

Thanks!

like image 709
Chris Blunt Avatar asked Aug 26 '09 09:08

Chris Blunt


Video Answer


1 Answers

Actually I think you're misunderstanding the error. Somewhere along the way Rails is trying to call contact_url and the parameters are wrong. My suspicion is that it is indeed calling the index action which then renders the contact partial. If I'm right, the contacts partial is the location of the issue. I would recommend reviewing the contacts partial for any possible errors. If you're still having trouble, please post the body of your contacts partial.

like image 66
Peter Wagenet Avatar answered Sep 22 '22 23:09

Peter Wagenet