Hi Carlos You need to stub a method within your definition of call that will raise an error, like Myron points out currently you are overwriting your definition and so the rescue has no effect.
Cheers Jon On Sun, 10 Jun 2018 at 16:42, Myron Marston <[email protected]> wrote: > Hi Carlos, > > It’s not clear to me what you’re trying to accomplish with that example. > Meep#call has a rescue clause but the code being rescued involves no > method calls ("no error" is not a method call) and, barring something > truly bizarre like a busted ruby installation, cannot raise any sort of > exception. As there’s no possibility of an ArgumentError being raised, > there’s no point in rescuing it (or in trying to write a test to cover the > possibility of an exception being raised). > > In addition, allow_any_instance_of(Meep).to > receive(:call).and_raise(ArgumentError) completely replaces your existing > implementation of Meep#call including the rescue ArgumentError. > > I’m not sure what to suggest since your example doesn’t make sense to me. > > Myron > > > On Mon, Jun 4, 2018 at 4:16 PM, Carlos Rodriguez <[email protected]> > wrote: > >> Hello folks, >> >> I'm trying to test the return value of a method after an exception is >> called and I'm unsure of how to actually test this. I basically want to >> throw away the exception to see the return value. See the super contrived >> example below: >> >> class Foo >> def call >> Bar.new.call >> end >> end >> >> class Bar >> def call >> Meep.new.call >> end >> end >> >> class Meep >> def call >> "no error" >> rescue ArgumentError >> "error" >> end >> end >> >> allow_any_instance_of(Meep).to receive(:call).and_raise(ArgumentError) >> expect(Foo.new.call).to eq "error" >> >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "rspec" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/rspec/9a05e52f-21c5-49bf-808b-e826261f9936%40googlegroups.com >> <https://groups.google.com/d/msgid/rspec/9a05e52f-21c5-49bf-808b-e826261f9936%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- > You received this message because you are subscribed to the Google Groups > "rspec" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/CADUxQmuwGYSNOU8uQsVLT9waGCkcQK8mZ7Nb_RPzv-iihKLTSg%40mail.gmail.com > <https://groups.google.com/d/msgid/rspec/CADUxQmuwGYSNOU8uQsVLT9waGCkcQK8mZ7Nb_RPzv-iihKLTSg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CAEGxe4uCNQoAy6Tqq%2BX2nKQ1Wq_Gp%3D5sTaBpWjOGE%2BLmHpVNkg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
