Of course, I can’t believe I forgot about that. I was messing around so much with stubbing methods that I didn’t notice that.
> On Aug 24, 2020, at 12:50 PM, Phil Pirozhkov <[email protected]> wrote: > > You should be using block syntax if you expect a certain block of code to > raise an exception. > ``` > expect { described_class.load_one_date(@date) }.to raise_exception > RuntimeError > ``` > https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/built-in-matchers/raise-error-matcher > > <https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/built-in-matchers/raise-error-matcher> > > `raise_exception` is not really compatible with the value expectations, but > there is no warning. > We are going to avoid that kind of mistakes in the next major release. > > - Phil > > On Mon, Aug 24, 2020 at 1:35 AM Jack Royal-Gordon <[email protected] > <mailto:[email protected]>> wrote: > I’ve got a spec that is meant in part to test error handling within a public > class method, and it stubs out a lower-level method that I don’t want to test > in this instance. The partial spec looks like: > > it 'reacts appropriately to an error from get_json' do > expect(described_class).to > receive(:get_json).with("historical/#{@date.strftime('%F')}.json") \ > .and_return(@invalid_rates) > expect(described_class.load_one_date(@date)).to raise_exception > RuntimeError > end > > The output from running the spec looks like: > > 1) CurrencyConversion.load_one_date(date, include_currencies=nil) reacts > appropriately to an error from get_json > Failure/Error: expect(described_class.load_one_date(@date)).to > raise_exception RuntimeError > RuntimeError: > Error from OpenExchangeRates when calling with date Sun, 23 Aug 2020: > Invalid App ID provided - please sign up at > https://openexchangerates.org/signup <https://openexchangerates.org/signup>, > or contact [email protected] > <mailto:[email protected]>. > # ./app/models/currency_conversion.rb:100:in `load_one_date' > # ./spec/models/currency_conversion_spec.rb:133:in `block (3 levels) in > <top (required)>’ > > So, I’m expecting a RuntimeError, I get a RuntimeError, and yet the spec > fails. The referenced line in currency_conversion.rb is the “raise” command, > and the referenced line in currency_conversion_spec.rb is the second > “expect”. I tried a couple variations (change the first “expect” to “allow” > and dropping the “RuntimeError” from the “raise_exception”, with no change to > the behavior. > > -- > 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] > <mailto:rspec%[email protected]>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/6BFF6A54-7F11-4028-B2EB-84759DF3ACE0%40pobox.com > > <https://groups.google.com/d/msgid/rspec/6BFF6A54-7F11-4028-B2EB-84759DF3ACE0%40pobox.com>. > > -- > 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] > <mailto:[email protected]>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/CAAk5Ok-ezhuH1tuAdSjjVF-KRH4bV-zwHv%2BRWiuu6_HfrUYiXA%40mail.gmail.com > > <https://groups.google.com/d/msgid/rspec/CAAk5Ok-ezhuH1tuAdSjjVF-KRH4bV-zwHv%2BRWiuu6_HfrUYiXA%40mail.gmail.com?utm_medium=email&utm_source=footer>. -- 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 view this discussion on the web visit https://groups.google.com/d/msgid/rspec/202B734E-A163-4C17-B884-B45E8E024AE1%40pobox.com.
