views:

1167

answers:

2

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!

+1  A: 

Can you post some controller code? The xhr :get, :index code should work fine, so perhaps there is an issue elsewhere?

Chris Powers
Thanks for your reply. I've added the sample controller code and the error response showing that the :show action is being called.
Chris
A: 

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.

Peter Wagenet