0

我使用https://github.com/mperham/sidekiq/wiki/Testing中的假测试为 sidekiq 测试设置了以下测试设置。

spec_helper.rb

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

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.before(:suite) do
    FactoryGirl.reload
    FactoryGirl.define do
      to_create { |instance| instance.save }
    end
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
    Airbrake.configure do |c|
      c.project_id = ENV['AIRBRAKE_PROJECT_ID']
      c.project_key = ENV['AIRBRAKE_PROJECT_KEY']
    end
  end

  config.before(:each, job: true) do
    Sidekiq::Worker.clear_all #make sure jobs don't linger between tests
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

  config.include FactoryGirl::Syntax::Methods
end

通知规范.rb

require 'spec_helper'

RSpec.describe NotificationWorker do
  it "perform should call Airbrake#notify", job: true do
    notification_worker = NotificationWorker.new
    message = "This is your error message"
    expect { notification_worker.perform(message) }.to change(NotificationWorker.jobs, :size).by(1)
  end
end

notification_worker.rb

class NotificationWorker
  include Sidekiq::Worker
  sidekiq_options queue: :high

  def perform(message)
    Airbrake.notify(message)
  end
end

然而,为什么我会收到以下错误消息:

Failure/Error: expect { notification_worker.perform(message) }.to change(NotificationWorker.jobs, :size).by(1)
       expected #size to have changed by 1, but was changed by 0

似乎作业数组应该增加 1。这是怎么回事?是不是 RSpec 和 Database Cleaner 交互导致的线程问题?

4

1 回答 1

2

因为它是直接调用 perform 方法而不是 Sidekiq 的 API。

尝试NotificationWorker.perform_async(message)

于 2016-01-06T00:37:01.380 回答