forked from emilybache/TDDwithMockObjectsAndDesignPrinciples
/
alarm_spec.rb
46 lines (39 loc) · 1.36 KB
/
alarm_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
require_relative "../../code/tire_pressure_monitoring_system/alarm"
describe Alarm do
context "#alarm_status" do
it "turns on alarm when pressure is less than minimum threshold" do
check_pressure(Alarm::LOWPRESSURETHRESHOLD - 1, true)
end
it "turns on alarm when pressure is greater than maximum threshold" do
check_pressure(Alarm::HIGHPRESSURETHRESHOLD + 1, true)
end
it "turns off alarm when pressure is at the threshold" do
check_pressure(Alarm::HIGHPRESSURETHRESHOLD, false)
end
end
context "#alarm_count" do
it "increases nubmer of alarms after turning on the alarm" do
alarm = create_alarm_with_stub_sensor(
Alarm::HIGHPRESSURETHRESHOLD + 1)
expect do
alarm.check
end.to change { alarm.alarm_count }.by 1
end
it "doesn't increase alarm counts when doesn't turn on alarm" do
alarm = create_alarm_with_stub_sensor(
Alarm::LOWPRESSURETHRESHOLD)
expect do
alarm.check
end.not_to change { alarm.alarm_count }
end
end
def check_pressure(stub_pressure, expected_alarm_status)
alarm = create_alarm_with_stub_sensor(stub_pressure)
alarm.check
alarm.on?.should == expected_alarm_status
end
def create_alarm_with_stub_sensor(stub_pressure)
sensor = stub(:pop_next_pressure_psi_value => stub_pressure)
Alarm.new(sensor)
end
end