rspec获取错误失败/错误:response.should render_template

时间:2015-02-07 11:54:13

标签: ruby-on-rails ruby rspec-rails

我使用的是rails 4和rspec 3。 我有一个控制器,我想用rspec测试,但我收到以下错误

Failure/Error: response.should render_template :show
expecting <"show"> but rendering with <[]>

controller_spec就像这样

  describe "GET #show" do 
    it "renders the #show view" do 
    get :show, id: FactoryGirl.create(:product)
    response.should render_template :show
  end
 end

_show.html部分就像这样

    <h2><%= @product.name %></h2>
<div class="product-price"> <h3><%= number_to_currency @product.price %> </h3></div>
<div class="product-description well"><%= @product.description %> </div>
</div>

<%= link_to "Edit", edit_product_path , remote: true, class: "btn btn-primary" %>
<%= link_to "Delete", product_delete_path(@product), remote: true, class: "btn btn-danger" %>

show.js.erb就像这样

$("#product-details").html("<%= escape_javascript(render 'show') %>")

并且在product_controller中显示函数是

 def show
    @product = Product.find(params[:id])
       respond_to do |f|
  f.html { render nothing: true } # prevents rendering a nonexistent template file
  f.js # still renders the JavaScript template
end
  end

1 个答案:

答案 0 :(得分:1)

规范正在请求HTML,并且由于您没有呈现任何内容(render nothing: true),因此您获得的结果是正确的。如果您想测试呈现.js模板,请在get前加上xhr

xhr get :show, id: FactoryGirl.create(:product)