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
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Run options: 
 
# Running tests:
 
.....
 
Finished tests in 0.300219s, 16.6545 tests/s, 76.6107 assertions/s.
 
5 tests, 23 assertions, 0 failures, 0 errors, 0 skips
Run options: 
 
# Running tests:
 
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
.......E..............
 
Finished tests in 0.612518s, 35.9173 tests/s, 53.8760 assertions/s.
 
  1) Error:
test_should_create_line_item(LineItemsControllerTest):
ActiveRecord::RecordNotFound: Couldn't find Product without an ID
    /home/rubys/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:313:in `find_with_ids'
    /home/rubys/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:107:in `find'
    /home/rubys/git/rails/activerecord/lib/active_record/querying.rb:5:in `find'
    /home/rubys/git/awdwr/edition4/work-193-32/depot/app/controllers/line_items_controller.rb:50:in `create'
    /home/rubys/git/rails/actionpack/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    /home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:167:in `process_action'
    /home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:10:in `process_action'
    /home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    /home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:425:in `_run__2116663521296700327__process_action__1827547031107526699__callbacks'
    /home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:405:in `__run_callback'
    /home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
    /home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:17:in `process_action'
    /home/rubys/git/rails/actionpack/lib/action_controller/metal/rescue.rb:29:in `process_action'
    /home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    /home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:123:in `block in instrument'
    /home/rubys/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    /home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:123:in `instrument'
    /home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    /home/rubys/git/rails/actionpack/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
    /home/rubys/git/rails/activerecord/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    /home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:121:in `process'
    /home/rubys/git/rails/actionpack/lib/abstract_controller/rendering.rb:45:in `process'
    /home/rubys/git/rails/actionpack/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
    /home/rubys/git/rails/actionpack/lib/action_controller/test_case.rb:490:in `process'
    /home/rubys/git/rails/actionpack/lib/action_controller/test_case.rb:54:in `process'
    /home/rubys/git/rails/actionpack/lib/action_controller/test_case.rb:412:in `post'
    /home/rubys/git/awdwr/edition4/work-193-32/depot/test/functional/line_items_controller_test.rb:21:in `block (2 levels) in <class:LineItemsControllerTest>'
    /home/rubys/git/rails/activesupport/lib/active_support/testing/assertions.rb:55:in `assert_difference'
    /home/rubys/git/awdwr/edition4/work-193-32/depot/test/functional/line_items_controller_test.rb:20:in `block in <class:LineItemsControllerTest>'
 
22 tests, 33 assertions, 0 failures, 1 errors, 0 skips
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [ruby -I"lib:test" -I"/home/rubys/.rvm/gems/ruby-1.9.3-p545/gems/rake-10.2.1/lib" "/home/rubys/.rvm/gems/ruby-1.9.3-p545/gems/rake-10.2.1/lib/rake/rake_test_loader.rb" "test/functional/**/*_test.rb" ]>

Update parameters passed as well as expected target of redirect

edit test/functional/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.

ruby -I test test/functional/line_items_controller_test.rb
Run options: 
 
# Running tests:
 
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
.......
 
Finished tests in 0.406128s, 17.2359 tests/s, 24.6228 assertions/s.
 
7 tests, 10 assertions, 0 failures, 0 errors, 0 skips

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