[mocha-developer] Automatically mocking ActiveRecord methods

James Moore jamesthepiper at gmail.com
Sun Mar 18 13:03:41 EDT 2007


Seems like I do a fair amount of mocking ActiveRecord's automatically
generated routines when I'm testing controllers.  Things like:

    @portalspace = PortalSpace.new :public_readable => true,
:public_writable => true, :name => 'public'
    @portalspace.stubs(:id).returns(203)
    PortalSpace.stubs(:find).with(203).returns(@portalspace)
    PortalSpace.stubs(:find).with('203').returns(@portalspace)
    PortalSpace.stubs(:find).with(:first, {:conditions =>
['portal_spaces.name = ?', 'public']}).returns(@portalspace)

Is there value in having an addon that says something like:

foo.stub_active_record

that would add some set of mocks/stubs for things like

foo.stub(:find).with(foo.id).return(foo)
foo.stub(:find).with(foo.id.to_s).return(foo)
foo.stub(:find_by_name).with(foo.name).return(foo)

etc.

 - James Moore


More information about the mocha-developer mailing list