rspec получает ошибку Ошибка/Ошибка: response.should render_template

Я использую 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

person custosat    schedule 07.02.2015    source источник


Ответы (1)


Спецификация запрашивает HTML, и, поскольку вы ничего не визуализируете (render nothing: true), вы получаете правильные результаты. Если вы хотите протестировать рендеринг шаблона .js, добавьте перед get префикс xhr:

xhr get :show, id: FactoryGirl.create(:product)
person infused    schedule 07.02.2015