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
On 11/10/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? >Could be a bug. Please submit a complete failing spec so we can reproduce it faithfully. http://rspec.rubyforge.org/contribute.html Aslak> Jay > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
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 >
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