Skip to content

Instantly share code, notes, and snippets.

@sgharms
Last active August 29, 2015 14:10
Show Gist options
  • Save sgharms/646c36867a1d1ba197bb to your computer and use it in GitHub Desktop.
Save sgharms/646c36867a1d1ba197bb to your computer and use it in GitHub Desktop.
class TimeGreeter
def initialize(time = Time.now)
@time = time.hour
end
def greet
puts "At the tone, the time of day is #{hour_word}"
end
private
def hour_word
return "morning" if @time < 12
return "night" if @time > 18
"afternoon"
end
end
TimeGreeter.new.greet # => Will change over time, may require a gem like TimeCop
# See here I can "inject" a time. I can "stub" in a value (some arbitrary
# test data) and make sure that the algorithm in TimeGreeter works.
TimeGreeter.new(Time.new(2002, 10, 31, 2, 2, 2, "+02:00")).greet
TimeGreeter.new(Time.new(2002, 10, 31, 22, 22, 22, "+02:00")).greet
# Thus you can imagine in an RSpec test that....
#
# do "test where i don't need timecop" do
# it "does the right thing" do
# expect(TimeGreeter.new(Time.new(2002, 10, 31, 22, 22, 22,
# "+02:00")).greet).to eq "At the tone, the time of day is night"
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment