mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
rescue_from now works with non-public methods
This commit is contained in:
parent
5f9e1bc5fb
commit
81b7e8472a
3 changed files with 42 additions and 12 deletions
3
NEWS.md
3
NEWS.md
|
@ -49,6 +49,9 @@
|
|||
|
||||
* Add ability to test `belongs_to` associations defined with `:inverse_of`.
|
||||
|
||||
* Fix `rescue_from` matcher so that it does not raise an error when testing
|
||||
a method handler which has been marked as protected or private.
|
||||
|
||||
# v 2.5.0
|
||||
|
||||
* Fix Rails/Test::Unit integration to ensure that the test case classes we are
|
||||
|
|
|
@ -72,7 +72,7 @@ module Shoulda
|
|||
|
||||
def handler_exists?
|
||||
if expected_method.present?
|
||||
controller.respond_to? expected_method
|
||||
controller.respond_to? expected_method, true
|
||||
else
|
||||
true
|
||||
end
|
||||
|
|
|
@ -2,20 +2,31 @@ require 'spec_helper'
|
|||
|
||||
describe Shoulda::Matchers::ActionController::RescueFromMatcher do
|
||||
context 'a controller that rescues from RuntimeError' do
|
||||
it "asserts controller is setup with rescue_from" do
|
||||
it 'asserts controller is setup with rescue_from' do
|
||||
expect(controller_with_rescue_from).to rescue_from RuntimeError
|
||||
end
|
||||
|
||||
context 'with a handler method' do
|
||||
it "asserts rescue_from was set up with handler method" do
|
||||
expect(controller_with_rescue_from_and_method).to rescue_from(RuntimeError).with(:error_method)
|
||||
it 'asserts rescue_from can find the handler when it is public' do
|
||||
controller = controller_with_rescue_from_and_method(:public)
|
||||
check_rescue_with_method_for(controller)
|
||||
end
|
||||
|
||||
it "asserts rescue_from was not set up with incorrect handler method" do
|
||||
it 'asserts rescue_from can find the handler when it is protected' do
|
||||
controller = controller_with_rescue_from_and_method(:protected)
|
||||
check_rescue_with_method_for(controller)
|
||||
end
|
||||
|
||||
it 'asserts rescue_from can find the handler when it is private' do
|
||||
controller = controller_with_rescue_from_and_method(:private)
|
||||
check_rescue_with_method_for(controller)
|
||||
end
|
||||
|
||||
it 'asserts rescue_from was not set up with incorrect handler method' do
|
||||
expect(controller_with_rescue_from_and_method).not_to rescue_from(RuntimeError).with(:other_method)
|
||||
end
|
||||
|
||||
it "asserts the controller responds to the handler method" do
|
||||
it 'asserts the controller responds to the handler method' do
|
||||
matcher = rescue_from(RuntimeError).with(:error_method)
|
||||
expect(matcher.matches?(controller_with_rescue_from_and_invalid_method)).to eq false
|
||||
expect(matcher.failure_message).to match(/does not respond to/)
|
||||
|
@ -23,7 +34,7 @@ describe Shoulda::Matchers::ActionController::RescueFromMatcher do
|
|||
end
|
||||
|
||||
context 'without a handler method' do
|
||||
it "the handler method is not included in the description" do
|
||||
it 'the handler method is not included in the description' do
|
||||
matcher = rescue_from(RuntimeError)
|
||||
expect(matcher.matches?(controller_with_rescue_from)).to eq true
|
||||
expect(matcher.description).not_to match(/with #/)
|
||||
|
@ -32,32 +43,48 @@ describe Shoulda::Matchers::ActionController::RescueFromMatcher do
|
|||
end
|
||||
|
||||
context 'a controller that does not rescue from RuntimeError' do
|
||||
it "asserts controller is not setup with rescue_from" do
|
||||
it 'asserts controller is not setup with rescue_from' do
|
||||
matcher = rescue_from RuntimeError
|
||||
expect(define_controller("RandomController")).not_to matcher
|
||||
expect(define_controller('RandomController')).not_to matcher
|
||||
expect(matcher.failure_message_when_negated).to match(/Did not expect \w+ to rescue from/)
|
||||
end
|
||||
end
|
||||
|
||||
def check_rescue_with_method_for(controller)
|
||||
expect(controller).to rescue_from(RuntimeError).with(:error_method)
|
||||
end
|
||||
|
||||
def controller_with_rescue_from
|
||||
define_controller "RescueRuntimeError" do
|
||||
define_controller 'RescueRuntimeError' do
|
||||
rescue_from(RuntimeError) {}
|
||||
end
|
||||
end
|
||||
|
||||
def controller_with_rescue_from_and_invalid_method
|
||||
define_controller "RescueRuntimeErrorWithMethod" do
|
||||
define_controller 'RescueRuntimeErrorWithMethod' do
|
||||
rescue_from RuntimeError, with: :error_method
|
||||
end
|
||||
end
|
||||
|
||||
def controller_with_rescue_from_and_method
|
||||
def controller_with_rescue_from_and_method(access = :public)
|
||||
controller = controller_with_rescue_from_and_invalid_method
|
||||
class << controller
|
||||
def error_method
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
case access
|
||||
when :protected
|
||||
class << controller
|
||||
protected :error_method
|
||||
end
|
||||
when :private
|
||||
class << controller
|
||||
private :error_method
|
||||
end
|
||||
end
|
||||
|
||||
controller
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue