Guest User

Untitled

a guest
Feb 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. require 'test/unit'
  2. require 'rubygems'
  3. require 'mocha'
  4. require 'stubba'
  5.  
  6. class Clock
  7. def current_time
  8. "The time is now #{Time.now.strftime("%H:%M")}"
  9. end
  10.  
  11. def quarter_time
  12. now = Time.now
  13. hour = now.hour
  14. next_hour = hour == 23 ? 0 : hour + 1
  15. minute = now.min
  16.  
  17. case minute
  18. when 0..20
  19. "quarter past #{hour}"
  20. when 21..40
  21. "half past #{hour}"
  22. when 41..59
  23. "quarter til #{next_hour}"
  24. end
  25. end
  26. end
  27.  
  28. class ClockTest < Test::Unit::TestCase
  29. def setup
  30. @clock = Clock.new
  31. end
  32.  
  33. def test_should_return_current_time
  34. now = Time.now
  35. now_strftime = now.strftime("%H:%M")
  36. now.expects(:strftime).with("%H:%M").returns(now_strftime)
  37. Time.expects(:now).returns(now)
  38.  
  39. assert_equal "The time is now #{now_strftime}",
  40. @clock.current_time
  41. end
  42.  
  43. def test_should_return_quarter_past_quarter_time
  44. now = Time.now
  45. time_1212 = Time.local(now.year, now.month, now.day, 12, 12, 0)
  46. time_1212.expects(:hour).returns(12)
  47. time_1212.expects(:min).returns(12)
  48. Time.expects(:now).returns(time_1212)
  49.  
  50. assert_equal "quarter past 12", @clock.quarter_time
  51. end
  52.  
  53. def test_should_return_half_past_quarter_time
  54. now = Time.now
  55. time_1230 = Time.local(now.year, now.month, now.day, 12, 30, 0)
  56. time_1230.expects(:hour).returns(12)
  57. time_1230.expects(:min).returns(30)
  58. Time.expects(:now).returns(time_1230)
  59.  
  60. assert_equal "half past 12", @clock.quarter_time
  61. end
  62.  
  63. def test_should_return_quarter_til_quarter_time
  64. now = Time.now
  65. time_1249 = Time.local(now.year, now.month, now.day, 12, 49, 0)
  66. time_1249.expects(:hour).returns(12)
  67. time_1249.expects(:min).returns(49)
  68. Time.expects(:now).returns(time_1249)
  69.  
  70. assert_equal "quarter til 13", @clock.quarter_time
  71. end
  72. end
Add Comment
Please, Sign In to add comment