From 7168e2ca7419a6138543be34107de6f15bd6bbbb Mon Sep 17 00:00:00 2001 From: Steve Tooke Date: Tue, 31 Jan 2012 11:03:14 +0000 Subject: [PATCH] Ensure NoMethodError propogates correctly. When a delegated method calls a non-existant method the correct NoMethodError should be called. If the NoMethodError is swallowed by the decorator it can cause problems with BDD workflow. --- lib/draper/base.rb | 5 +++-- spec/draper/base_spec.rb | 12 ++++++++++++ spec/support/samples/product.rb | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/draper/base.rb b/lib/draper/base.rb index 4496d6b..a654ded 100644 --- a/lib/draper/base.rb +++ b/lib/draper/base.rb @@ -197,8 +197,9 @@ module Draper model.send(method, *args, &block) end self.send(method, *args, &block) - rescue NoMethodError - super + rescue NoMethodError => no_method_error + super if no_method_error.name == method + raise no_method_error end else super diff --git a/spec/draper/base_spec.rb b/spec/draper/base_spec.rb index 07648e6..0098abe 100644 --- a/spec/draper/base_spec.rb +++ b/spec/draper/base_spec.rb @@ -551,6 +551,18 @@ describe Draper::Base do subject.hello_world end end + + context "when the delegated method calls a non-existant method" do + it "raises the correct NoMethodError" do + begin + subject.some_action + rescue NoMethodError => e + e.name.should_not == :some_action + else + fail("No exception raised") + end + end + end end describe "#kind_of?" do diff --git a/spec/support/samples/product.rb b/spec/support/samples/product.rb index 2344ff5..92f0425 100644 --- a/spec/support/samples/product.rb +++ b/spec/support/samples/product.rb @@ -45,6 +45,10 @@ class Product < ActiveRecord::Base "Sample Title" end + def some_action + self.nonexistant_method + end + def block yield end