I''ve come across rather strange behaviour when trying to raise an exception in a stubbed method. I''m speccing the behaviour of a Rails create action, where I''m using save! to catch failed saves. In the case of working save, I''m using the following stub: @client.stub!(:save!).and_return(true) which works fine. However, in the negative case, @client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid) The save! call in the controller doesn''t seem to work. I get the following error: ArgumentError in ''ClientsController POST /clients with invalid parameters should show new form again'' wrong number of arguments (0 for 1) /Users/jarkko/Sites/koulutusweb/app/controllers/clients_controller.rb: 41:in `create'' The line #41 consists only of @client.save! I''m at a loss seeing where the wrong number of arguments is really happening because save! certainly shouldn''t assume any args. Moreover, both cases work fine in the real app, so it seems to me something funky is happening when stubbing the method. Anyone else stumbled upon anything similar? Cheers, //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2417 bytes Desc: not available Url : http://rubyforge.org/pipermail/rspec-users/attachments/20070913/a7dcc021/attachment-0001.bin
aslak hellesoy
2007-Sep-13 20:29 UTC
[rspec-users] Failing to raise an exception in a stub
On 9/13/07, Jarkko Laine <jarkko at jlaine.net> wrote:> I''ve come across rather strange behaviour when trying to raise an > exception in a stubbed method. > > I''m speccing the behaviour of a Rails create action, where I''m using > save! to catch failed saves. In the case of working save, I''m using > the following stub: > > @client.stub!(:save!).and_return(true) > > which works fine. > > However, in the negative case, > > @client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid) >Passing a class only works if the new method takes 0 args. Otherwise you have to pass an exception instance. ActiveRecord::RecordInvalid takes one. @client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid.new(@client))> The save! call in the controller doesn''t seem to work. I get the > following error: > > ArgumentError in ''ClientsController POST /clients with invalid > parameters should show new form again'' > wrong number of arguments (0 for 1) > /Users/jarkko/Sites/koulutusweb/app/controllers/clients_controller.rb: > 41:in `create'' > > The line #41 consists only of > > @client.save! > > I''m at a loss seeing where the wrong number of arguments is really > happening because save! certainly shouldn''t assume any args. > Moreover, both cases work fine in the real app, so it seems to me > something funky is happening when stubbing the method. > > Anyone else stumbled upon anything similar? > > Cheers, > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > >
On 13.9.2007, at 23.29, aslak hellesoy wrote:> On 9/13/07, Jarkko Laine <jarkko at jlaine.net> wrote: >> I''ve come across rather strange behaviour when trying to raise an >> exception in a stubbed method. >> >> I''m speccing the behaviour of a Rails create action, where I''m using >> save! to catch failed saves. In the case of working save, I''m using >> the following stub: >> >> @client.stub!(:save!).and_return(true) >> >> which works fine. >> >> However, in the negative case, >> >> @client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid) >> > > Passing a class only works if the new method takes 0 args. Otherwise > you have to pass an exception instance. ActiveRecord::RecordInvalid > takes one. > > @client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid.new > (@client))Cheers, that was it! //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2417 bytes Desc: not available Url : http://rubyforge.org/pipermail/rspec-users/attachments/20070914/3cfd2a61/attachment.bin