Reputation: 99
I am using rails 4 and rspec 3. I have a controller which I want to test using rspec, but I am getting the following error
Failure/Error: response.should render_template :show
expecting <"show"> but rendering with <[]>
The controller_spec is like this
describe "GET #show" do
it "renders the #show view" do
get :show, id: FactoryGirl.create(:product)
response.should render_template :show
end
end
The _show.html
partial is like this
<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 is like this
$("#product-details").html("<%= escape_javascript(render 'show') %>")
and show function in product_controller is
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
Upvotes: 0
Views: 579
Reputation: 24337
The spec is requesting HTML, and since you're rendering nothing (render nothing: true
), the results you're getting are correct. If you want to test rendering the .js template, preface the get
with xhr
:
xhr get :show, id: FactoryGirl.create(:product)
Upvotes: 1