Agile Web Development with Rails, Edition 4

10.1 Iteration E1: Creating a Smarter Cart 9.3 Iteration D3: Adding a button

9.4 Playtime

Once again, get the tests working, and add tests for the smarter cart.

See that the tests fail.

rake test
Run options: --seed 16650
 
# Running:
 
........E...................
 
Finished in 0.469586s, 59.6270 runs/s, 142.6789 assertions/s.
 
  1) Error:
LineItemsControllerTest#test_should_create_line_item:
ActiveRecord::RecordNotFound: Couldn't find Product with 'id'=
    app/controllers/line_items_controller.rb:35:in `create'
    test/controllers/line_items_controller_test.rb:21:in `block (2 levels) in <class:LineItemsControllerTest>'
    test/controllers/line_items_controller_test.rb:20:in `block in <class:LineItemsControllerTest>'
 
28 runs, 67 assertions, 0 failures, 1 errors, 0 skips

Update parameters passed as well as expected target of redirect

edit test/controllers/line_items_controller_test.rb
  test "should create line_item" do
    assert_difference('LineItem.count') do
      post :create, product_id: products(:ruby).id
    end
 
    assert_redirected_to cart_path(assigns(:line_item).cart)
  end

Rerun the previously failing test to verify the fix.

rake test test/controllers/line_items_controller_test.rb
Run options: --seed 53882
 
# Running:
 
.......
 
Finished in 0.294896s, 23.7372 runs/s, 44.0833 assertions/s.
 
7 runs, 13 assertions, 0 failures, 0 errors, 0 skips

10.1 Iteration E1: Creating a Smarter Cart 9.3 Iteration D3: Adding a button