RSpec未定义方法`to_sym'

时间:2020-03-05 01:10:23

标签: ruby-on-rails ruby rspec

我的班级负责与我要测试的Jira公司董事会建立联系。

class

module Jira
  class JiraConnection
    URL = 'https://company_name.atlassian.net/'.freeze

    def call
      JIRA::Client.new(options)
    end

    private

    def options
      {
        username: ENV['USERNAME'],
        password: ENV['PASSWORD'],
        site: URL,
        context_path: '',
        auth_type: :basic,
        use_ssl: true
      }
    end
  end
end

JIRA::Client.new来自jira-ruby gem。我要测试

我的规格:

RSpec.describe Jira::JiraConnection, type: :service do
  subject(:connect) { described_class.new }

  let(:options) do
    {
      username: username_secret,
      password: password_secret,
      site: 'https://company_name.atlassian.net/',
      context_path: '',
      auth_type: :basic,
      use_ssl: true
    }
  end

  let(:username_secret) { ENV.fetch('USERNAME') }
  let(:password_secret) { ENV.fetch('PASSWORD') }

  before do
    allow(JIRA::Client).to receive(:new).with(options)
  end

  it 'connect to Jira API' do
    expect(subject.call).to receive(JIRA::Client)
  end
end

使用上述规格时,出现错误:

Failure/Error: expect(subject.call).to receive(JIRA::Client)

 NoMethodError:
   undefined method `to_sym' for JIRA::Client:Class
   Did you mean?  to_s

2 个答案:

答案 0 :(得分:0)

您正尝试使用expect(...).to receive API测试方法的返回值,该API用于测试方法是否被调用(或,用于存根方法)。

如果您要检查返回值是JIRA::Client的实例,则可以这样做:

expect(subject.call).to be_a(JIRA::Client)

或者,使用更基本的eq(等于)匹配器:

expect(subject.call.class).to eq(JIRA::Client)

答案 1 :(得分:0)

您可能想要的是:

describe '#call' do
  it 'initializes Jira API client with proper connection options' do
    expect(JIRA::Client).to receive(:new).with(options).once
    connect.call
  end

  it 'returns Jira API client' do
    expect(connect.call).to be_a(JIRA::Client)
  end
end
相关问题