Activity Description

Add unit tests which add unique products and duplicate products. Note that you will need to modify the fixture to refer to products and carts by name, for example product: ruby.

Author’s Solutions


Readers’ Solutions

Long says

My attempt:

In test/unit/cart_test.rb: def new_cart_with_one_product(product_name) cart = Cart.create cart.add_product(products(product_name).id) cart end test 'cart should create a new line item when adding a new product' do cart = new_cart_with_one_product(:one) assert_equal 1, cart.line_items.count # Add a new product cart.add_product(products(:ruby).id) assert_equal 2, cart.line_items.count end test 'cart should update an existing line item when adding an existing product' do cart = new_cart_with_one_product(:one) # Re-add the same product cart.add_product(products(:one).id) assert_equal 1, cart.line_items.count end h4. Arun says I am doubtful whether the question was about creating a new and duplicate line items or products. I thought the answer is along the lines of test "unique_product_should_be_saved" product = => "unique-title", :description => "a unique product", :image_url => "unique.png", :price => 22) assert end test "non_unique_product_should_error_out" product = => "unique-title", :description => "a unique product", :image_url => "unique.png", :price => 22) assert ! assert_equal "has not been saved since it is a duplicate ", product.errors[:title].join('; ') end