0

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?

Demian Sims
  • 871
  • 1
  • 14
  • 29
  • Which line is future_outages_controller_spec.rb:25? – Sergio Tulentsev Jan 01 '20 at 18:41
  • 4
    I don't really think that this is the best way to test this in the first place. Just use a normal request spec and create a resource and write expectations that test the attributes of the newly created resource. What you are testing here is the implementation of your controller and not the actual behaviour. – max Jan 01 '20 at 18:47
  • Is it the whole controller code that you've provided? It feels (based on the stacktrace) like there's sth missing. – Greg Jan 01 '20 at 21:49
  • rpsec line 25 is `should permit(:start_time, :end_time, :reason, :service_id). for(:create, params: params).on(:future_outage)` – Demian Sims Jan 02 '20 at 15:37
  • Show more that one stacktrace line. The error is `ActionView::Template::Error` so it's probably not thrown by shoulda matcher. – Greg Jan 04 '20 at 08:28

0 Answers0