/
action_controller_flash_before_render_spec.rb
131 lines (121 loc) · 3.62 KB
/
action_controller_flash_before_render_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ActionControllerFlashBeforeRender, :config do
context 'when using `flash` before `render`' do
context 'within an instance method' 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"
^^^^^ Use `flash.now` before `render`.
render :index
end
end
RUBY
expect_correction(<<~RUBY)
class HomeController < #{parent_class}
def create
flash.now[:alert] = "msg"
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
def create
flash[:alert] = "msg"
render :index
end
end
RUBY
end
end
end
context 'within a block' 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}
before_action do
flash[:alert] = "msg"
^^^^^ Use `flash.now` before `render`.
render :index
end
end
RUBY
expect_correction(<<~RUBY)
class HomeController < #{parent_class}
before_action do
flash.now[:alert] = "msg"
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)
class HomeController < ApplicationController
def self.create
flash[:alert] = "msg"
render :index
end
end
RUBY
end
end
context 'within a class body' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
flash[:alert] = "msg"
render :index
end
RUBY
end
end
context 'with no context' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
flash[:alert] = "msg"
render :index
RUBY
end
end
end
context 'when using `flash` before `redirect_to`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
def create
flash[:alert] = "msg"
redirect_to "https://www.example.com/"
end
end
RUBY
end
end
end