1
未定義的方法我是新軌,且我有,鑑於失敗未定義方法的功能測試的一個問題:的Rails:函試驗失敗了,鑑於
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'
它是一切OK瀏覽器,只有測試失敗。
這裏是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: [email protected]
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
* orders_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
有人可以幫我嗎?
我在視圖中添加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: "[email protected]", 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'嗎?你還可以嘗試在'view Exception.new(@ orders.inspect)' – Max 2013-02-12 13:01:53
@Max我更新我的問題與orders_controller代碼在您的視圖中引發異常。我認爲在哪裏放置'raise Exception.new(@ orders.inspect)'?對不起,但我是新的鐵軌。非常感謝 。 – iRails 2013-02-12 13:37:27
把它放在'<%@ orders.each do | order |之前%>'。這可以讓你看到'@ orders'在你的測試中看起來像什麼。請添加該行,運行測試,然後發佈輸出。 – Max 2013-02-12 13:47:54