[mocha-developer] I'm misunderstanding how stubs works

Paul Butcher paul at 82ask.com
Fri Sep 22 19:22:00 EDT 2006


We're still just starting out with Mocha/Stubba, so please forgive any
newbie errors.

I'm using "stubs" to test some realtime functions, to control exactly which
time is returned from Time.now. I would expect the following test to pass:

  def test_two_stubs
    t = Time.now - 60
    Time.stubs(:now).returns(t)

    start_time = Time.now

    t += 20
    Time.stubs(:now).returns(t)

    end_time = Time.now

    assert_equal end_time - start_time, 20
  end

But it fails with:

  1) Failure:
test_two_stubs(MochaTest) [mochatest.rb:19]:
<0.0> expected but was
<20>.

I can create a test which works as I intend:

  def test_lambda
    t = Time.now - 60
    Time.stubs(:now).returns(lambda { t })

    start_time = Time.now

    t += 20

    end_time = Time.now

    assert_equal end_time - start_time, 20
  end

But I'd be interested to understand why the first version doesn't.

Thanks in advance for any help you can offer!

-------------------------------------------------
Paul Butcher
CTO
RE5ULT Limited
74 Eden Street,
Cambridge
CB1 1EL
http://www.82ask.com/

Office: +44 (0) 1223 309080
Mobile: +44 (0) 7740 857648
Email: paul at 82ask.com
MSN: paul at paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher
LinkedIn: https://www.linkedin.com/in/paulbutcher
-------------------------------------------------

82ASK: text any question to 82ASK (82275) and get the answer in minutes.
Visit www.82ask.com to get your first 2 questions answered for free.

 




More information about the mocha-developer mailing list