RSpe c для заглушенного метода внутри вызова - PullRequest
0 голосов
/ 22 апреля 2020

Я пробую тестовый класс, который отвечает за создание билетов Jira. Я хочу заблокировать метод create_issue, который находится внутри вызова метода

module JiraTickets
  class Creator
    def initialize(webhook)
      @webhook = webhook
    end

    def call
      create_issue(support_ticket_class, webhook)
    end

    private

    def client
      @client ||= JIRA::Client.new
    end

    def support_ticket_class
      @support_ticket_class ||= "SupportBoard::Issues::#{webhook.action_type_class}".constantize
    end

    def create_issue(support_ticket_class, webhook)
      issue = client.Issue.build
      issue.save(support_ticket_class.new(webhook).call)
    end

    def fields
      {
        'fields' => {
          'summary' => 'example.rb',
          'project' => { 'id' => '11' },
          'issuetype' => { 'id' => '3' }
          }
      }
    end
  end
end

Метод create_issue должен вернуть true. Итак, я сделал спецификации:

RSpec.describe JiraTickets::Creator do
  describe '#call' do
    subject { described_class.new(webhook).call }

    let(:webhook) { GithubApi::Webhook.new(webhook_hash, 'repository') }
    let(:webhook_hash) { { repository: { name: 'Test-repo' }, action: 'publicized' } }
    let(:creator_instance) { instance_double(JiraTickets::Creator) }

    before do
      allow(described_class).to receive(:new).with(webhook).and_return(creator_instance)
      allow(creator_instance).to receive(:call).and_return(true)
    end

    context 'when webhook class is supported' do
      it 'expect to create Jira ticket' do
        expect(subject).to receive(:call)
      end
    end
  end
end

Но я получаю сообщение об ошибке:

 Failure/Error: expect(subject).to receive(:call)
   true does not implement: call

1 Ответ

0 голосов
/ 22 апреля 2020

Вам просто нужно проверить, что метод был вызван на заглушке creator_instance

RSpec.describe JiraTickets::Creator do
  describe '#call' do
    subject { described_class.new(webhook) }

    let(:webhook) { GithubApi::Webhook.new(webhook_hash, 'repository') }
    let(:webhook_hash) { { repository: { name: 'Test-repo' }, action: 'publicized' } }

    before do
      allow_any_instance_of(described_class).to receive(:create_issue).with(any_args).and_return(true)
    end

    context 'when webhook class is supported' do
      it 'expects to create Jira ticket' do
        expect(subject.call).to eq(true)
      end
    end
  end
end
...