[mocha-developer] Using any_instance with Rails to make sure the right thing is saved?
jamesmead44 at gmail.com
Tue Feb 6 13:57:05 EST 2007
On 06/02/07, James Moore <jamesthepiper at gmail.com> wrote:
> 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
> new instances of a class are created, and then do something like
> automatically mock some of their methods?
You can stub the 'new' or 'create' methods of a class and return a mock
So you'd have something along these lines in your test...
annotation = mock('annotation')
post :create, :name => 'wibble'
More information about the mocha-developer