gpt4 book ai didi

ruby-on-rails - 在 View 中使用另一个模型导致测试错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:17 24 4
gpt4 key购买 nike

app/views/products/index.html.erb:

<p id="notice"><%= notice %></p>

<%= render 'search' %>

<h1>Listing Products</h1>

<br>

<%= will_paginate @products %>
<% if @products.present? %>

<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Image</th>
<th>Price</th>
<th>Category</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.title %></td>
<td><%= product.description %></td>
<td><%= image_tag product.photo.url(:small) %></td>
<td><%= number_to_currency(product.price, :unit => '$') %></td>
<td><%= link_to Category.find(product.category_id).name, category_path(product.category_id) %></td>
<td><%= link_to 'Show', product %></td>
<% if current_user.try(:admin?) %>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
<td><a href="/cart/<%= product.id %>">Add To Cart</a></td>
</tr>
<% end %>
</tbody>
</table>

<% else %>
<% if params[:search].present? %>
<p>There are no products containing the term(s) <b><%= params[:search] %></b>.</p>
<% else %>
<p>There are no any products found in database.</p>
<% end %>
<% end %>

<br>
<% if current_user.try(:admin?) %>
<%= link_to 'New Product', new_product_path %>
<% end %>

app/views/products/show.html.erb:

<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @product.title %>
</p>

<p>
<strong>Description:</strong>
<%= @product.description %>
</p>

<p>
<strong>Image:</strong>
<%= image_tag @product.photo.url(:original) %>
</p>

<p>
<strong>Price:</strong>
<%= @product.price %>
</p>

<p>
<strong>Category:</strong>
<%= @product.category.name %>
</p>

<%= social_share_button_tag(@product.title, :url => request.original_url, :image => root_url + @product.photo.url(:small), desc: @product.description, via: "SCOR") %>

<% if current_user.try(:admin?) %>
<%= link_to 'Edit', edit_product_path(@product) %> |
<% end %>
<%= link_to 'Back', products_path %>

我在 test/controllers/product_controller_test.rb 中的测试

  test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:products)
end

test "should show product" do
get :show, id: @product
assert_response :success
end

导致这些错误:

  1) Error:
ProductsControllerTest#test_should_get_index:
ActionView::Template::Error: Couldn't find Category with 'id'=1
app/views/products/index.html.erb:31:in `block in _app_views_products_index_html_erb___969781135508440478_70267650596940'
app/views/products/index.html.erb:25:in `_app_views_products_index_html_erb___969781135508440478_70267650596940'
test/controllers/products_controller_test.rb:15:in `block in <class:ProductsControllerTest>'


2) Error:
ProductsControllerTest#test_should_show_product:
ActionView::Template::Error: undefined method `name' for nil:NilClass
app/views/products/show.html.erb:25:in `_app_views_products_show_html_erb___1500506684449289207_70267650787820'
test/controllers/products_controller_test.rb:36:in `block in <class:ProductsControllerTest>'

我正在使用 rake test:functionals 运行我的测试。创建、更新和销毁的其他测试正在运行,但我从索引和显示操作的测试中收到错误。

如您所见,我在产品 View 中使用了类别模型,显然它导致了这些错误。那么我该如何解决这个问题呢?

我的意思是 View 中的这些行有问题:

<td><%= link_to Category.find(product.category_id).name, category_path(product.category_id) %></td>

<%= @product.category.name %>

如果您想查看整个项目:https://github.com/mertyildiran/SCOR

最佳答案

在您的产品固定装置中,您定义了一个 category_id = 1 的产品

当您运行测试时,它找不到 id = 1 的类别

test/fixtures/products.yml 中:

替换:

category_id: 1

通过

category: one

为什么是“一个”?因为在 test/fixtures/categories.yml 中,您定义了一个标记为 one 的类别:

one:
name: New T-Shirts
desc: Example category description

您不应在固定装置中引用 id,而应使用关联。

关于该主题的一篇不错的帖子:http://blog.bigbinary.com/2014/09/21/tricks-and-tips-for-using-fixtures-in-rails.html

此外,您应该替换:

link_to Category.find(product.category_id).name, category_path(product.category_id)

通过

link_to product.category.name, category_path(product.category)

关于ruby-on-rails - 在 View 中使用另一个模型导致测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37231083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com