-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscount_manager_spec.rb
61 lines (46 loc) · 1.79 KB
/
discount_manager_spec.rb
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
61
require 'spec_helper'
describe DiscountManager do
let(:promo_rule) {double('promo_rule')}
let(:product) { Struct.new(:code, :name, :price).new('001', 'foobar', 10.97) }
subject { DiscountManager.new([promo_rule]) }
describe '#discount_price_for' do
before do
allow(promo_rule).to receive(:type) { PromoRule::TYPE[:multi_discount] }
allow(promo_rule).to receive(:apply) { 60 }
end
it 'should pass the calculation to the promo_rule with params' do
expect(promo_rule).to receive(:type) { PromoRule::TYPE[:multi_discount] }
expect(promo_rule).to receive(:apply).with(product.code, 2)
subject.discount_price_for(product, 2)
end
it 'should return the discounted price when rule applied' do
price = subject.discount_price_for(product, 2)
expect(price).to eq(60)
end
it 'should return the original price if no rule applied' do
discount_manager = DiscountManager.new
price = discount_manager.discount_price_for(product, 1)
expect(price).to eq(product.price)
end
end
describe '#discount_total' do
before do
allow(promo_rule).to receive(:type) { PromoRule::TYPE[:discount] }
allow(promo_rule).to receive(:apply) { 100 }
end
it 'should pass the calculation to the rule with subtotal' do
expect(promo_rule).to receive(:type) { PromoRule::TYPE[:discount] }
expect(promo_rule).to receive(:apply).with(10)
subject.discount_total(10)
end
it 'should return the calculated total when rule applied' do
total = subject.discount_total(10)
expect(total).to eq(100)
end
it 'should return original subtotal if no rule match' do
discount_manager = DiscountManager.new
total = discount_manager.discount_total(10)
expect(total).to eq(10)
end
end
end