rspec webmock to_raise 错误未引发错误

rspec webmock to_raise error not raising error

我有以下代码片段并进行测试

class Service
  def self.status(base_uri = 'localhost', basic_auth = {})
    base_uri = "#{ base_uri }/status"
    response = HTTParty.get(base_uri, basic_auth: basic_auth)
  rescue StandardError => e
    binding.pry
    raise "Error occured code: #{ response.code } msg: #{ e.message }"
  end
end
it 'should return error msg if url is not valid' do
  bad_url = "http://bar_url.com"
  expected = 'Error occured'
  stub_request(:get, bad_url).
            with(headers: {'Authorization'=>'Basic YWRtaW46YWRtaW4='}).
            to_raise(StandardError)

  expect { Service.status(bad_url, { username: @username, password: @password }) }.to raise_error(/#{ expected }/) 
end 

此代码基于 documentation
结果 rspec 总是引发异常:

expected /Error occured/, got #<WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: GET h...rization'=>'Basic YWRtaW46YWRtaW4='})

谁能帮我弄清楚我应该改变什么才能得到 StandardError 结果?

由于服务调用 "#{ base_uri }/status"http://bar_url.com/statusstub_request 仅使用 http://bar_url.com 声明,因此 Net::HTTP.Get 查找与这而不是 http://bar_url/status.
这就是为什么测试没有报错的原因。