Question: ActiveJob spec doesn't work with ActionController::Parameters

Question

ActiveJob spec doesn't work with ActionController::Parameters

Answers 0
Added at 2017-01-02 12:01
Tags
Question

My test:

describe TasksCsvsController do
  describe '#index' do
    let(:params) { {'clients' => {'id' => ['1', '2', '3']}} }

    before do
      ActiveJob::Base.queue_adapter = :test
    end

    it 'enqueues tasks csv job' do
      get :create, params: params
      expect(ProjectsCsvJob).to have_been_enqueued.with(params['clients'])
    end
  end
end

The controller it tests:

class TasksCsvsController < ApplicationController
  def create
    ProjectsCsvJob.perform_now(csv_params.to_unsafe_hash)
    redirect_to tasks_path, notice: I18n.t('flashes.tasks_csv_generating', email: current_user.email)
  end

  private

  def csv_params
    params.require(:clients).permit(:from, :to, tasks_grid: {}, id: [])
  end
end

And the ActiveJob:

class ProjectsCsvJob < ApplicationJob
  queue_as :default

  def perform(clients_params)
    # it does nothing
  end
end

The test doesn't pass:

Failure/Error: expect(ProjectsCsvJob).to have_been_enqueued.with(params['clients'])
       expected to enqueue exactly 1 jobs, with [{"id"=>["1", "2", "3"]}], but enqueued 0

This is strange, because when I debug during the test, params['clients'].to_unsafe_hash is what I expect.

However, when I change the controller's line to

ProjectsCsvJob.perform_later({'id' => ['1', '2', '3']})

the test passes.

Answers
Source Show
◀ Wstecz