forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_matcher.rb
206 lines (192 loc) · 6.84 KB
/
callback_matcher.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
module Shoulda
module Matchers
module ActionController
# The `use_before_filter` matcher is used to test that a before_filter
# callback is defined within your controller.
#
# class UsersController < ApplicationController
# before_filter :authenticate_user!
# end
#
# # RSpec
# RSpec.describe UsersController, type: :controller do
# it { should use_before_filter(:authenticate_user!) }
# it { should_not use_before_filter(:prevent_ssl) }
# end
#
# # Minitest (Shoulda)
# class UsersControllerTest < ActionController::TestCase
# should use_before_filter(:authenticate_user!)
# should_not use_before_filter(:prevent_ssl)
# end
#
# @note This method is only available when using shoulda-matchers under
# Rails 4.x.
# @return [CallbackMatcher]
#
if RailsShim.action_pack_lt_5?
def use_before_filter(callback)
CallbackMatcher.new(callback, :before, :filter)
end
end
# The `use_after_filter` matcher is used to test that an after_filter
# callback is defined within your controller.
#
# class IssuesController < ApplicationController
# after_filter :log_activity
# end
#
# # RSpec
# RSpec.describe IssuesController, type: :controller do
# it { should use_after_filter(:log_activity) }
# it { should_not use_after_filter(:destroy_user) }
# end
#
# # Minitest (Shoulda)
# class IssuesControllerTest < ActionController::TestCase
# should use_after_filter(:log_activity)
# should_not use_after_filter(:destroy_user)
# end
#
# @note This method is only available when using shoulda-matchers under
# Rails 4.x.
# @return [CallbackMatcher]
#
if RailsShim.action_pack_lt_5?
def use_after_filter(callback)
CallbackMatcher.new(callback, :after, :filter)
end
end
# The `use_before_action` matcher is used to test that a before_action
# callback is defined within your controller.
#
# class UsersController < ApplicationController
# before_action :authenticate_user!
# end
#
# # RSpec
# RSpec.describe UsersController, type: :controller do
# it { should use_before_action(:authenticate_user!) }
# it { should_not use_before_action(:prevent_ssl) }
# end
#
# # Minitest (Shoulda)
# class UsersControllerTest < ActionController::TestCase
# should use_before_action(:authenticate_user!)
# should_not use_before_action(:prevent_ssl)
# end
#
# @return [CallbackMatcher]
#
def use_before_action(callback)
CallbackMatcher.new(callback, :before, :action)
end
# The `use_after_action` matcher is used to test that an after_action
# callback is defined within your controller.
#
# class IssuesController < ApplicationController
# after_action :log_activity
# end
#
# # RSpec
# RSpec.describe IssuesController, type: :controller do
# it { should use_after_action(:log_activity) }
# it { should_not use_after_action(:destroy_user) }
# end
#
# # Minitest (Shoulda)
# class IssuesControllerTest < ActionController::TestCase
# should use_after_action(:log_activity)
# should_not use_after_action(:destroy_user)
# end
#
# @return [CallbackMatcher]
#
def use_after_action(callback)
CallbackMatcher.new(callback, :after, :action)
end
# The `use_around_filter` matcher is used to test that an around_filter
# callback is defined within your controller.
#
# class ChangesController < ApplicationController
# around_filter :wrap_in_transaction
# end
#
# # RSpec
# RSpec.describe ChangesController, type: :controller do
# it { should use_around_filter(:wrap_in_transaction) }
# it { should_not use_around_filter(:save_view_context) }
# end
#
# # Minitest (Shoulda)
# class ChangesControllerTest < ActionController::TestCase
# should use_around_filter(:wrap_in_transaction)
# should_not use_around_filter(:save_view_context)
# end
#
# @note This method is only available when using shoulda-matchers under
# Rails 4.x.
# @return [CallbackMatcher]
#
if RailsShim.action_pack_lt_5?
def use_around_filter(callback)
CallbackMatcher.new(callback, :around, :filter)
end
end
# The `use_around_action` matcher is used to test that an around_action
# callback is defined within your controller.
#
# class ChangesController < ApplicationController
# around_action :wrap_in_transaction
# end
#
# # RSpec
# RSpec.describe ChangesController, type: :controller do
# it { should use_around_action(:wrap_in_transaction) }
# it { should_not use_around_action(:save_view_context) }
# end
#
# # Minitest (Shoulda)
# class ChangesControllerTest < ActionController::TestCase
# should use_around_action(:wrap_in_transaction)
# should_not use_around_action(:save_view_context)
# end
#
# @return [CallbackMatcher]
#
def use_around_action(callback)
CallbackMatcher.new(callback, :around, :action)
end
# @private
class CallbackMatcher
def initialize(method_name, kind, callback_type)
@method_name = method_name
@kind = kind
@callback_type = callback_type
end
def matches?(controller)
@controller = controller
@controller_class = controller.class
callbacks.map(&:filter).include?(method_name)
end
def failure_message
"Expected that #{controller_class.name} would have :#{method_name} as a #{kind}_#{callback_type}"
end
def failure_message_when_negated
"Expected that #{controller_class.name} would not have :#{method_name} as a #{kind}_#{callback_type}"
end
def description
"have :#{method_name} as a #{kind}_#{callback_type}"
end
protected
def callbacks
controller_class._process_action_callbacks.select do |callback|
callback.kind == kind
end
end
attr_reader :method_name, :controller, :controller_class, :kind,
:callback_type
end
end
end
end