Ruby-on-rails 如何使用 Rspec 测试后台作业 (Sidekiq)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15717712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:43:00  来源:igfitidea点击:

How to test background jobs (Sidekiq) with Rspec?

ruby-on-railsrspecbackground-processstubsidekiq

提问by mind.blank

A large part of my application relies on background jobs to process user's websites, so I need to write some tests to cover these.

我的应用程序的很大一部分依赖于后台作业来处理用户的网站,所以我需要编写一些测试来覆盖这些。

First question is how to test code that's in a Sidekiqworker class, for example app/workers/some_worker.rb

例如,第一个问题是如何测试Sidekiq工作类中的代码app/workers/some_worker.rb

class SomeWorker
  include Sidekiq::Worker

  def perform(model_id)
    ...
  end
end

Secondly, how do I stub code such as the following so I don't need to actually run workers and slow down my tests?

其次,我如何存根如下代码,这样我就不需要实际运行工作程序并减慢我的测试速度?

response = HTTParty.get("http://users-site.com", timeout: 30)

回答by Mike Perham

回答by Phil Ostler

Take a look at the rspec-sidekiqgem I've authored to do exactly this (testing of Sidekiq workers).

看看我编写的rspec-sidekiqgem(测试 Sidekiq 工人)。

回答by Unkas

Sidekiq provides a few options for testing your workers

Sidekiq 提供了一些选项来测试您的工作人员

https://github.com/mperham/sidekiq/wiki/Testing

https://github.com/mperham/sidekiq/wiki/Testing

For me it worked after I add these lines at the top of a spec-file

对我来说,在规范文件的顶部添加这些行后,它起作用了

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