11.1 Iteration F1: Moving the Cart 10.3 Iteration E3: Finishing the Cart
Once again, get the tests working, and add tests for the smarter cart.
See that the tests fail.
rake test
Run options: --seed 49985
# Running:
.....F......................
Finished in 0.461908s, 60.6181 runs/s, 147.2153 assertions/s.
1) Failure:
CartsControllerTest#test_should_destroy_cart [/home/rubys/git/awdwr/edition4/work-224-42/depot/test/controllers/carts_controller_test.rb:43]:
"Cart.count" didn't change by -1.
Expected: 1
Actual: 2
28 runs, 68 assertions, 1 failures, 0 errors, 0 skips
Update expected target of redirect: Cart#destroy.
edit test/controllers/carts_controller_test.rb
test "should destroy cart" do
session[:cart_id] = @cart.id
assert_difference('Cart.count', -1) do
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
rake test test/models/cart_test.rb
Run options: --seed 20439
# Running:
.E
Finished in 0.074090s, 26.9942 runs/s, 26.9942 assertions/s.
1) Error:
CartTest#test_add_duplicate_product:
NameError: undefined local variable or method `book_one' for #<CartTest:0x000000059d5788>
test/models/cart_test.rb:24:in `block in <class:CartTest>'
2 runs, 2 assertions, 0 failures, 1 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
rake test test/models/cart_test.rb
Run options: --seed 2567
# Running:
..
Finished in 0.089792s, 22.2738 runs/s, 55.6844 assertions/s.
2 runs, 5 assertions, 0 failures, 0 errors, 0 skips
Verify that the tests pass.
rake test
Run options: --seed 8959
# Running:
..............................
Finished in 0.418830s, 71.6281 runs/s, 179.0703 assertions/s.
30 runs, 75 assertions, 0 failures, 0 errors, 0 skips
Now the tests should pass.
rake test
Run options: --seed 34553
# Running:
..............................
Finished in 0.432390s, 69.3819 runs/s, 173.4547 assertions/s.
30 runs, 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/20160507044131_add_price_to_line_item.rb
edit db/migrate/20160507044131_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 20160507044131_add_price_to_line_item.rb 20160507000006_add_price_to_line_item.rb
== 20160507000006 AddPriceToLineItem: migrating ===============================
-- add_column(:line_items, :price, :decimal)
-> 0.0004s
== 20160507000006 AddPriceToLineItem: migrated (0.0201s) ======================
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 f4e98e5] Adding a Cart
4 files changed, 61 insertions(+), 11 deletions(-)
git tag iteration-d
11.1 Iteration F1: Moving the Cart 10.3 Iteration E3: Finishing the Cart