summaryrefslogtreecommitdiff
path: root/spec/unit/cart_spec.rb
blob: 80809f6dcb765268361ae75eb94b9005758295ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require "spec_helper"

class Cart
  def initialize(items = [])
    @items = items
  end

  def add(product)
    @items.push(product)
  end

  def includes?(product)
    @items.include?(product)
  end

  def quantity_of(product)
    @items.find_all do |item|
      item == product
    end.count
  end
end

describe Cart do
  let(:sut) { Cart.new }

  context "when there are no items in the cart"  do
    let(:product) { fake }
    let(:result) { sut.includes?(product) }

    it "should return false" do
      result.should be_false
    end
  end

  context "when adding a product" do
    let(:product) { fake }

    let(:result) do
      sut.add(product)
      sut.quantity_of(product)
    end

    it "should increase the quanity of that product" do
      result.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)
    end
    it "should indicate the total quanity of that product" do
      result.should == 2
    end
  end
end