forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
callback_matcher.rb
121 lines (110 loc) · 3.81 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
module Shoulda
module Matchers
module ActionController
# 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_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