使用 Resque 和 Rspec 示例进行测试?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人对我的工人进行了不同的测试。我使用 RSpec,例如在我的用户模型中我测试如下内容:
然后我有一个名为 spec/workers/foo_worker_spec.rb 的文件,其中包含以下内容:
然后您的模型/控制器测试运行得更快,并且您之间没有依赖关系模型/控制器和您的测试中的工作人员。您也不必在规范中模拟太多与工人无关的东西。
但如果你不想像你提到的那样做,它前段时间对我有用。我将 Resque.inline = true 放入我的测试环境配置中。
I personally test my workers different. I use RSpec and for example in my user model I test something like this:
Then I have a file called spec/workers/foo_worker_spec.rb with following content:
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.
在spec_helper.rb中,
您可以添加
我遇到了同样的问题,上面解决了它。
In the spec_helper.rb
you can add
I was facing same issue, with above it solved it.
看起来有关日志记录的问题从未得到解答。我遇到了类似的情况,这是因为没有设置 Resque 记录器。您可以做一些简单的事情,例如:
或者您可以通过将其添加到 /lib/tasks/resque.rake 来设置单独的日志文件。当你运行你的工作程序时,它会写入 /log/resque.log
像上面提到的 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:
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
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. :)