[mocha-developer] Using any_instance with Rails to make sure the right thing is saved?

James Moore jamesthepiper at gmail.com
Tue Feb 6 12:45:17 EST 2007

I'd like to test that an object with the correct attributes is saved,
something like:

  def test_create_without_existing_suite
    this = self
    w = Annotation.any_instance.expects(:save).with do |x|
      this.assert_equal 'fnord',
    w.returns true

But the object is created inside the method being tested (in this case, a
test of a Rails controller's 'create' method), so
I don't think there's a way to just mock the method on the particular

Is there a way to get the target of the mocked method from inside

Or am I going about this the wrong way?  Is there a way to be notified when
new instances of a class are created, and then do something like
automatically mock some of their methods?

 - James Moore

More information about the mocha-developer mailing list