使用 Resque 和 Rspec 示例进行测试?

发布于 2024-12-01 13:30:03 字数 788 浏览 1 评论 0原文

我正在使用 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 中,并且没有任何 Resque 回调作为 Rails.env.test?在测试环境中返回true。

"this must get printed in console"

在测试时从未打印。我的测试也失败了。

有没有我错过的配置。 目前我正在使用

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

I am processing my background jobs using Resque.
My model looks like this

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

Now to test in synchronously i have added

Resque.inline = Rails.env.test?

in my config/initializers/resque.rb file

This was supposed to call #perform method inline without queuing it into Redis and without any Resque callbacks as Rails.env.test? returns true in test environment.

But

"this must get printed in console"

is never printed while testing. and my tests are also failing.

Is there any configurations that i have missed.
Currently i am using

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2024-12-08 13:30:03

我个人对我的工人进行了不同的测试。我使用 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

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

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

I personally test my workers different. I use RSpec and for example in my user model I test something like this:

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

Then I have a file called spec/workers/foo_worker_spec.rb with following content:

require 'spec_helper'

describe FooWorker do

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

end

Then your model/controller tests run faster and you don't have the dependency between model/controller and your worker in your tests. You also don't have to mock so much things in specs which don't have to do with the worker.

But if you wan't to do it like you mentioned, it worked for me some times ago. I put Resque.inline = true into my test environment config.

雨巷深深 2024-12-08 13:30:03

在spec_helper.rb中,

您可以添加

config.before :each do
    Resque.inline = true
end

我遇到了同样的问题,上面解决了它。

In the spec_helper.rb

you can add

config.before :each do
    Resque.inline = true
end

I was facing same issue, with above it solved it.

残花月 2024-12-08 13:30:03

看起来有关日志记录的问题从未得到解答。我遇到了类似的情况,这是因为没有设置 Resque 记录器。您可以做一些简单的事情,例如:

Resque.logger = Rails.logger

或者您可以通过将其添加到 /lib/tasks/resque.rake 来设置单独的日志文件。当你运行你的工作程序时,它会写入 /log/resque.log

Resque.before_fork = Proc.new {
ActiveRecord::Base.establish_connection

# Open the new separate log file
logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')

# Activate file synchronization
logfile.sync = true

# Create a new buffered logger
Resque.logger = ActiveSupport::Logger.new(logfile)
Resque.logger.level = Logger::INFO
Resque.logger.info "Resque Logger Initialized!"
}

像上面提到的 daniel-spangenberg 这样的模拟应该写入 STDOUT ,除非你的方法位于类的“私有”部分。在编写 rspec 测试时,这让我犯了几次错误。 ActionMailer 也需要它自己的日志设置。我想我一直期待更多的约定而不是配置。 :)

It looks like the question about logging never got answered. I ran into something similar to this and it was from not setting up the Resque logger. You can do something as simple as:

Resque.logger = Rails.logger

Or you can setup a separate log file by adding this to your /lib/tasks/resque.rake. When you run your worker it will write to /log/resque.log

Resque.before_fork = Proc.new {
ActiveRecord::Base.establish_connection

# Open the new separate log file
logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')

# Activate file synchronization
logfile.sync = true

# Create a new buffered logger
Resque.logger = ActiveSupport::Logger.new(logfile)
Resque.logger.level = Logger::INFO
Resque.logger.info "Resque Logger Initialized!"
}

Mocking like daniel-spangenberg mentioned above ought to write to STDOUT unless your methods are in the "private" section of your class. That's tripped me up a couple times when writing rspec tests. ActionMailer requires it's own log setup too. I guess I've been expecting more convention than configuration. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文