Eivind Uggedal
2007-Aug-07 23:55 UTC
[rspec-users] Problems with raising errors on a mocked object
I''m trying to mock a object to raise a certain error. By doing so I get a ArgumentError on ActiveRecord''s save! method: http://pastie.caboo.se/85628 I''ve tried to debug it but just can''t seem to find what I''m doing wrong. Any help is greatly appreciated. Cheers, Eivind Uggedal
David Chelimsky
2007-Aug-08 03:30 UTC
[rspec-users] Problems with raising errors on a mocked object
On 8/7/07, Eivind Uggedal <eivindu at ifi.uio.no> wrote:> I''m trying to mock a object to raise a certain error. By doing so I > get a ArgumentError on ActiveRecord''s save! method: > > http://pastie.caboo.se/85628 > > I''ve tried to debug it but just can''t seem to find what I''m doing > wrong. Any help is greatly appreciated.Please run this: ruby script/spec ./spec/controllers/users_controller_spec.rb -b That will give the complete backtrace for the failure, which I''d ask you to pastie as well.> > Cheers, > Eivind Uggedal > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Eivind Uggedal
2007-Aug-08 05:44 UTC
[rspec-users] Problems with raising errors on a mocked object
Here you go: http://pastie.caboo.se/85876 Eivind On 8/8/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 8/7/07, Eivind Uggedal <eivindu at ifi.uio.no> wrote: > > I''m trying to mock a object to raise a certain error. By doing so I > > get a ArgumentError on ActiveRecord''s save! method: > > > > http://pastie.caboo.se/85628 > > > > I''ve tried to debug it but just can''t seem to find what I''m doing > > wrong. Any help is greatly appreciated. > > Please run this: > > ruby script/spec ./spec/controllers/users_controller_spec.rb -b > > That will give the complete backtrace for the failure, which I''d ask > you to pastie as well. > > > > > Cheers, > > Eivind Uggedal > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2007-Aug-08 06:39 UTC
[rspec-users] Problems with raising errors on a mocked object
On 8/8/07, Eivind Uggedal <eivindu at ifi.uio.no> wrote:> Here you go: http://pastie.caboo.se/85876OK - that helped. The and_raise method can take an exception class or object. If you pass it the class, it will try to create an instance of it using Klass.new. This is the source of the problem. In your example we see: @user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid) Rspec tries to call ActiveRecord::RecordInvalid.new, but that requires an argument, which is why you get an error saying it got 0 for 1 arguments. Because and_raise can also take an exception object, the solution to this problem is: @user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(@user)) I guess the root of the problem is poor docs - I''m updating the rdoc now and it will be published with the next release. Cheers, David> > Eivind > > On 8/8/07, David Chelimsky <dchelimsky at gmail.com> wrote: > > On 8/7/07, Eivind Uggedal <eivindu at ifi.uio.no> wrote: > > > I''m trying to mock a object to raise a certain error. By doing so I > > > get a ArgumentError on ActiveRecord''s save! method: > > > > > > http://pastie.caboo.se/85628 > > > > > > I''ve tried to debug it but just can''t seem to find what I''m doing > > > wrong. Any help is greatly appreciated. > > > > Please run this: > > > > ruby script/spec ./spec/controllers/users_controller_spec.rb -b > > > > That will give the complete backtrace for the failure, which I''d ask > > you to pastie as well. > > > > > > > > Cheers, > > > Eivind Uggedal > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Eivind Uggedal
2007-Aug-08 07:26 UTC
[rspec-users] Problems with raising errors on a mocked object
Thanks, that worked great after I had mocked out the ActiveRecord::Errors::full_messages: http://pastie.caboo.se/85887 Cheers, Eivind Uggedal On 8/8/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 8/8/07, Eivind Uggedal <eivindu at ifi.uio.no> wrote: > > Here you go: http://pastie.caboo.se/85876 > > OK - that helped. > > The and_raise method can take an exception class or object. If you > pass it the class, it will try to create an instance of it using > Klass.new. This is the source of the problem. In your example we see: > > @user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid) > > Rspec tries to call ActiveRecord::RecordInvalid.new, but that requires > an argument, which is why you get an error saying it got 0 for 1 > arguments. > > Because and_raise can also take an exception object, the solution to > this problem is: > > @user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(@user)) > > I guess the root of the problem is poor docs - I''m updating the rdoc > now and it will be published with the next release. > > Cheers, > David > > > > > > Eivind > > > > On 8/8/07, David Chelimsky <dchelimsky at gmail.com> wrote: > > > On 8/7/07, Eivind Uggedal <eivindu at ifi.uio.no> wrote: > > > > I''m trying to mock a object to raise a certain error. By doing so I > > > > get a ArgumentError on ActiveRecord''s save! method: > > > > > > > > http://pastie.caboo.se/85628 > > > > > > > > I''ve tried to debug it but just can''t seem to find what I''m doing > > > > wrong. Any help is greatly appreciated. > > > > > > Please run this: > > > > > > ruby script/spec ./spec/controllers/users_controller_spec.rb -b > > > > > > That will give the complete backtrace for the failure, which I''d ask > > > you to pastie as well. > > > > > > > > > > > Cheers, > > > > Eivind Uggedal > > > > _______________________________________________ > > > > rspec-users mailing list > > > > rspec-users at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >