On Jan 26, 2012, at 8:44 AM, Yi Wen wrote:
> 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
> _______________________________________________
> rspec-users mailing list
> [email protected]
> 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.
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users