在我的RSpec的測試中,我需要模擬一個AJAX GET請求索引操作,並一直在使用的代碼作爲兩個Rails的文檔和RSpec的書中描述:模擬一個XHR GET請求
xhr :get, :index
儘管這總是失敗,因爲測試試圖加載show動作(不帶任何參數)而不是指定的索引動作。
控制器操作是:
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
通過運行規範拋出的錯誤(顯示:show動作正在使用):
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, ....>}
有誰知道我可以模擬一個AJAX在測試中調用索引操作?
謝謝!