/
action_controller_flash_before_render_spec.rb
101 lines (93 loc) · 2.66 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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::ActionControllerFlashBeforeRender, :config do
context 'within a rails controller class' do
context 'within an instance method' do
context 'when using `flash` before `render`' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class HomeController < ApplicationController
def create
flash[:alert] = "msg"
^^^^^ Use `flash.now` before `render`.
render :index
end
end
RUBY
expect_correction(<<~RUBY)
class HomeController < ApplicationController
def create
flash.now[:alert] = "msg"
render :index
end
end
RUBY
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.google.com/"
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 block' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
class HomeController < ApplicationController
before_action do
flash[:alert] = "msg"
^^^^^ Use `flash.now` before `render`.
render :index
end
end
RUBY
expect_correction(<<~RUBY)
class HomeController < ApplicationController
before_action do
flash.now[:alert] = "msg"
render :index
end
end
RUBY
end
end
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
flash[:alert] = "msg"
render :index
end
RUBY
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