我正在嘗試編寫一個簡單的rspec測試,用於引發錯誤異常。我如何編寫期望值以便將錯誤消息ALREADY_A_BITLY_LINK - '500'
與raise_error(BitlyError)
相匹配。這rspec實際上通過。但是,如果我將相同的raise_error用於無效的URL。它也會通過。我如何測試特定的異常消息?bitly引發錯誤異常rspec測試
bitly_spec.rb
require 'rails_helper'
describe Bitly do
before do
@bitly = Bitly.new(username, api_key)
end
let(:bitly_url) { 'bitly_url' } #stackoverflow doesn't allow URL shortener
it 'should return exception error if given url is bitly' do
expect { @bitly.shorten(bitly_url) }.to raise_error(BitlyError)
end
end
調試
@bitly.shorten(bitly_url)
回報*** BitlyError Exception: ALREADY_A_BITLY_LINK - '500'
raise_error(BitlyError)
回報#<RSpec::Matchers::BuiltIn::RaiseError:0x007fa229c56f48 @block=nil, @actual_error=nil, @warn_about_bare_error=false, @expected_error=BitlyError, @expected_message=nil>