RSpec undefined method `to_sym'

Viewed 756

My class is responsible for connection with the Jira company board which I want to test it.

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 comes from jira-ruby gem. I want to test it

my specs:

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

With above specs I'm getting an error:

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

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

You're trying to test the return value of a method but using the expect(...).to receive API, which is for testing that a method was called (or, for stubbing a method).

If you want to check that the return value is an instance of JIRA::Client, you could do it like so:

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

Alternatively, using the more basic eq (equal) matcher:

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

What you probably want instead:

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
Related