【问题标题】:Resque Job and rspecResque 作业和 rspec
【发布时间】:2014-03-06 00:17:59
【问题描述】:

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

【问题讨论】:

  • 如果你用symbols而不是字符串传递Hash会发生什么? :import_key => 'test_key'
  • 如果我传递一个哈希,它会出现同样的问题。
  • 是的,我的意思是符号。对于那个很抱歉。我实际上意识到了这个问题,但仍然不确定如何解决它。作业正在排队,但没有工人来运行它们。测试中有没有办法让工人开始工作?

标签: ruby-on-rails rspec resque resque-status


【解决方案1】:

对于单元测试,不建议在不同的线程/进程上运行您正在测试的代码(这是 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

【讨论】:

猜你喜欢
  • 2016-05-29
  • 2013-12-05
  • 1970-01-01
  • 2014-03-21
  • 2013-06-20
  • 2015-09-17
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多