Skip to content

Commit

Permalink
Add a spec for Rails/ActionControllerFlashBeforeRender
Browse files Browse the repository at this point in the history
Closes rubocop#811.
  • Loading branch information
koic committed Oct 14, 2022
1 parent c18c95d commit 0ba3732
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0ba3732

Please sign in to comment.