无法模拟第三方api客户端连接工具

时间:2016-08-06 01:40:19

标签: ruby rspec

describe ".valid_jira_build?" do

  context 'given valid build name' do
    let(:configd_client) { ('login.yml') }
    let(:client) { double(configd_client) }

    before do
     allow(YAML)
     .to receive(:load_file)
     .with('login.yml')
     .and_return(configd_client)

   allow(JenkinsApi::Client)
            .to receive(:new).with(configd_client)
            .and_return(client)
   allow(client).to receive_message_chain(:job, :status) {'success'}
 end

 it 'will return true' do
  client.stub(:job)
  expect(JenkinsClient.new('build').valid_jira_build?).to be_true
 end
end

我正在尝试模仿JenkinsApi :: Client :: Job.get_latest_build_status,我尝试过很多方法来模拟这个,但我是RSpec的新手并且遇到了很多问题。我不希望Client对象与服务器联系,但未能正确模拟它以防止这种情况发生。

任何帮助将不胜感激。从此版本收到的当前错误是:

1) JenkinsClient.valid_jira_build? given valid build name will return true
      Failure/Error: latest_build_status = client.job.status
                                       ("#{jira_build_to_check}")
 NoMethodError:
       undefined method `status' for nil:NilClass
     # ./lib/jenkins_client.rb:59:in `valid_jira_build?'
     # ./lib/jenkins_client.rb:30:in `initialize'
     # ./spec/jenkins_client_spec.rb:31:in `new'
     # ./spec/jenkins_client_spec.rb:31:in `block (4 levels) in <top (required)>'

1 个答案:

答案 0 :(得分:0)

更像这样的事情:

  let(:client) { double(JenkinsApi::Client) }
  allow(client).to receive_message_chain(:job, :status) {'success'}
  allow(JenkinsApi::Client)
    .to receive(:new).and_return(client)

您是否关心测试您是否正在提取login.yml文件?如果是这样,您希望在client内添加更多双打。

我认为,问题在于,您的client.stub(:job)正在覆盖实际的客户端消息链。所以这条线也应该被抛弃。