Gra*_*son 2 rspec ruby-on-rails resque resque-status
我有一个Resque工作,我试图使用rspec测试.这份工作看起来像:
class ImporterJob
def perform
job_key = options['test_key']
user = options['user']
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Resque状态,所以我使用create方法创建工作,如下所示:
ImporterJob.create({key:'test_key',user: 2})
Run Code Online (Sandbox Code Playgroud)
如果我尝试在rspec测试中以相同的方式创建作业,那么这些选项似乎无法完成工作.就像在我之后插入binding.pry时user = options['user'],options hash是空的.
我的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
Run Code Online (Sandbox Code Playgroud)
对于单元测试,不建议在不同的线程/进程上运行您正在测试的代码(这是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
Run Code Online (Sandbox Code Playgroud)
所以,你的测试应该是这样的:
it 'validates and imports csv data' do
Resque.inline do
ImporterJob.create(import_options)
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2680 次 |
| 最近记录: |