2013-04-26 16:34:21 -04:00
|
|
|
module Shoulda
|
|
|
|
module Matchers
|
|
|
|
module ActionController
|
2014-01-23 13:07:36 -05:00
|
|
|
# The `rescue_from` matcher tests usage of the `rescue_from` macro. It
|
|
|
|
# asserts that an exception and method are present in the list of
|
|
|
|
# exception handlers, and that the handler method exists.
|
|
|
|
#
|
|
|
|
# class ApplicationController < ActionController::Base
|
|
|
|
# rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
|
|
|
|
#
|
|
|
|
# private
|
|
|
|
#
|
|
|
|
# def handle_not_found
|
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # RSpec
|
|
|
|
# describe ApplicationController do
|
|
|
|
# it do
|
|
|
|
# should rescue_from(ActiveRecord::RecordNotFound).
|
|
|
|
# with(:handle_not_found)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # Test::Unit
|
|
|
|
# class ApplicationControllerTest < ActionController::TestCase
|
|
|
|
# should rescue_from(ActiveRecord::RecordNotFound).
|
|
|
|
# with(:handle_not_found)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# @return [RescueFromMatcher]
|
|
|
|
#
|
2013-04-26 16:34:21 -04:00
|
|
|
def rescue_from(exception)
|
|
|
|
RescueFromMatcher.new exception
|
|
|
|
end
|
|
|
|
|
2014-01-23 13:07:36 -05:00
|
|
|
# @private
|
2013-04-26 16:34:21 -04:00
|
|
|
class RescueFromMatcher
|
|
|
|
def initialize(exception)
|
|
|
|
@exception = exception
|
2014-06-27 18:39:39 -04:00
|
|
|
@expected_method = nil
|
|
|
|
@controller = nil
|
2013-04-26 16:34:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def with(method)
|
|
|
|
@expected_method = method
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(controller)
|
|
|
|
@controller = controller
|
|
|
|
rescues_from_exception? && method_name_matches? && handler_exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
description = "rescues from #{exception}"
|
|
|
|
description << " with ##{expected_method}" if expected_method
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
2013-12-24 06:24:27 -05:00
|
|
|
def failure_message
|
2013-04-26 16:34:21 -04:00
|
|
|
"Expected #{expectation}"
|
|
|
|
end
|
2013-12-24 06:24:27 -05:00
|
|
|
alias failure_message_for_should failure_message
|
2013-04-26 16:34:21 -04:00
|
|
|
|
2013-12-24 06:24:27 -05:00
|
|
|
def failure_message_when_negated
|
2013-04-26 16:34:21 -04:00
|
|
|
"Did not expect #{expectation}"
|
|
|
|
end
|
2013-12-24 06:24:27 -05:00
|
|
|
alias failure_message_for_should_not failure_message_when_negated
|
2013-04-26 16:34:21 -04:00
|
|
|
|
2014-06-24 11:57:52 -04:00
|
|
|
protected
|
2013-04-26 16:34:21 -04:00
|
|
|
|
|
|
|
attr_reader :controller, :exception, :expected_method, :handlers
|
|
|
|
|
|
|
|
def expectation
|
|
|
|
expectation = "#{controller} to rescue from #{exception}"
|
|
|
|
|
|
|
|
if expected_method && !method_name_matches?
|
|
|
|
expectation << " with ##{expected_method}"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless handler_exists?
|
|
|
|
expectation << " but #{controller} does not respond to #{expected_method}"
|
|
|
|
end
|
|
|
|
expectation
|
|
|
|
end
|
|
|
|
|
|
|
|
def rescues_from_exception?
|
|
|
|
@handlers = controller.rescue_handlers.select do |handler|
|
|
|
|
handler.first == exception.to_s
|
|
|
|
end
|
|
|
|
handlers.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_name_matches?
|
|
|
|
if expected_method.present?
|
|
|
|
handlers.any? do |handler|
|
|
|
|
handler.last == expected_method
|
|
|
|
end
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handler_exists?
|
|
|
|
if expected_method.present?
|
2014-02-18 17:54:27 -05:00
|
|
|
controller.respond_to? expected_method, true
|
2013-04-26 16:34:21 -04:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|