[rspec-users] Stubbing Time.now in trunk

David Chelimsky dchelimsky at gmail.com
Fri Nov 10 16:35:59 EST 2006


Try this:

@time_now = Time.parse("Jan 1 2001")
Time.stub!(:now).and_return(@time_now)

OR

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

What you were doing was returning a lambda. So the code that was
expecting a time value was getting a poor lambda that nobody is
calling :(

Mary had a little lambda....

Cheers,
David


On 11/9/06, Jay Levitt <lists-rspec at shopwatch.org> wrote:
> I'm a big fan of stubbing Time.now so it returns a known value.  I used
> to be able to use stubba and say:
>
> @time_now = Time.parse("Jan 1 2001")
> Time.stubs(:now).returns(lambda{@time_now})
>
> However, something in trunk broke that.  Fine, rspec's got its own
> stubbing lib now, so I tried switching to that:
>
> @time_now = Time.parse("Jan 1 2001")
> Time.stub!(:now).and_return(lambda{@time_now})
>
> This gives me some VERY odd errors.  For instance, if this is in a
> controller spec, and the next line is
>   get 'index'
>
> I get:
>
> wrong number of arguments (1 for 0)
> ./spec/controllers/one_spec.rb:9:in `should render the index page'
>
> Line 9 is the get 'index' line. If I delete that, so there is no next
> line, I get:
>
> NoMethodError in 'the test app should render the index page'
> undefined method `to_i' for
> #<Proc:0x04a0374c at ./spec/controllers/one_spec.rb:8>
>
> Am I doing something wrong, or is this a bug in trunk?
>
> Jay
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list