[英]Resque Job and rspec
我有一個Resque作業,正在嘗試使用rspec進行測試。 該工作看起來像:
class ImporterJob
def perform
job_key = options['test_key']
user = options['user']
end
我正在使用Resque狀態,因此正在使用create方法創建作業,如下所示:
ImporterJob.create({key:'test_key',user: 2})
如果我嘗試在rspec測試中以相同的方式創建作業,則這些選項似乎不適合該作業。 與之類似,當我在user = options['user']
之后插入binding.pry時,選項哈希為空。
我的rspec測試如下所示:
describe ImporterJob do
context 'create' do
let(:import_options) {
{
'import_key' => 'test_key',
'user' => 1,
'model' => 'Test',
'another_flag' => nil
}
}
before(:all) do
Resque.redis.set('test_key',csv_file_location('data.csv'))
end
it 'validates and imports csv data' do
ImporterJob.create(import_options)
end
end
end
對於單元測試,建議不要在不同的線程/進程上運行正在測試的代碼(這是Requeue在做的事情)。 相反,您應該通過直接運行它來模擬情況。 幸運的是, Resque
具有一個名為inline
的功能:
# If 'inline' is true Resque will call #perform method inline
# without queuing it into Redis and without any Resque callbacks.
# If 'inline' is false Resque jobs will be put in queue regularly.
# @return [Boolean]
attr_writer :inline
# If block is supplied, this is an alias for #inline_block
# Otherwise it's an alias for #inline?
def inline(&block)
block ? inline_block(&block) : inline?
end
因此,您的測試應如下所示:
it 'validates and imports csv data' do
Resque.inline do
ImporterJob.create(import_options)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.