Brian Ploetz
2010-Aug-26 03:13 UTC
[rspec-users] should raise_error(ArgumentError) resulting in NoMethodError
Environment ------------------ ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0] rspec (1.3.0) Code ------- def currency_to_dollars(currency_amount) raise ArgumentError("Currency amount can''t be nil") if currency_amount.nil? end Spec -------- it "should raise an ArgumentError if currency_amount is nil" do lambda { @service.currency_to_dollars(nil) }.should raise_error(ArgumentError) end Results in this failure: 1) ''Service should raise an ArgumentError if currency_amount is nil'' FAILED expected ArgumentError, got #<NoMethodError: undefined method `ArgumentError'' for #<Service:0x0000010087e5f0>> test/spec/service_spec.rb:92:in `block (2 levels) in <top (required)>'' Changing the test to either of these two variants allows the the to pass: lambda { @service.currency_to_dollars(nil) }.should raise_error(StandardError) lambda { @service.currency_to_dollars(nil) }.should raise_error Why am I unable to test for a specific StandardError subclass? Thanks in advance for any help. BP -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100825/4b96c5f4/attachment.html>
Brian Ploetz
2010-Aug-26 03:25 UTC
[rspec-users] should raise_error(ArgumentError) resulting in NoMethodError
FYI, the same thing happens with rspec 2.0.0.beta.20 as well: 1) Service should raise an ArgumentError if currency_amount is nil Failure/Error: lambda { @service.currency_to_dollars(nil) }.should raise_error(ArgumentError) expected ArgumentError, got #<NoMethodError: undefined method `ArgumentError'' for #<Service:0x000001029665d8>> # ./test/spec/service_spec.rb:92:in `block (2 levels) in <top (required)>'' On Aug 25, 11:13?pm, Brian Ploetz <bplo... at gmail.com> wrote:> Environment > ------------------ > ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0] > rspec (1.3.0) > > Code > ------- > ? ? def currency_to_dollars(currency_amount) > ? ? ? raise ArgumentError("Currency amount can''t be nil") if > currency_amount.nil? > ? ? end > > Spec > -------- > ? ? it "should raise an ArgumentError if currency_amount is nil" do > ? ? ? lambda { @service.currency_to_dollars(nil) }.should > raise_error(ArgumentError) > ? ? end > > Results in this failure: > 1) > ''Service should raise an ArgumentError if currency_amount is nil'' FAILED > expected ArgumentError, got #<NoMethodError: undefined method > `ArgumentError'' for #<Service:0x0000010087e5f0>> > test/spec/service_spec.rb:92:in `block (2 levels) in <top (required)>'' > > Changing the test to either of these two variants allows the the to pass: > > ? ? lambda { @service.currency_to_dollars(nil) }.should > raise_error(StandardError) > ? ? lambda { @service.currency_to_dollars(nil) }.should raise_error > > Why am I unable to test for a specific StandardError subclass? > > Thanks in advance for any help. > BP > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2010-Aug-26 04:14 UTC
[rspec-users] should raise_error(ArgumentError) resulting in NoMethodError
On Aug 25, 2010, at 10:13 PM, Brian Ploetz wrote:> Environment > ------------------ > ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0] > rspec (1.3.0) > > Code > ------- > def currency_to_dollars(currency_amount) > raise ArgumentError("Currency amount can''t be nil") if currency_amount.nil? > end > > Spec > -------- > it "should raise an ArgumentError if currency_amount is nil" do > lambda { @service.currency_to_dollars(nil) }.should raise_error(ArgumentError) > end > > Results in this failure: > 1) > ''Service should raise an ArgumentError if currency_amount is nil'' FAILED > expected ArgumentError, got #<NoMethodError: undefined method `ArgumentError'' for #<Service:0x0000010087e5f0>>This message is telling you there is no ArgumentError method, not that the constant ArgumentError is missing. The method needs to be (adding ".new"): def currency_to_dollars(currency_amount) raise ArgumentError.new("Currency amount can''t be nil") if currency_amount.nil? end> test/spec/service_spec.rb:92:in `block (2 levels) in <top (required)>'' > > > Changing the test to either of these two variants allows the the to pass: > > lambda { @service.currency_to_dollars(nil) }.should raise_error(StandardError) > lambda { @service.currency_to_dollars(nil) }.should raise_errorThese pass because NoMethodError, which is what is being thrown, is a subclass of StandardError and Exception. HTH, David
Rob Biedenharn
2010-Aug-26 04:35 UTC
[rspec-users] should raise_error(ArgumentError) resulting in NoMethodError
On Aug 26, 2010, at 12:14 AM, David Chelimsky wrote:> On Aug 25, 2010, at 10:13 PM, Brian Ploetz wrote: >> Environment >> ------------------ >> ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0] >> rspec (1.3.0) >> >> Code >> ------- >> def currency_to_dollars(currency_amount) >> raise ArgumentError("Currency amount can''t be nil") if >> currency_amount.nil? >> end >> >> Spec >> -------- >> it "should raise an ArgumentError if currency_amount is nil" do >> lambda { @service.currency_to_dollars(nil) }.should >> raise_error(ArgumentError) >> end >> >> Results in this failure: >> 1) >> ''Service should raise an ArgumentError if currency_amount is nil'' >> FAILED >> expected ArgumentError, got #<NoMethodError: undefined method >> `ArgumentError'' for #<Service:0x0000010087e5f0>> > > This message is telling you there is no ArgumentError method, not > that the constant ArgumentError is missing. The method needs to be > (adding ".new"): > > def currency_to_dollars(currency_amount) > raise ArgumentError.new("Currency amount can''t be nil") if > currency_amount.nil? > endYou might see this form, too. def currency_to_dollars(currency_amount) raise ArgumentError, "Currency amount can''t be nil" if currency_amount.nil? end Note the comma that separates the arguments to raise (Kernel#raise). -Rob> >> test/spec/service_spec.rb:92:in `block (2 levels) in <top >> (required)>'' >> >> >> Changing the test to either of these two variants allows the the to >> pass: >> >> lambda { @service.currency_to_dollars(nil) }.should >> raise_error(StandardError) >> lambda { @service.currency_to_dollars(nil) }.should raise_error > > These pass because NoMethodError, which is what is being thrown, is > a subclass of StandardError and Exception. > > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersRob Biedenharn Rob at AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab at GaslightSoftware.com http://GaslightSoftware.com/
Brian Ploetz
2010-Aug-26 13:19 UTC
[rspec-users] should raise_error(ArgumentError) resulting in NoMethodError
*smacks head* Duh. Thanks David and Rob! On Aug 26, 12:35?am, Rob Biedenharn <R... at AgileConsultingLLC.com> wrote:> On Aug 26, 2010, at 12:14 AM, David Chelimsky wrote: > > > > > On Aug 25, 2010, at 10:13 PM, Brian Ploetz wrote: > >> Environment > >> ------------------ > >> ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0] > >> rspec (1.3.0) > > >> Code > >> ------- > >> ? ?def currency_to_dollars(currency_amount) > >> ? ? ?raise ArgumentError("Currency amount can''t be nil") if ? > >> currency_amount.nil? > >> ? ?end > > >> Spec > >> -------- > >> ? ?it "should raise an ArgumentError if currency_amount is nil" do > >> ? ? ?lambda { @service.currency_to_dollars(nil) }.should ? > >> raise_error(ArgumentError) > >> ? ?end > > >> Results in this failure: > >> 1) > >> ''Service should raise an ArgumentError if currency_amount is nil'' ? > >> FAILED > >> expected ArgumentError, got #<NoMethodError: undefined method ? > >> `ArgumentError'' for #<Service:0x0000010087e5f0>> > > > This message is telling you there is no ArgumentError method, not ? > > that the constant ArgumentError is missing. The method needs to be ? > > (adding ".new"): > > > ? ?def currency_to_dollars(currency_amount) > > ? ? ?raise ArgumentError.new("Currency amount can''t be nil") if ? > > currency_amount.nil? > > ? ?end > > You might see this form, too. > > ? ? def currency_to_dollars(currency_amount) > ? ? ? raise ArgumentError, "Currency amount can''t be nil" if ? > currency_amount.nil? > ? ? end > > Note the comma that separates the arguments to raise (Kernel#raise). > > -Rob > > > > > > >> test/spec/service_spec.rb:92:in `block (2 levels) in <top ? > >> (required)>'' > > >> Changing the test to either of these two variants allows the the to ? > >> pass: > > >> ? ?lambda { @service.currency_to_dollars(nil) }.should ? > >> raise_error(StandardError) > >> ? ?lambda { @service.currency_to_dollars(nil) }.should raise_error > > > These pass because NoMethodError, which is what is being thrown, is ? > > a subclass of StandardError and Exception. > > > HTH, > > David > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > Rob Biedenharn ? ? ? ? ? > R... at AgileConsultingLLC.com ?http://AgileConsultingLLC.com/ > r... at GaslightSoftware.com ? ? ? ? ? ?http://GaslightSoftware.com/ > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users