Search code examples
ruby-on-rails-4strong-parameters

Rails 4 Strong Parameters test logging of Unpermitted parameters


Does anyone know how to test the logging of Unpermitted parameters using Rspec.

I have tried using Rails.logger.should_receive() with :warn, :info, :error but I can't seem to capture the log message.

Here is the controller test:

describe Api::V2::ItemsController do

  before do
    @item = FactoryGirl.create(:item)
    @user_id = @item.user_id
    @invalid_user_id = @user_id + 1
  end

  it 'should not assign user_id' do
    put :update, id: @item.id, item: { user_id: @invalid_user_id }
    assigns(:item).user_id.should == @user_id
  end

  it 'should write "Unpermitted parameters: user_id" to log' do
    Rails.logger.should_receive(:warn).with("Unpermitted parameters: user_id")
    put :update, id: @item.id, item: { user_id: @invalid_user_id }
  end

end

Solution

  • Have you tried with :debug?

    expect(Rails.logger).to receive(:debug).with('...')
    # or
    # expect(ActionController::Base.logger).to receive(:debug).with('...')
    

    To change the log level to warn you could try the config below in any of your environment files.

    # config/environments/development.rb
    # config/environments/production.rb
    config.log_level = :warn
    

    More: Debugging Rails Applications