[mocha-developer] Object.stubs doesn't seem to work.

James Mead jamesmead44 at gmail.com
Mon Jul 30 10:27:09 EDT 2007

Hi Jonathan,

You need to use Mocha in the context of a Test::Unit::TestCase (or an RSpec

The blog post missed this out to improve the signal-to-noise ratio.

require 'rubygems'
require 'mocha'
require 'test/unit'

class TimeTest < Test::Unit::TestCase

  def test_time_now
    Time.stubs(:now).returns(Time.parse('Thu Feb 01 00:00:00 UTC 2007'))
    assert_equal Time.parse('Thu Feb 01 00:00:00 UTC 2007'), Time.now


The reason it needs to be within a TestCase or similar is because Mocha
temporarily modifies the Time class for the duration of the test and needs a
hook to put it back how it was.

I hope that makes sense.

More information about the mocha-developer mailing list