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

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

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

相关文章:

ruby-on-rails - Net::IMAP 失败:连接已关闭

ruby - 如何在 Resque 中查看已处理的作业?

ruby-on-rails - 使用 Mongoid (MongoDB)、Redis、Resque、Capistrano 等部署 Ruby on Rails 应用程序

ruby-on-rails - 在 Rails 中构建一个简单的搜索表单?

html - Ruby on Rails——模型 View 以某种方式成为导航栏的一部分

ruby-on-rails - 可锁定设计-如何使用unlock_in解锁帐户

ruby - RSpec 在发布请求之间保留实例变量

ruby-on-rails - rails - rspec - 为什么这个 "validates_inclusion_of :role, :in => %w[one, two, three ] }"不起作用

ruby-on-rails - 请求正文不可预测时的 stub_request

php - 在 Laravel 中使用 php-resque 时出现 "Constant CRLF already defined"错误