Jackson
Jackson

Reputation: 6861

Resque Job and rspec

I have a Resque job that I am trying to test using rspec. The job looks something like:

class ImporterJob

def perform
 job_key = options['test_key']
 user = options['user']
end

I am using Resque status so I am using the create method to create the job like so:

ImporterJob.create({key:'test_key',user: 2})

If I try to create a job the same way in an rspec test, the options appear to not be making it to the job. As in, when I insert a binding.pry after user = options['user'], the options hash is empty.

My rspec test looks like this:

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

Upvotes: 3

Views: 4187

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

For unit-testing it is not advisable to run the code you are testing on a different thread/process (which is what Requeue is doing). Instead you should simulate the situation by running it directly. Fortunately, Resque has a feature called 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

So, your test should look like this:

it 'validates and imports csv data' do
  Resque.inline do
    ImporterJob.create(import_options)
  end
end

Upvotes: 6

Related Questions