我的测试:
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
它测试的控制器:
class ProjectsCsvJob < ApplicationJob
queue_as :default
def perform(clients_params)
# it does nothing
end
end
和ActiveJob:
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
测试未通过:
params['clients'].to_unsafe_hash
这很奇怪,因为当我在测试期间调试时,ProjectsCsvJob.perform_later({'id' => ['1', '2', '3']})
就是我所期望的。
然而,当我将控制器的行更改为
时{{1}}
测试通过。
答案 0 :(得分:0)
您正在尝试使用不支持的ActionController::Parameters
序列化ActiveJob
对象。
ActiveJob默认支持以下类型的参数:
- 基本类型(NilClass,String,Integer,Float,BigDecimal,TrueClass,FalseClass)
- 符号
- 日期
- 时间
- DateTime
- ActiveSupport :: TimeWithZone
- ActiveSupport :: Duration
- 哈希(键应为字符串或符号类型)
- ActiveSupport :: HashWithIndifferentAccess
- 数组