-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromo_rule_spec.rb
103 lines (82 loc) · 2.74 KB
/
promo_rule_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'spec_helper'
describe PromoRule do
subject { PromoRule.new(PromoRule::TYPE[:discount]) {} }
describe '#type' do
it 'should return type as value' do
expect(subject.type).to eq(1)
end
end
describe 'discount_promo_rule method' do
subject { PromoRule.discount_rule(60, 10) }
it 'should be instance of PromoRule' do
expect(subject).to be_a_kind_of(PromoRule)
end
end
describe 'creating discount_promo_rule processor' do
it 'should call block with no arguments' do
value = false
promo_rule = PromoRule.new(PromoRule::TYPE[:discount]) do
value = true
end
expect(promo_rule.apply).to be_truthy
end
it 'should call block with one argument' do
promo_rule = PromoRule.new(PromoRule::TYPE[:discount]) do |value|
value
end
expect(promo_rule.apply('foo')).to eq('foo')
end
it 'should call block with two arguments' do
promo_rule = PromoRule.new(PromoRule::TYPE[:discount]) do |value1, value2|
"#{value1}-#{value2}"
end
expect(promo_rule.apply('foo', 'bar')).to eq('foo-bar')
end
it 'should call block with three arguments' do
promo_rule = PromoRule.new(PromoRule::TYPE[:discount]) do |value1, value2, value3|
"#{value1}-#{value2}-#{value3}"
end
expect(promo_rule.apply('foo', 'bar', 'baz')).not_to eq('foo-bar-baz')
end
end
describe 'multi_discount method' do
subject { PromoRule.multi_discount_rule('001', 10, 5.00) }
it 'should be instance of PromoRule' do
expect(subject).to be_a_kind_of(PromoRule)
end
it 'should calculate discount when count is equal' do
discount = subject.apply('001', 10)
expect(discount).to eq(5.00)
end
it 'should calculate discount when count is more than' do
discount = subject.apply('001', 11)
expect(discount).to eq(5.00)
end
it 'should not discount when count is less' do
discount = subject.apply('001', 9)
expect(discount).to be_nil
end
it 'should not discount when product code is different' do
discount = subject.apply('002', 10)
expect(discount).to be_nil
end
end
describe 'discount method' do
subject { PromoRule.discount_rule(60, 10) }
it 'should be instance of PromoRule' do
expect(subject).to be_a_kind_of(PromoRule)
end
it 'should calculate discount when count is equal' do
discounted = subject.apply(60)
expect(discounted).to eq(54)
end
it 'should calculate discount when count is more than' do
discounted = subject.apply(61)
expect(discounted).to eq(54.9)
end
it 'should not discount when count is less than' do
discounted = subject.apply(59)
expect(discounted).to eq(59)
end
end
end