ruby-on-rails - 使用 Resque 和 Rspec 示例进行测试?

标签 ruby-on-rails background jobs resque

我正在使用 Resque 处理我的后台作业。 我的模型看起来像这样

class SomeClass
  ...
  repo = Repo.find(params[:repo_id])
  Resque.enqueue(ReopCleaner, repo.id)
  ...
end

class RepoCleaner
  @queue = :repo_cleaner

  def self.perform(repo_id)
    puts "this must get printed in console"
    repo = Repo.find(repo_id)    
    # some more action here
  end
end

现在要同步测试我已经添加了

Resque.inline = Rails.env.test?

在我的 config/initializers/resque.rb 文件中

这应该内联调用 #perform 方法,而不将其排队到 Redis 中,并且没有像 Rails.env.test 那样的任何 Resque 回调?在测试环境中返回true。

但是

"this must get printed in console"

在测试时永远不会被打印。我的测试也失败了。

有没有我遗漏的配置。 目前我正在使用

resque (1.17.1)
resque_spec (0.7.0)
resque_unit (0.4.0)

最佳答案

我个人对我的 worker 进行了不同的测试。我使用 RSpec,例如在我的用户模型中我测试了如下内容:

it "enqueue FooWorker#create_user" do
  mock(Resque).enqueue(FooWorker, :create_user, user.id)
  user.create_on_foo
end

然后我有一个名为 spec/workers/foo_worker_spec.rb 的文件,其中包含以下内容:

require 'spec_helper'

describe FooWorker do

  describe "#perform" do
    it "redirects to passed action" do
      ...
      FooWorker.perform
      ...
    end
  end

end

然后你的模型/ Controller 测试运行得更快,并且测试中模型/ Controller 和工作线程之间不存在依赖关系。您也不必在规范中模拟太多与工作人员无关的东西。

但是如果你不想像你提到的那样做,它以前对我有用过。我将 Resque.inline = true 放入我的测试环境配置中。

关于ruby-on-rails - 使用 Resque 和 Rspec 示例进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7212138/

相关文章:

delphi - 定时器进程影响delphi中的用户界面

html - 在移动设备上滚动时,CSS 全屏背景在底部显示白条

java - Jenkins "execute shell command"段字符串参数的使用

sql-server - 在 Azure 上创建弹性作业代理后,导出失败

ruby-on-rails - 读取 xlsx/xls 文件 - Rails 4

ruby-on-rails - 用于将通知推送到苹果 ipad 的 rails apns - 使用哪个 gem?

mysql - Ruby on Rails : How to update the view with a new query

ruby-on-rails - ActionController::RoutingError: 使用复选框 + ajax 时没有路由匹配 ->

android - 使用自定义形状和 png 图像设置背景

c# - 使用 IJobParallelFor 的 System.InedxOutOfRangeException