From 0ba373236ce9da37748636604e999c97896bbc8e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 14 Oct 2022 16:52:13 +0900 Subject: [PATCH] Add a spec for `Rails/ActionControllerFlashBeforeRender` Closes #811. --- ...ion_controller_flash_before_render_spec.rb | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb b/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb index 54d5d2cbba..c30fd285b9 100644 --- a/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb +++ b/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb @@ -101,6 +101,46 @@ class NonController < ApplicationRecord end end + context 'with a condition' do + %w[ActionController::Base ApplicationController].each do |parent_class| + context "within a class inherited from #{parent_class}" do + it 'registers an offense and corrects' do + expect_offense(<<~RUBY) + class HomeController < #{parent_class} + def create + flash[:alert] = "msg" if condition + ^^^^^ Use `flash.now` before `render`. + render :index + end + end + RUBY + + expect_correction(<<~RUBY) + class HomeController < #{parent_class} + def create + flash.now[:alert] = "msg" if condition + render :index + end + end + RUBY + end + end + end + + context 'within a non Rails controller class' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + class NonController < ApplicationRecord + before_action do + flash[:alert] = "msg" + render :index + end + end + RUBY + end + end + end + context 'within a class method' do it 'does not register an offense' do expect_no_offenses(<<~RUBY)