Permalink
Browse files

Generate appropriate error more judiciously

  • Loading branch information...
1 parent 85c056c commit 569fb1fffb216ad96721fe1f5d706535e9920154 @marcandre marcandre committed Apr 29, 2012
Showing with 18 additions and 2 deletions.
  1. +3 −2 activemodel/lib/active_model/observing.rb
  2. +15 −0 activemodel/test/cases/observing_test.rb
@@ -90,8 +90,9 @@ def count_observers
def instantiate_observer(observer) #:nodoc:
# string/symbol
if observer.respond_to?(:to_sym)
- observer.to_s.camelize.constantize.instance
- elsif observer.respond_to?(:instance)
+ observer = observer.to_s.camelize.constantize
+ end
+ if observer.respond_to?(:instance)
observer.instance
else
raise ArgumentError,
@@ -70,6 +70,21 @@ def setup
ObservedModel.instantiate_observers
end
+ test "raises an appropriate error when a developer accidentally adds the wrong class (i.e. Widget instead of WidgetObserver)" do
+ assert_raise ArgumentError do
+ ObservedModel.observers = ['string']
+ ObservedModel.instantiate_observers
+ end
+ assert_raise ArgumentError do
+ ObservedModel.observers = [:string]
+ ObservedModel.instantiate_observers
+ end
+ assert_raise ArgumentError do
+ ObservedModel.observers = [String]
+ ObservedModel.instantiate_observers
+ end
+ end
+
test "passes observers to subclasses" do
FooObserver.instance
bar = Class.new(Foo)

0 comments on commit 569fb1f

Please sign in to comment.