Reputation: 80
I just can't seem to get a hold of what exactly stubs are.
Could someone just explain what the following RSPEC code is supposed to do. And what is the benefit of using stub here?
require "performance_monitor"
require "time" # loads up the Time.parse method -- do NOT create time.rb!
describe "Performance Monitor" do
before do
@eleven_am = Time.parse("2011-1-2 11:00:00")
end
it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do
fake_time = @eleven_am
Time.stub(:now) { fake_time }
elapsed_time = measure do
fake_time += 60 # adds one minute to fake_time
end
elapsed_time.should == 60
end
end
I think I'll be able to understand with an example.
Upvotes: 0
Views: 237
Reputation: 126
Note that stub will only 'override' this method only in this one spec. Other specs will respond do Time.now
properly
Upvotes: 0
Reputation: 12320
stub
is used here for override
the function now
of Time
so here instead of return current time
which you got from Time.now
after stub it will return fake_time
Upvotes: 3