gpt4 book ai didi

ruby-on-rails - rails : Functionals Test fails for undefined method in view

转载 作者:行者123 更新时间:2023-11-28 20:56:37 26 4
gpt4 key购买 nike

我是 Rails 的新手,我遇到了一个功能测试问题, View 中未定义的方法失败了:

1) Error:
test_should_get_index(OrdersControllerTest):
ActionView::Template::Error: undefined method `name' for nil:NilClass
app/views/orders/index.html.erb:19:in `_app_views_orders_index_html_erb__2011622583_2177866460_0'
app/views/orders/index.html.erb:14:in `_app_views_orders_index_html_erb__2011622583_2177866460_0'
app/controllers/orders_controller.rb:8:in `index'
/test/functional/orders_controller_test.rb:9:in `test_should_get_index'

在浏览器中一切正常,只是测试失败。

这里是 views/orders/index.html.erb 文件内容:

<% @orders.each do |order| %>
<tr>
<td><%= order.name %></td>
<td><%= order.address %></td>
<td><%= order.email %></td>
<td><%= order.pay_type.name %></td>

Pay_type 是另一个包含支付类型名称的表;在订单表中有一个名为 pay_type_id 的外键。在浏览器中,我正确地看到带有 order.pay_type.name 语句的支付类型名称,但功能测试失败。

test_should_get_index:

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

订购灯具:

one:
name: Dave Thomas
address: MyText
email: dave@example.org
pay_type_id: 1

two:
name: MyString
address: MyText
email: MyString
pay_type_id: 1

paytypes 装置:

one:
id: 1
name: Check

two:
id: 2
name: Credit Card

three:
id: 3
name: Purchase Order

*订单 Controller :*

def index
@orders = Order.paginate :page=>params[:page], :order=>'created_at desc',
:per_page => 10

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @orders }
end
end

有人可以帮助我吗?

我在 View 中添加了 raise Exception.new(@orders.inspect) 并且出现以下错误:

`1) Error:
test_should_get_index(OrdersControllerTest):
ActionView::Template::Error: [#<Order id: 298486374, name: "MyString", address: "MyText", email: "MyString", created_at: "2013-02-12 14:05:11", updated_at: "2013-02-12 14:05:11", pay_type_id: 1>, #<Order id: 980190962, name: "Dave Thomas", address: "MyText", email: "dave@example.org", created_at: "2013-02-12 14:05:11", updated_at: "2013-02-12 14:05:11", pay_type_id: 1>]
app/views/orders/index.html.erb:14:in `_app_views_orders_index_html_erb__1955286768_2214618420_0'
app/controllers/orders_controller.rb:8:in `index'
/test/functional/orders_controller_test.rb:9:in `test_should_get_index'`

最佳答案

你在@orders 中有一些东西是 nil,当你在它上面调用 name 时它会给出那个错误。确保@orders 仅包含订单,并且在测试时不包含 nil。

关于ruby-on-rails - rails : Functionals Test fails for undefined method in view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14832969/

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