调用内部的存根方法的RSpec。

问题描述 投票:0回答:2

我正在尝试测试类,它负责创建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
ruby-on-rails ruby rspec
2个回答
0
投票

你只需要检查该方法是否在存根上被调用了。创建人_实例

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
© www.soinside.com 2019 - 2024. All rights reserved.