从第三方API引发异常

时间:2016-08-09 01:12:52

标签: ruby rspec

context 'given an invalid build name' do
  let(:config_file) { ('login.yml') }
  let(:client) { double(JenkinsApi::Client) }
  let(:logger_mock) { double("logger").as_null_object }
  let(:exception) { double(JenkinsApi::Exceptions::NotFound) }

  before do
    allow(YAML)
      .to receive(:load_file)
      .with('login.yml')
      .and_return(config_file)
    allow(JenkinsApi::Client)
      .to receive(:new).with(config_file).and_return(client) 
    allow(client).to receive_message_chain(:job, :status) {'build'}
    allow(client.job).to receive(JenkinsApi::Exceptions::NotFound).and_return(exception)
  end

  it 'should throw NotFound error' do
    expect{client.job.status('bad_build')}.to raise_error
  end

为了测试,我希望Jenkins API引发JenkinsApi :: Exceptions :: NotFound。我不想ping实际服务器进行测试,而是使用mocking来引发异常。我尝试了很多变化,但无法成功调用它。

任何帮助都将不胜感激。

Failure/Error: allow(client.job).to  
  receive(JenkinsApi::Exceptions::NotFound).and_return(exception)

NoMethodError:
   undefined method `to_sym' for JenkinsApi::Exceptions::NotFound:Class
   Did you mean?  to_s

这是我收到的当前错误。我也一直在收到关于没有实际提出异常的错误。

0 个答案:

没有答案