Commit c107fc3

mo khan <mo@mokhan.ca>
2013-04-29 22:47:37
add multiple products to a cart
1 parent f0f0a26
Changed files (2)
lib/cart.rb
@@ -16,4 +16,8 @@ class Cart
       item == product
     end.count
   end
+
+  def total_items
+    @items.count
+  end
 end
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