gpt4 book ai didi

ruby-on-rails - 重新请求作业和 rspec

转载 作者:行者123 更新时间:2023-12-04 05:42:51 25 4
gpt4 key购买 nike

我有一个 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

关于ruby-on-rails - 重新请求作业和 rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22191524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com