如何使用 rspec 测试 sidekiq 执行方法返回

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

大家好,我不知道如何测试 sidekiq 工作执行方法返回另一个类方法的调用

我有这个 sedekiq 工人

module Payments
  module Bitso
    class ProcessPaymentWorker
      include Sidekiq::Worker
      sidekiq_options queue: 'default'
      def perform(options)
        Users::Payments::Bitso::Disperse.new(options).call
      end
    end
  end
end

我已经尝试过这个规范,但代码覆盖率被标记为未覆盖

Users::Payments::Bitso::Disperse.new(options).call

require 'rails_helper'
require 'sidekiq/testing'
Sidekiq::Testing.fake!

RSpec.describe Payments::Bitso::ProcessPaymentWorker, type: :worker do
  let(:options) { { foo: 'bar' } }

  describe 'worker' do
    it 'should enqueue jobs' do
      expect { described_class.perform_async(options) }.to change(described_class.jobs, :size).by(1)
    end
  end

  describe '#perform' do
    let(:disperse) { class_double(Users::Payments::Bitso::Disperse) }

    it 'should call disperse' do
      allow(described_class).to receive(:perform_async).with(options).and_return(disperse)
    end
  end
end

我应该如何测试

Users::Payments::Bitso::Disperse.new(options).call
将我的规格覆盖率标记为 100%?

ruby-on-rails rspec
1个回答
0
投票

测试中缺少断言 (

expect
)。另外,您正在测试实例方法,因此您可能需要一个
instance_double
。 像这样的东西应该有效。

  describe '#perform' do
    let(:disperse) { instance_double(Users::Payments::Bitso::Disperse) }

    it 'should call disperse' do
      expect(Users::Payments::Bitso::Disperse).to receive(:new).with(options).and_return(disperse)
      expect(disperse).to receive(:call)
      described_class.new.perform
    end
  end
© www.soinside.com 2019 - 2024. All rights reserved.