Chr*_*unt 8 rspec ruby-on-rails xmlhttprequest
在我的RSpec测试中,我需要模拟对索引操作的AJAX GET请求,并且一直在使用Rails文档和RSpec书中描述的代码:
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调用索引操作?
谢谢!
实际上我认为你误解了这个错误。Rails 尝试调用过程中的某个地方contact_url,参数错误。我怀疑它确实正在调用索引操作,然后使接触部分呈现。如果我是对的,联系人部分就是问题所在。我建议检查部分联系人是否有任何可能的错误。如果您仍然遇到问题,请发布您的部分联系人正文。