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
(in /home/rubys/svn/rails4/Book/util/work-193/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.......
Finished in 0.999783 seconds.
7 tests, 25 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 21645
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.F....................
Finished in 0.572870 seconds.
1) Failure:
test_should_destroy_cart(CartsControllerTest) [test/functional/carts_controller_test.rb:47]:
Expected response to be a redirect to <http://test.host/carts> but was a redirect to <http://test.host/>
22 tests, 36 assertions, 1 failures, 0 errors, 0 skips
Test run options: --seed 12025
Errors running 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
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
Loaded suite test/unit/cart_test
Started
E.
Finished in 0.111946 seconds.
1) Error:
test_add_duplicate_product(CartTest):
NameError: undefined local variable or method `book_one' for #<CartTest:0x9cd5dbc>
test/unit/cart_test.rb:24:in `block in <class:CartTest>'
2 tests, 2 assertions, 0 failures, 1 errors, 0 skips
Test run options: --seed 39830
pub depot_i
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
Loaded suite test/unit/cart_test
Started
..
Finished in 0.203101 seconds.
2 tests, 5 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 39443
Now do the tests pass? Nope.
rake test
(in /home/rubys/svn/rails4/Book/util/work-193/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
........
Finished in 0.204518 seconds.
8 tests, 29 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 21792
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
......................
Finished in 0.594875 seconds.
22 tests, 35 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 13247
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
(in /home/rubys/svn/rails4/Book/util/work-193/depot)
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
........
Finished in 0.265573 seconds.
8 tests, 29 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 4255
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.3-r28190%global/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.......................
Finished in 0.599459 seconds.
23 tests, 37 assertions, 0 failures, 0 errors, 0 skips
Test run options: --seed 32797
git commit -a -m "Adding a Cart"
Created commit dc60030: Adding a Cart
6 files changed, 80 insertions(+), 9 deletions(-)
git tag iteration-d
11.1 Iteration F1: Moving the Cart 10.3 Iteration E3: Finishing the Cart