Say I do: ```ruby object.method 5.days.ago ``` In the test I want to test using should_receive like: ```ruby object.should_receive(:method).with(5.days.ago) ``` This will fail since two time objects aren't exact the same. What is the general pattern for testing this in rspec? Thanks
on 2012-01-26 16:50
on 2012-01-26 17:01
You can use things like Timecop. I prefer to just stub Time.now: timestamp = Time.now.to_i Time.stub(:now).returns(Time.at(timestamp)) Best regards Morten Mller Riis
on 2012-01-26 17:47
On Jan 26, 2012, at 8:44 AM, Yi Wen wrote: > ``` > > This will fail since two time objects aren't exact the same. > > What is the general pattern for testing this in rspec? > > Thanks > _______________________________________________ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users In general, it's a good idea not to "hard code" time within the code that uses it. There are many ways to avoid doing this: 1.) Pass in the time. def method_that_uses_time(time) do_something_with_time(time) end it '...' do time = double('time') object.should_receive(:do_something_with_time).with(time) object.method_that_uses_time(time) end 2.) Put the time in it's own method, and stub it. def method_that_uses_time do_something_with_time(the_time) end def the_time; 5.days.ago end it '...' do time = double('time') object.should_receive(:the_time).and_return(time) object.method_that_uses_time end And then there are gems like delorean and timecop, which "freeze" time.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.