From: James Mead Date: 2006-07-17T18:34:51+09:00 Subject: Re: alias new??? David Chelimsky wrote: > On Jul 12, 2006, at 12:15 AM, Logan Capaldo wrote: > 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. > Sounds interesting. I've been working on something similar - a mocking library called Mocha (http://mocha.rubyforge.org). It's not specifically targetted at Rails controller tests, but I think the Stubba/AutoMocha parts might allow you to do what you want. Stubba allows you to mock or stub class methods. For AutoMocha, I alias the :new method in Mocha:MockClass (http://mocha.rubyforge.org/classes/Mocha/MockClass.html) and define a new version that delegates to method_missing which is at the heart of the mocking functionality. The inherited method then ensures that any subclasses get the original :new method back. James. -- Posted via http://www.ruby-forum.com/.