Agile Web Development with Rails, Edition 4

11.1 Iteration F1: Moving the Cart 10.3 Iteration E3: Finishing the Cart

10.4 Playtime

2 tests, 0 assertions, 0 failures, 1 errors.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:27:in `assert_test_summary'
  /home/rubys/git/awdwr/edition4/checkdepot.rb:170:in `block in <class:DepotTest>'

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

See that the tests fail.

rails test
.F.........................
 
Finished tests in 0.612561s, 44.0772 tests/s, 109.3769 assertions/s.
 
  1) Failure:
CartsControllerTest#test_should_destroy_cart [test/controllers/carts_controller_test.rb:47]:
Expected response to be a redirect to <http://test.host/carts> but was a redirect to <http://test.host/>.
Expected "http://test.host/carts" to be === "http://test.host/".
 
27 tests, 67 assertions, 1 failures, 0 errors, 0 skips

Substitute names of products and carts for numbers

edit test/fixtures/line_items.yml
# Read about fixtures at
# http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
 
one:
  product: ruby
  cart: one
 
two:
  product: ruby
  cart: one

Update expected target of redirect: Cart#destroy.

edit test/controllers/carts_controller_test.rb
  test "should destroy cart" do
    assert_difference('Cart.count', -1) do
      session[:cart_id] = @cart.id
      delete :destroy, id: @cart
    end
 
    assert_redirected_to store_path
  end

Test both unique and duplicate products.

edit test/models/cart_test.rb
require 'test_helper'
 
class CartTest < ActiveSupport::TestCase
  test "add unique products" do
    cart = Cart.create
    book_one = products(:one)
    book_two  = products(:two)
    cart.add_product(book_one.id).save!
    cart.add_product(book_two.id).save!
    assert_equal 2, cart.line_items.size
    assert_equal book_one.price + book_two.price, cart.total_price
  end
  
  test "add_duplicate_product" do
    cart = Cart.create
    ruby_book = products(:ruby)
    cart.add_product(ruby_book.id).save!
    cart.add_product(ruby_book.id).save!
    assert_equal 2*book_one.price, cart.total_price
    assert_equal 1, cart.line_items.size
    assert_equal 2, cart.line_items[0].quantity
  end 
end
rails test test/models/cart_test.rb
EE
 
Finished tests in 0.054926s, 36.4125 tests/s, 0.0000 assertions/s.
 
  1) Error:
CartTest#test_add_duplicate_product:
NoMethodError: undefined method `products' for #<CartTest:0x00000001f20bb0>
    test/models/cart_test.rb:21:in `block in <class:CartTest>'
 
  2) Error:
CartTest#test_add_unique_products:
NoMethodError: undefined method `products' for #<CartTest:0x00000003ef0968>
    test/models/cart_test.rb:9:in `block in <class:CartTest>'
 
2 tests, 0 assertions, 0 failures, 2 errors, 0 skips

Refactor.

edit test/models/cart_test.rb
require 'test_helper'
 
class CartTest < ActiveSupport::TestCase
  def setup
    @cart  = Cart.create
    @book_one = products(:ruby)
    @book_two  = products(:two)
  end
  
  test "add unique products" do
    @cart.add_product(@book_one.id).save!
    @cart.add_product(@book_two.id).save!
    assert_equal 2, @cart.line_items.size
    assert_equal @book_one.price + @book_two.price, @cart.total_price
  end
 
  test "add duplicate product" do
    @cart.add_product(@book_one.id).save!
    @cart.add_product(@book_one.id).save!
    assert_equal 2*@book_one.price, @cart.total_price
    assert_equal 1, @cart.line_items.size
    assert_equal 2, @cart.line_items[0].quantity
  end 
end
rails test test/models/cart_test.rb
EE
 
Finished tests in 0.027716s, 72.1599 tests/s, 0.0000 assertions/s.
 
  1) Error:
CartTest#test_add_duplicate_product:
NoMethodError: undefined method `products' for #<CartTest:0x00000004356570>
    test/models/cart_test.rb:6:in `setup'
 
  2) Error:
CartTest#test_add_unique_products:
NoMethodError: undefined method `products' for #<CartTest:0x00000004b4c798>
    test/models/cart_test.rb:6:in `setup'
 
2 tests, 0 assertions, 0 failures, 2 errors, 0 skips

Verify that the tests pass.

rails test
.............................
 
Finished tests in 0.613734s, 47.2518 tests/s, 117.3148 assertions/s.
 
29 tests, 72 assertions, 0 failures, 0 errors, 0 skips

Add a test ensuring that non-empty carts can't be deleted.

edit test/controllers/products_controller_test.rb
  test "can't delete product in cart" do
    assert_difference('Product.count', 0) do
      delete :destroy, id: products(:ruby)
    end
 
    assert_redirected_to products_path
  end
 
  test "should destroy product" do
    assert_difference('Product.count', -1) do
      delete :destroy, id: @product
    end
 
    assert_redirected_to products_path
  end

Now the tests should pass.

rails test
..............................
 
Finished tests in 0.639724s, 46.8952 tests/s, 117.2380 assertions/s.
 
30 tests, 75 assertions, 0 failures, 0 errors, 0 skips

Add price to line item

rails generate migration add_price_to_line_item price:decimal
      invoke  active_record
      create    db/migrate/20130310025354_add_price_to_line_item.rb
edit db/migrate/20130310025354_add_price_to_line_item.rb
class AddPriceToLineItem < ActiveRecord::Migration
  def change
    add_column :line_items, :price, :decimal
    LineItem.all.each do |li|
      li.price = li.product.price
    end
  end
end
rake db:migrate
mv 20130310025354_add_price_to_line_item.rb 20130309000006_add_price_to_line_item.rb
==  AddPriceToLineItem: migrating =============================================
-- add_column(:line_items, :price, :decimal)
   -> 0.0004s
==  AddPriceToLineItem: migrated (0.0794s) ====================================
 
edit app/models/cart.rb
class Cart < ActiveRecord::Base
  has_many :line_items, dependent: :destroy
 
  def add_product(product_id)
    current_item = line_items.find_by_product_id(product_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(product_id: product_id)
      current_item.price = current_item.product.price
    end
    current_item
  end
 
  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end
end
git commit -a -m "Adding a Cart"
[master 13a922f] Adding a Cart
 4 files changed, 63 insertions(+), 11 deletions(-)
git tag iteration-d

11.1 Iteration F1: Moving the Cart 10.3 Iteration E3: Finishing the Cart