Skip to content

Commit 0ba3732

Browse files
committed
Add a spec for Rails/ActionControllerFlashBeforeRender
Closes rubocop#811.
1 parent c18c95d commit 0ba3732

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,46 @@ class NonController < ApplicationRecord
101101
end
102102
end
103103

104+
context 'with a condition' do
105+
%w[ActionController::Base ApplicationController].each do |parent_class|
106+
context "within a class inherited from #{parent_class}" do
107+
it 'registers an offense and corrects' do
108+
expect_offense(<<~RUBY)
109+
class HomeController < #{parent_class}
110+
def create
111+
flash[:alert] = "msg" if condition
112+
^^^^^ Use `flash.now` before `render`.
113+
render :index
114+
end
115+
end
116+
RUBY
117+
118+
expect_correction(<<~RUBY)
119+
class HomeController < #{parent_class}
120+
def create
121+
flash.now[:alert] = "msg" if condition
122+
render :index
123+
end
124+
end
125+
RUBY
126+
end
127+
end
128+
end
129+
130+
context 'within a non Rails controller class' do
131+
it 'does not register an offense' do
132+
expect_no_offenses(<<~RUBY)
133+
class NonController < ApplicationRecord
134+
before_action do
135+
flash[:alert] = "msg"
136+
render :index
137+
end
138+
end
139+
RUBY
140+
end
141+
end
142+
end
143+
104144
context 'within a class method' do
105145
it 'does not register an offense' do
106146
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)