Is there a way to do ModelName.create without triggering the various callbacks that would normally be triggered? I''m in an rspec test and want to save a model without worrying about the callback stuff. thanks max -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser wrote:> Max Williams wrote: >> Is there a way to do ModelName.create without triggering the various >> callbacks that would normally be triggered? > > Yes; check the AR docs. I believe save is the function you''re looking > forThanks for your advice. I have access to the AR docs, but doing new then save triggers the same callbacks as calling create.> >> I''m in an rspec test and >> want to save a model without worrying about the callback stuff. > > Why? If you''re testing your app, you should have the callbacks work > just like in the real thing.But i''m not testing the model in question, it''s just an associated model for the model i am testing. The callbacks are not relevant to these tests at all.> > If you''re just trying to cobble together some test data, consider using > factories, probably with Machinist.Yeah, i know, i have used those before, it just seems like overkill for this case. -- Posted via http://www.ruby-forum.com/.
Max Williams wrote: [...]>> for > Thanks for your advice. I have access to the AR docs, but doing new > then save triggers the same callbacks as calling create.Then maybe it''s something else, or maybe I''m thinking of just bypassing validatons...sorry to have steered you wrong!> >> >>> I''m in an rspec test and >>> want to save a model without worrying about the callback stuff. >> >> Why? If you''re testing your app, you should have the callbacks work >> just like in the real thing. > But i''m not testing the model in question, it''s just an associated model > for the model i am testing. The callbacks are not relevant to these > tests at all.That makes some sense.>> >> If you''re just trying to cobble together some test data, consider using >> factories, probably with Machinist. > Yeah, i know, i have used those before, it just seems like overkill for > this case.It''s not overkill. It''s the exact situation for which factories are the perfect solution. If you''ve got your blueprints set up right, all you need is one more .make call. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org
Marnen Laibow-Koser wrote:>>> If you''re just trying to cobble together some test data, consider using >>> factories, probably with Machinist. >> Yeah, i know, i have used those before, it just seems like overkill for >> this case. > > It''s not overkill. It''s the exact situation for which factories are > the perfect solution. If you''ve got your blueprints set up right, all > you need is one more .make call.Yeah...that was in a different project though :) I know it''s wrong, but i just ended up stubbing the methods called by the callbacks. cheers max -- Posted via http://www.ruby-forum.com/.
On Tue, Jun 30, 2009 at 7:11 PM, Max Williams<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Marnen Laibow-Koser wrote: > >>>> If you''re just trying to cobble together some test data, consider using >>>> factories, probably with Machinist. >>> Yeah, i know, i have used those before, it just seems like overkill for >>> this case. >> >> It''s not overkill. It''s the exact situation for which factories are >> the perfect solution. If you''ve got your blueprints set up right, all >> you need is one more .make call. > > Yeah...that was in a different project though :) I know it''s wrong, but > i just ended up stubbing the methods called by the callbacks. > > cheers > max >This plugin helps avoid call backs: http://github.com/cjbottaro/without_callbacks/tree/master Andrew Timberlake http://ramblingsonrails.com http://MyMvelope.com - The SIMPLE way to manage your savings
Max Williams wrote: [...]> I know it''s wrong,Then don''t do it. That way lies Bad Code.> but > i just ended up stubbing the methods called by the callbacks.That''s probably OK, although I''d tend to make life easier and use mock_model.> > cheers > maxBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.