Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Functionals Test fails for undefined method in view
    primarykey
    data
    text
    <p>I'm new of Rails and I have a problem with a functional test that fails for undefined method in view :</p> <pre><code>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' </code></pre> <p>It is all ok in the browser, only the test fails.</p> <p>Here is the <code>views/orders/index.html.erb</code> file content:</p> <pre><code>&lt;% @orders.each do |order| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= order.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= order.address %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= order.email %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= order.pay_type.name %&gt;&lt;/td&gt; </code></pre> <p><code>Pay_type</code> is another table that contains the pay type name; in orders table there is a foreign key called <code>pay_type_id</code>. In the browser I see correctly the pay type name with <code>order.pay_type.name</code> statement, but functional test fails.</p> <p><code>test_should_get_index:</code></p> <pre><code> test "should get index" do get :index assert_response :success assert_not_nil assigns(:orders) end </code></pre> <p><em>orders fixtures:</em></p> <pre><code>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 </code></pre> <p><em>paytypes fixtures:</em></p> <pre><code>one: id: 1 name: Check two: id: 2 name: Credit Card three: id: 3 name: Purchase Order </code></pre> <p>*orders_controller:*</p> <pre><code>def index @orders = Order.paginate :page=&gt;params[:page], :order=&gt;'created_at desc', :per_page =&gt; 10 respond_to do |format| format.html # index.html.erb format.xml { render :xml =&gt; @orders } end end </code></pre> <p>Can somebody help me ? </p> <p>I added <code>raise Exception.new(@orders.inspect)</code> in the view and i get following error:</p> <pre><code>`1) Error: test_should_get_index(OrdersControllerTest): ActionView::Template::Error: [#&lt;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&gt;, #&lt;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&gt;] 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'` </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload