diff options
| author | mo khan <mo@mokhan.ca> | 2013-04-29 16:47:37 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-04-29 16:47:37 -0600 |
| commit | c107fc3cf43d1c669f05cf843e8d457e8ac334f9 (patch) | |
| tree | d2d06c358728cc9d29f10e4ebd517058e42811fd | |
| parent | f0f0a26ed30a4fcb8f469720f88975c3b817521b (diff) | |
add multiple products to a cart
| -rw-r--r-- | lib/cart.rb | 4 | ||||
| -rw-r--r-- | spec/unit/cart_spec.rb | 47 |
2 files changed, 36 insertions, 15 deletions
diff --git a/lib/cart.rb b/lib/cart.rb index a855e23..1bfe562 100644 --- a/lib/cart.rb +++ b/lib/cart.rb @@ -16,4 +16,8 @@ class Cart item == product end.count end + + def total_items + @items.count + end end diff --git a/spec/unit/cart_spec.rb b/spec/unit/cart_spec.rb index 048c813..c77ea34 100644 --- a/spec/unit/cart_spec.rb +++ b/spec/unit/cart_spec.rb @@ -3,9 +3,12 @@ require "spec_helper" describe Cart do let(:sut) { Cart.new } + let(:crayons) { fake } + let(:phone) { fake } + let(:laptop) { fake } + context "when there are no items in the cart" do - let(:product) { fake } - let(:result) { sut.includes?(product) } + let(:result) { sut.includes?(crayons) } it "should return false" do result.should be_false @@ -13,28 +16,42 @@ describe Cart do end context "when adding a product" do - let(:product) { fake } + before { sut.add(crayons) } - let(:result) do - sut.add(product) - sut.quantity_of(product) + it "should increase the quanity of that product" do + sut.quantity_of(crayons).should == 1 end - it "should increase the quanity of that product" do - result.should == 1 + it "should indicate the total number of unique items in the cart" do + sut.total_items.should == 1 end end context "when adding more then one of the same product" do - let(:product) { fake } - - let(:result) do - sut.add(product) - sut.add(product) - sut.quantity_of(product) + before :each do + sut.add(crayons) + sut.add(crayons) end + it "should indicate the total quanity of that product" do - result.should == 2 + sut.quantity_of(crayons).should == 2 + end + + it "should indicate the total number of items in the cart" do + sut.total_items.should == 2 end end + + context "when adding different products" do + before :each do + sut.add(crayons) + sut.add(phone) + sut.add(laptop) + end + + it "should indicate the total number of items in the cart" do + sut.total_items.should == 3 + end + end + end |
