RSpec未定义方法`to_sym'

问题描述 投票:1回答:1

我的班级负责与我要测试的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
ruby-on-rails ruby rspec
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.