small medium large xlarge

27 May 2014, 04:39
Luke Jernejcic (2 posts)

After adding the new OrderController tests, I get this error when running rake test:

#: rake test
Run options: --seed 62227

# Running tests:


Finished tests in 0.452152s, 81.8309 tests/s, 192.4132 assertions/s.

  1) Error:
ActionView::Template::Error: undefined method `*' for nil:NilClass
    app/models/line_item.rb:6:in `total_price'
    app/views/line_items/_line_item.html.erb:8:in `_app_views_line_items__line_item_html_erb__3521261521655348433_2158167120'
    app/views/carts/_cart.html.erb:4:in `_app_views_carts__cart_html_erb__461519425516510111_2164232800'
    app/views/layouts/application.html.erb:20:in `block in _app_views_layouts_application_html_erb__1687751035659072424_2156153220'
    app/helpers/application_helper.rb:6:in `hidden_div_if'
    app/views/layouts/application.html.erb:19:in `_app_views_layouts_application_html_erb__1687751035659072424_2156153220'
    test/controllers/orders_controller_test.rb:27:in `block in <class:OrdersControllerTest>'

37 tests, 87 assertions, 0 failures, 1 errors, 0 skips

Here is line_item.rb:

class LineItem < ActiveRecord::Base
  belongs_to :product
  belongs_to :cart

  def total_price
    product_price * quantity

I cannot find any difference in my code from that of the book. I thought maybe I needed values in the fixtures, so I added some, but no go.

Here is my test:

  test "should get new" do
    item =
    item.product = products(:ruby)!
    session[:cart_id] =

    get :new
    assert_response :success

Any thoughts?

18 Oct 2014, 08:16
Nikita Ivanov (1 post)

You question is still actual?

24 Dec 2014, 12:46
Patryk Nowak (1 post)

Just add in this particular test the line: item.price = 10 or something like that and it will pass. You just created the line_item, but it had a problem with a price, since total_price in line_item.rb takes price from line_items table. Unfortunately your line_item in test was made by “new”, not by “create”, where the price of product is taken and put into table.


You must be logged in to comment