2009-08-26 135 views
8

在我的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在測試中調用索引操作?

謝謝!

回答

2

其實我認爲你誤解了錯誤。 Rails正試圖調用contact_url,並且參數是錯誤的。我懷疑它確實在調用索引操作,然後使聯繫人部分顯示。如果我是對的,聯繫人部分是問題的位置。我會建議檢查部分聯繫人是否有任何可能的錯誤。如果仍有問題,請將部分聯繫人的身體張貼。

0

您正在嘗試製作非持久聯繫人對象的網址。您可以在消息中看到::id=>#<Contact id: nil