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!