Skip to content

Instantly share code, notes, and snippets.

@leemour
Created April 26, 2014 15:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leemour/11322648 to your computer and use it in GitHub Desktop.
Save leemour/11322648 to your computer and use it in GitHub Desktop.
RSpec time stubbing without Timecop
#Timecop is fun, but you don't need a whole gem for that. Just use
#Time.stub(:now), e.g.
describe "time" do
before do
@fake_time = Time.now
Time.stub(:now) { @fake_time }
end
it "is equal" do
Time.now.should == Time.now # now it passes
end
it "is close" do
Time.now.should be_within(0.1).of(Time.now)
end
end
#It's easy to move @fake_time around in the middle of a spec, too, to
#simulate time passing or to zap over to e.g. the moment when daylight
#savings happens.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment