小编Chr*_*unt的帖子

模拟XHR GET请求

在我的RSpec测试中,我需要模拟对索引操作的AJAX GET请求,并且一直在使用Rails文档和RSpec书中描述的代码:

xhr :get, :index
Run Code Online (Sandbox Code Playgroud)

但总是失败,因为测试试图加载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
Run Code Online (Sandbox Code Playgroud)

运行规范引发的错误是(显示:正在使用的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, ....>}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何在测试中模拟AJAX调用索引操作?

谢谢!

rspec ruby-on-rails xmlhttprequest

8
推荐指数
1
解决办法
8608
查看次数

标签 统计

rspec ×1

ruby-on-rails ×1

xmlhttprequest ×1