2015-02-07 54 views
0

我使用,我想用RSpec的測試軌道4,5和RSpec 3. 我有一個控制器,但我收到以下錯誤rspec的得到錯誤故障/錯誤:response.should render_template

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_controller這個

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

和播放功能,

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

該規範正在請求HTML,既然你什麼都不渲染(render nothing: true),結果你'得到的是正確的。如果要測試渲染.js模板,請使用xhr前言get

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