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, or contact
sup...@openexchangerates.org.
# ./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.