mercredi 1 janvier 2020

Rspec testing strong params in Rails not working (wrong number of arguments)

I'm trying to implement some testing for my controller strong params and following the docs to the 'T'.

My controller:

class FutureOutagesController < ApplicationController
  before_action :find_future_outage, only: [:show, :update, :destroy] 
  before_action :future_outage_params, only: [:create, :update, :destroy]

  def index 
    @future_outages = FutureOutage.all
  end

  def show
  end

  def create
    @future_outage = FutureOutage.new(future_outage_params)
  end

  def update
  end

  def destroy
  end

  private 

  def find_future_outage 
    @future_outage = FutureOutage.find(params[:id])
  end 

  def future_outage_params
    params.require(:future_outage).permit(:start_time, :end_time, :reason, :service_id)
  end
end

my rsepc test with FactoryBot and ShouldaMatchers:

require 'rails_helper'

RSpec.describe FutureOutagesController, type: :controller do
    let(:fo) { build :future_outage }

  context 'test params' do
    it do
      params = {
        future_outage: {
          start_time: fo.start_time,
          end_time: fo.end_time,
          reason: fo.reason,
          service_id: fo.service_id
        }
      } 
      should permit(:start_time, :end_time, :reason, :service_id).
      for(:create, params: params).on(:future_outage)
    end
  end

end

I get the error:

Failures:

  1) FutureOutagesController test params is expected to (for POST #create) restrict parameters on :future_outage to :start_time, :end_time, :reason, and :service_id
     Failure/Error:
       should permit(:start_time, :end_time, :reason, :service_id).
       for(:create, params: params).on(:future_outage)

     ActionView::Template::Error:
       wrong number of arguments (given 2, expected 1)
     # ./spec/controllers/future_outages_controller_spec.rb:25:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # ArgumentError:
     #   wrong number of arguments (given 2, expected 1)
     #   ./spec/controllers/future_outages_controller_spec.rb:25:in `block (3 levels) in <top (required)>'

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire