【问题标题】:How to test sidekiq with cucumber如何用黄瓜测试sidekiq
【发布时间】:2013-05-23 14:44:55
【问题描述】:
我有一种方法可以启动两个不同的 Sidekiq 工作人员 - 一个用于发送电子邮件,一个用于发布到前端的 Faye 频道。我正在尝试测试 Cucumber 功能中的行为。该代码在开发中运行良好。
我已将require 'sidekiq/testing/inline' 包含在features/support/env.rb 中,以便我可以查看工人工作的实际结果。电子邮件的测试工作正常(使用 Spreewald 电子邮件步骤),但我看不到任何证据表明其他工作人员已被执行。我在工作程序中包含了“logger.debug”语句,但它们没有出现在log/test.log 中——我还应该看看其他地方吗?
worker 似乎不太可能没有运行,但前端似乎没有收到 Faye 消息。欢迎任何有关如何调试它的建议。
【问题讨论】:
标签:
ruby-on-rails
cucumber
sidekiq
【解决方案1】:
通过在 support/env.rb 中添加适当的 require 语句,我能够在我的黄瓜测试中运行我的 Sidekiq 工作人员:
require 'sidekiq/testing'
如果使用 Spork,请将其放入您的 prefork 块中:
Spork.prefork do
# ...
require 'sidekiq/testing'
# ...
然后我写了一个类似这样的功能:
When I submit the form that adds jobs to the queue
Then I should see "Your jobs were added to the queue."
When I wait for the jobs to finish
And I refresh the page
Then the completed jobs are listed on the page
然后我手动排空队列:
When(/^I wait for the jobs to finish$/) do
MySpecialWorker.drain
end
您还可以通过在 require 语句后将 Sidekiq::Testing.inline! 添加到您的 support/env.rb 来使所有作业内联运行,但由于我只想运行这些作业,因此我选择仅手动运行我关心的那些关于。
【解决方案2】:
有时,我们需要使用Sidekiq::Testing.fake! 模式对任何测试用例进行测试。
在我们的项目中,我们已经使用了 RSpec 钩子方法来制作它。按照下面的代码,你可以看到:
在文件features/support/capybara.rb
Around('@sidekiq_job') do |_scenario, block|
Sidekiq::Testing.fake! do
block.call
end
end
这是一个使用此配置的示例:
@sidekiq_job
Scenario: Your scenario
Given ...
And ...
When ...
Then the demo should be enqueued
还有
Then /^the demo employee should be enqueued$/ do
expect(YourWorker.jobs.size).to eq(1)
end