Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To add test_alarm_is_on_when_pressure_out_of_range() and test_alarm_i…
…s_off_when_pressure_within_range(), create customerized mock to isolate sensor, meanwhile, make sensor as public variable.
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
TDDMicroExercises/Ruby/tire_pressure_monitoring_system/test/alarm_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,39 @@ | ||
require 'test/unit' | ||
require_relative '../alarm' | ||
require_relative '../sensor' | ||
|
||
class MockSensor < Sensor | ||
def initialize psi_value | ||
@psi_value = psi_value | ||
end | ||
|
||
def pop_next_pressure_psi_value | ||
@psi_value | ||
end | ||
end | ||
|
||
class AlarmTest < Test::Unit::TestCase | ||
def test_alarm_is_off_after_it_initialized | ||
alarm = Alarm.new | ||
assert_equal false, alarm.alarm_on | ||
end | ||
|
||
def test_alarm_is_on_when_pressure_out_of_range | ||
alarm = Alarm.new | ||
alarm.sensor = MockSensor.new(10) | ||
|
||
alarm.check | ||
|
||
assert_equal true, alarm.alarm_on | ||
end | ||
|
||
def test_alarm_is_off_when_pressure_within_range | ||
alarm = Alarm.new | ||
alarm.sensor = MockSensor.new(20) | ||
|
||
alarm.check | ||
|
||
assert_equal false, alarm.alarm_on | ||
end | ||
|
||
end |