Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CallbackMatcher for ActionController
Matcher for checking if a filter / action callback is in place within a controller. Example: ```ruby describe UserController do it { should use_before_filter(:authenticate_user!) } end ```
- Loading branch information
Damian Galarza and Harry Schwartz
authored and
Damian Galarza
committed
Mar 7, 2014
1 parent
fb77a6c
commit b508346
Showing
4 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
lib/shoulda/matchers/action_controller/callback_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,100 @@ | |||
module Shoulda # :nodoc: | |||
module Matchers | |||
module ActionController # :nodoc: | |||
# Ensure a controller uses a given before_filter | |||
# | |||
# Example: | |||
# | |||
# it { should use_before_filter(:authenticate_user!) } | |||
# it { should_not use_before_filter(:prevent_ssl) } | |||
def use_before_filter(callback) | |||
CallbackMatcher.new(callback, :before, :filter) | |||
end | |||
|
|||
# Ensure a controller uses a given before_filter | |||
# | |||
# Example: | |||
# | |||
# it { should use_after_filter(:log_activity) } | |||
# it { should_not use_after_filter(:destroy_user) } | |||
def use_after_filter(callback) | |||
CallbackMatcher.new(callback, :after, :filter) | |||
end | |||
|
|||
# Ensure a controller uses a given before_action | |||
# | |||
# Example: | |||
# | |||
# it { should use_before_action(:authenticate_user!) } | |||
# it { should_not use_before_action(:prevent_ssl) } | |||
def use_before_action(callback) | |||
CallbackMatcher.new(callback, :before, :action) | |||
end | |||
|
|||
# Ensure a controller uses a given after_action | |||
# | |||
# Example: | |||
# | |||
# it { should use_after_action(:log_activity) } | |||
# it { should_not use_after_action(:destroy_user) } | |||
def use_after_action(callback) | |||
CallbackMatcher.new(callback, :after, :action) | |||
end | |||
|
|||
# Ensure a controller uses a given around_filter | |||
# | |||
# Example: | |||
# | |||
# it { should use_around_filter(:log_activity) } | |||
# it { should_not use_around_filter(:destroy_user) } | |||
def use_around_filter(callback) | |||
CallbackMatcher.new(callback, :around, :filter) | |||
end | |||
|
|||
# Ensure a controller uses a given around_action | |||
# | |||
# Example: | |||
# | |||
# it { should use_around_action(:log_activity) } | |||
# it { should_not use_around_action(:destroy_user) } | |||
def use_around_action(callback) | |||
CallbackMatcher.new(callback, :around, :action) | |||
end | |||
|
|||
class CallbackMatcher # :nodoc: | |||
def initialize(method_name, kind, callback_type) | |||
@method_name = method_name | |||
@kind = kind | |||
@callback_type = callback_type | |||
end | |||
|
|||
def matches?(subject) | |||
@subject = subject | |||
callbacks.map(&:filter).include?(method_name) | |||
end | |||
|
|||
def failure_message | |||
"Expected that #{subject.name} would have :#{method_name} as a #{kind}_#{callback_type}" | |||
end | |||
alias failure_message_for_should failure_message | |||
|
|||
def failure_message_when_negated | |||
"Expected that #{subject.name} would not have :#{method_name} as a #{kind}_#{callback_type}" | |||
end | |||
alias failure_message_for_should_not failure_message_when_negated | |||
|
|||
def description | |||
"have :#{method_name} as a #{kind}_#{callback_type}" | |||
end | |||
|
|||
private | |||
|
|||
def callbacks | |||
subject._process_action_callbacks.select { |callback| callback.kind == kind } | |||
end | |||
|
|||
attr_reader :method_name, :subject, :kind, :callback_type | |||
end | |||
end | |||
end | |||
end |
79 changes: 79 additions & 0 deletions
79
spec/shoulda/matchers/action_controller/callback_matcher_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,79 @@ | |||
require 'spec_helper' | |||
|
|||
describe Shoulda::Matchers::ActionController::CallbackMatcher do | |||
shared_examples 'CallbackMatcher' do |kind, callback_type| | |||
let(:matcher) { described_class.new(:authenticate_user!, kind, callback_type) } | |||
let(:controller) { define_controller('HookController') } | |||
|
|||
describe '#matches?' do | |||
it "matches when a #{kind} hook is in place" do | |||
add_callback(kind, callback_type, :authenticate_user!) | |||
|
|||
expect(matcher.matches?(controller)).to be_true | |||
end | |||
|
|||
it "does not match when a #{kind} hook is missing" do | |||
expect(matcher.matches?(controller)).to be_false | |||
end | |||
end | |||
|
|||
describe 'description' do | |||
it 'includes the filter kind and name' do | |||
expect(matcher.description).to eq "have :authenticate_user! as a #{kind}_#{callback_type}" | |||
end | |||
end | |||
|
|||
describe 'failure message' do | |||
it 'includes the filter kind and name that was expected' do | |||
message = "Expected that HookController would have :authenticate_user! as a #{kind}_#{callback_type}" | |||
|
|||
expect { | |||
expect(controller).to send("use_#{kind}_#{callback_type}", :authenticate_user!) | |||
}.to fail_with_message(message) | |||
end | |||
end | |||
|
|||
describe 'failure message when negated' do | |||
it 'includes the filter kind and name that was expected' do | |||
add_callback(kind, callback_type, :authenticate_user!) | |||
message = "Expected that HookController would not have :authenticate_user! as a #{kind}_#{callback_type}" | |||
|
|||
expect { | |||
expect(controller).not_to send("use_#{kind}_#{callback_type}", :authenticate_user!) | |||
}.to fail_with_message(message) | |||
end | |||
end | |||
|
|||
private | |||
|
|||
def add_callback(kind, callback_type, callback) | |||
controller.send("#{kind}_#{callback_type}", callback) | |||
end | |||
end | |||
|
|||
describe '#use_before_filter' do | |||
it_behaves_like 'CallbackMatcher', :before, :filter | |||
end | |||
|
|||
describe '#use_after_filter' do | |||
it_behaves_like 'CallbackMatcher', :after, :filter | |||
end | |||
|
|||
describe '#use_around_filter' do | |||
it_behaves_like 'CallbackMatcher', :around, :filter | |||
end | |||
|
|||
if rails_4_x? | |||
describe '#use_before_action' do | |||
it_behaves_like 'CallbackMatcher', :before, :action | |||
end | |||
|
|||
describe '#use_after_action' do | |||
it_behaves_like 'CallbackMatcher', :after, :action | |||
end | |||
|
|||
describe '#use_around_action' do | |||
it_behaves_like 'CallbackMatcher', :around, :action | |||
end | |||
end | |||
end |