Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'test/unit'
- require 'rubygems'
- require 'mocha'
- require 'stubba'
- class Clock
- def current_time
- "The time is now #{Time.now.strftime("%H:%M")}"
- end
- def quarter_time
- now = Time.now
- hour = now.hour
- next_hour = hour == 23 ? 0 : hour + 1
- minute = now.min
- case minute
- when 0..20
- "quarter past #{hour}"
- when 21..40
- "half past #{hour}"
- when 41..59
- "quarter til #{next_hour}"
- end
- end
- end
- class ClockTest < Test::Unit::TestCase
- def setup
- @clock = Clock.new
- end
- def test_should_return_current_time
- now = Time.now
- now_strftime = now.strftime("%H:%M")
- now.expects(:strftime).with("%H:%M").returns(now_strftime)
- Time.expects(:now).returns(now)
- assert_equal "The time is now #{now_strftime}",
- @clock.current_time
- end
- def test_should_return_quarter_past_quarter_time
- now = Time.now
- time_1212 = Time.local(now.year, now.month, now.day, 12, 12, 0)
- time_1212.expects(:hour).returns(12)
- time_1212.expects(:min).returns(12)
- Time.expects(:now).returns(time_1212)
- assert_equal "quarter past 12", @clock.quarter_time
- end
- def test_should_return_half_past_quarter_time
- now = Time.now
- time_1230 = Time.local(now.year, now.month, now.day, 12, 30, 0)
- time_1230.expects(:hour).returns(12)
- time_1230.expects(:min).returns(30)
- Time.expects(:now).returns(time_1230)
- assert_equal "half past 12", @clock.quarter_time
- end
- def test_should_return_quarter_til_quarter_time
- now = Time.now
- time_1249 = Time.local(now.year, now.month, now.day, 12, 49, 0)
- time_1249.expects(:hour).returns(12)
- time_1249.expects(:min).returns(49)
- Time.expects(:now).returns(time_1249)
- assert_equal "quarter til 13", @clock.quarter_time
- end
- end
Add Comment
Please, Sign In to add comment