我的sidekiq工人就像以下示例:
Class BooksWorker
include Sidekiq::Worker
def perform
books = Book.where(collected: true)
books.each do |book|
book.update_attribute(:status, "read")
book.toggle!(:collected)
end
end
end
我想在禁用模式下使用Sidekiq测试来检查:
- 工作被招募
- sidekiq与redis通信(将作业推到redis)
- 从Redis检索工作并执行
- 工作提供预期的结果
我可以创建什么样的测试来检查上面的所有四个点?
考虑下面的示例测试:
require 'test_helper'
require 'sidekiq/testing'
class BooksWorkerDisableTest < Minitest::Test
def setup
configure = -> (config) do
config.redis = { url: 'redis://localhost:6379/15' }
end
Sidekiq.configure_client(&configure)
Sidekiq.configure_server(&configure)
Sidekiq::Testing.disable!
@books = Book.where(collected: true)
end
test "collected books should be marked as read before archived" do
BooksWorker.perform_async
@books.each do |book|
assert book.status == "read"
assert book.collected == false
end
end
end
如果在测试期间的Sidekiq招募工作,将其推到Redis,从Redis中检索并执行它,则假设仅在执行工作后关闭测试才能完成测试需要多长时间?<<<<<<<<<<
上面的测试检查仅第四点:如何测试其他点?我想只是隐含的是,如果执行作业,那么它们已经排队并将其推向Redis。
您的测试应测试您的代码,而不是Sidekiq。执行手动测试以验证所有内容已集成。禁用模式可以测试1和2,您的测试可以直接验证4。3是手动完成的。