David Chelimsky
7/12/2006 10:56:00 AM
On Jul 12, 2006, at 12:15 AM, Logan Capaldo wrote:
> On Jul 12, 2006, at 1:01 AM, ara.t.howard@noaa.gov wrote:
>> On Wed, 12 Jul 2006, Logan Capaldo wrote:
>>>
>>> module MagicWithNew
>>> def new(*args, &block)
>>> if some_condition
>>> do_special_stuff
>>
>> fails to initialize object! you need the old 'new' here. or
>> super.
>>
> Well he did say "special stuff". He only needs old new or super
> there if he plans on initializing the object ;)
Yes. I'm writing an acts_as_mock plugin for rails controller tests.
Since activerecord relies on a bunch of class methods, I need to be
able to mock them as well so I can write tests like this:
def test_blah
expected = Story.new
Story.should_receive(:new).and_return(expected)
expected.should_receive(:some_message)
get :blah
end
This (in theory) will allow us to run controller (functional) tests
in isolation (i.e. no DB necessary), and therefore much faster.
In the example above, the first call to :new gets handled by super.
The second line tells the class that the next time :new is called, it
should return this particular object (expected). After that, you can
set expectations on expected.
Thanks to your help here, this is working like a charm!
I've got a lot more work to do on this to make it even alpha-release
worthy, but I'll post a note here when I get there.
Cheers,
David