I need to write specs to check permitted params for create action and getting error

186 views Asked by At

I need to write specs to check permitted params for create action.

module Backoffice
  class JobsController < BaseController
    def create
      @job = Job.new(job_params)
    end
    def job_params
      params.require(:job).permit(:title)
    end
  end
end

RSpec.describe Backoffice::JobsController, type: :controller do
  it do
    params = {
      job: {
        title: 'John'
      }
    }
    should permit(:title).
      for(:create, params: params).
      on(:job)
  end
end

For some reason I'm getting:

Expected POST #create to restrict parameters on :job to :title, but it did not restrict any parameters.

Is it because controller in a module?

1

There are 1 answers

0
LuzNon On

This is because your params should not be a hash, these params come from ActionController::Parameters. Try something like:

let(:params) do
  ActionController::Parameters.new({
    job: {
      title: 'John'
    }
  })
end