2014-10-27 00:51:42 +00:00
|
|
|
require 'unit_spec_helper'
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2014-12-24 21:37:04 +00:00
|
|
|
describe Shoulda::Matchers::ActionController::CallbackMatcher, type: :controller do
|
2014-01-31 22:06:49 +00:00
|
|
|
shared_examples 'CallbackMatcher' do |kind, callback_type|
|
2014-04-16 06:53:40 +00:00
|
|
|
let(:kind) { kind }
|
|
|
|
let(:callback_type) { callback_type }
|
|
|
|
let(:method_name) { :authenticate_user! }
|
|
|
|
let(:matcher) { described_class.new(method_name, kind, callback_type) }
|
|
|
|
let(:controller) { define_controller('HookController').new }
|
|
|
|
|
|
|
|
def match
|
|
|
|
__send__("use_#{kind}_#{callback_type}", method_name)
|
|
|
|
end
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2014-04-16 06:53:40 +00:00
|
|
|
it "matches when a #{kind} hook is in place" do
|
|
|
|
add_callback(kind, callback_type, method_name)
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2014-04-16 06:53:40 +00:00
|
|
|
expect(controller).to match
|
|
|
|
end
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2014-04-16 06:53:40 +00:00
|
|
|
it "does not match when a #{kind} hook is missing" do
|
|
|
|
expect(controller).not_to match
|
2014-01-31 22:06:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'description' do
|
|
|
|
it 'includes the filter kind and name' do
|
2014-04-16 06:53:40 +00:00
|
|
|
expect(matcher.description).to eq "have #{method_name.inspect} as a #{kind}_#{callback_type}"
|
2014-01-31 22:06:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'failure message' do
|
|
|
|
it 'includes the filter kind and name that was expected' do
|
2014-04-16 06:53:40 +00:00
|
|
|
message = "Expected that HookController would have #{method_name.inspect} as a #{kind}_#{callback_type}"
|
2014-01-31 22:06:49 +00:00
|
|
|
|
|
|
|
expect {
|
2014-04-16 06:53:40 +00:00
|
|
|
expect(controller).to send("use_#{kind}_#{callback_type}", method_name)
|
2014-01-31 22:06:49 +00:00
|
|
|
}.to fail_with_message(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'failure message when negated' do
|
|
|
|
it 'includes the filter kind and name that was expected' do
|
2014-04-16 06:53:40 +00:00
|
|
|
add_callback(kind, callback_type, method_name)
|
|
|
|
message = "Expected that HookController would not have #{method_name.inspect} as a #{kind}_#{callback_type}"
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2014-04-16 06:53:40 +00:00
|
|
|
expect { expect(controller).not_to match }.to fail_with_message(message)
|
2014-01-31 22:06:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_callback(kind, callback_type, callback)
|
2014-04-16 06:53:40 +00:00
|
|
|
controller.class.__send__("#{kind}_#{callback_type}", callback)
|
2014-01-31 22:06:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-18 00:51:41 +00:00
|
|
|
if action_pack_lt_5?
|
|
|
|
describe '#use_before_filter' do
|
|
|
|
it_behaves_like 'CallbackMatcher', :before, :filter
|
|
|
|
end
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2017-09-18 00:51:41 +00:00
|
|
|
describe '#use_after_filter' do
|
|
|
|
it_behaves_like 'CallbackMatcher', :after, :filter
|
|
|
|
end
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2017-09-18 00:51:41 +00:00
|
|
|
describe '#use_around_filter' do
|
|
|
|
it_behaves_like 'CallbackMatcher', :around, :filter
|
|
|
|
end
|
2014-01-31 22:06:49 +00:00
|
|
|
end
|
|
|
|
|
2017-09-18 00:51:41 +00:00
|
|
|
describe '#use_before_action' do
|
|
|
|
it_behaves_like 'CallbackMatcher', :before, :action
|
|
|
|
end
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2017-09-18 00:51:41 +00:00
|
|
|
describe '#use_after_action' do
|
|
|
|
it_behaves_like 'CallbackMatcher', :after, :action
|
|
|
|
end
|
2014-01-31 22:06:49 +00:00
|
|
|
|
2017-09-18 00:51:41 +00:00
|
|
|
describe '#use_around_action' do
|
|
|
|
it_behaves_like 'CallbackMatcher', :around, :action
|
2014-01-31 22:06:49 +00:00
|
|
|
end
|
|
|
|
end
|