Fix wrong UninferrableDecoratorError error raising

This commit is contained in:
eugen0329 2016-11-07 04:36:53 +03:00
parent 45e18569d9
commit de2ae4a7d3
2 changed files with 10 additions and 1 deletions

View File

@ -74,10 +74,10 @@ module Draper
decorator_name = "#{prefix}Decorator"
decorator_name.constantize
rescue NameError => error
raise unless error.missing_name?(decorator_name)
if superclass.respond_to?(:decorator_class)
superclass.decorator_class
else
raise unless error.missing_name?(decorator_name)
raise Draper::UninferrableDecoratorError.new(self)
end
end

View File

@ -182,6 +182,15 @@ module Draper
end
end
context "when the decorator contains name error" do
it "throws an NameError" do
# We imitate ActiveSupport::Autoload behavior here in order to cause lazy NameError exception raising
allow_any_instance_of(Module).to receive(:const_missing) { Class.new { any_nonexisting_method_name } }
expect{Model.decorator_class}.to raise_error { |error| expect(error).to be_an_instance_of(NameError) }
end
end
context "when the decorator can't be inferred" do
it "throws an UninferrableDecoratorError" do
expect{Model.decorator_class}.to raise_error UninferrableDecoratorError