[rspec-users] Stubbing Time.now in trunk

Jay Levitt lists-rspec at shopwatch.org
Fri Nov 10 18:17:42 EST 2006


David Chelimsky wrote:
> Try this:
> 
> @time_now = Time.parse("Jan 1 2001")
> Time.stub!(:now).and_return(@time_now)

That works, but the effect I was trying to achieve was a changeable 
Time.now, as in:

@time_now = Time.parse("Jan 1 2001")
Time.stub!(:now).and_return(lambda{@time_now})
MyApp.do_something
@time_now = Time.parse("Oct 1 2001")
MyApp.should_be_expired


> 
> OR
> 
> Time.stub!(:now).and_return lambda { @time_now }

This is what I was trying, and was seeing those weird next-line errors.

What does seem to work is

Time.stub!(:now).and_return { @time_now }

e.g. just passing a block to and_return, instead of a lambda.  So I 
guess it's just a different syntax than I was used to with Mocha, which 
(now that I look at it) special-cases a return-value that is_a?(Proc).

Jay



More information about the rspec-users mailing list