11.1 Iteration F1: Moving the Cart 10.3 Iteration E3: Finishing the Cart
7 tests, 25 assertions, 0 failures, 0 errors. Traceback: /home/rubys/git/awdwr/edition4/checkdepot.rb:125:in `block in <class:DepotTest>'
Once again, get the tests working, and add tests for the smarter cart.
See that the tests fail.
rake test
/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: test/unit/**/*_test.rb (ArgumentError)
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:606:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: test/functional/**/*_test.rb (ArgumentError)
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:606:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
Errors running test:units, test:functionals!
Substitute names of products and carts for numbers
edit test/fixtures/line_items.yml
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
product: ruby
cart: one
two:
product: ruby
cart: one
Update expected target of redirect: Cart#destroy.
edit test/functional/carts_controller_test.rb
test "should destroy cart" do
assert_difference('Cart.count', -1) do
session[:cart_id] = @cart.id
delete :destroy, :id => @cart.to_param
end
assert_redirected_to store_path
end
Test both unique and duplicate products.
edit test/unit/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
ruby -I test test/unit/cart_test.rb
Run options:
# Running tests:
E.
Finished tests in 0.294037s, 6.8019 tests/s, 6.8019 assertions/s.
1) Error:
test_add_duplicate_product(CartTest):
NameError: undefined local variable or method `book_one' for #<CartTest:0x0000000381a620>
test/unit/cart_test.rb:24:in `block in <class:CartTest>'
2 tests, 2 assertions, 0 failures, 1 errors, 0 skips
ruby -v: ruby 1.9.4dev (2011-10-18 trunk 33476) [x86_64-linux]
Refactor.
edit test/unit/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
ruby -I test test/unit/cart_test.rb
Run options:
# Running tests:
..
Finished tests in 0.287124s, 6.9656 tests/s, 17.4141 assertions/s.
2 tests, 5 assertions, 0 failures, 0 errors, 0 skips
ruby -v: ruby 1.9.4dev (2011-10-18 trunk 33476) [x86_64-linux]
Verify that the tests pass.
rake test
/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: test/unit/**/*_test.rb (ArgumentError)
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:606:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: test/functional/**/*_test.rb (ArgumentError)
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:606:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
Errors running test:units, test:functionals!
Add a test ensuring that non-empty carts can't be deleted.
edit test/functional/products_controller_test.rb
test "can't delete product in cart" do
assert_difference('Product.count', 0) do
delete :destroy, id: products(:ruby).to_param
end
assert_redirected_to products_path
end
test "should destroy product" do
assert_difference('Product.count', -1) do
delete :destroy, id: @product.to_param
end
assert_redirected_to products_path
end
Now the tests should pass.
rake test
/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: test/unit/**/*_test.rb (ArgumentError)
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:606:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
/home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: test/functional/**/*_test.rb (ArgumentError)
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:606:in `run'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from /home/rubys/.rvm/rubies/ruby-1.9.3-r33476/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
Errors running test:units, test:functionals!
Add price to line item
rails generate migration add_price_to_line_item price:decimal
invoke active_record
create db/migrate/20111018164138_add_price_to_line_item.rb
edit db/migrate/20111018164138_add_price_to_line_item.rb
class AddPriceToLineItem < ActiveRecord::Migration
def self.up
add_column :line_items, :price, :decimal
LineItem.all.each do |li|
li.price = li.product.price
end
end
def self.down
remove_column :line_items, :price
end
end
rake db:migrate
mv 20111018164138_add_price_to_line_item.rb 20110711000006_add_price_to_line_item.rb
== AddPriceToLineItem: migrating =============================================
-- add_column(:line_items, :price, :decimal)
-> 0.0011s
== AddPriceToLineItem: migrated (0.1258s) ====================================
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 94b3362] Adding a Cart
6 files changed, 95 insertions(+), 11 deletions(-)
git tag iteration-d
11.1 Iteration F1: Moving the Cart 10.3 Iteration E3: Finishing the Cart