Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Fixed broken specs. Added fix for exceptions raised in with_observers
Browse files Browse the repository at this point in the history
Before, if an exception is raised in with_observers, the peeping tom list
was not cleared, potentially breaking other specs. The list is now
cleared in an 'ensure' block
  • Loading branch information
jdsiegel authored and bkeepers committed Aug 17, 2009
1 parent 06ff4a4 commit c36be7c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/no_peeping_toms.rb
Expand Up @@ -22,6 +22,7 @@ def with_observers(*observer_syms)
o.respond_to?(:instance) ? o.instance : o.to_s.classify.constantize.instance
end
yield
ensure
self.peeping_toms.clear
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/no_peeping_toms_spec.rb
Expand Up @@ -55,6 +55,7 @@ def before_update(person)

it "should register a name change with an anonymous observer" do
observer = Class.new(ActiveRecord::Observer) do
observe NoPeepingTomsSpec::Person
def before_update(person)
$observer_called_names.push person.name
end
Expand Down Expand Up @@ -85,9 +86,11 @@ def before_update(person)

it "should handle multiple anonymous observers" do
observer1 = Class.new(ActiveRecord::Observer) do
observe NoPeepingTomsSpec::Person
def before_update(person) ; $observer_called_names.push "#{person.name} 1" ; end
end
observer2 = Class.new(ActiveRecord::Observer) do
observe NoPeepingTomsSpec::Person
def before_update(person) ; $observer_called_names.push "#{person.name} 2" ; end
end

Expand All @@ -102,5 +105,14 @@ def before_update(person) ; $observer_called_names.push "#{person.name} 2" ; end

$calls_to_another_observer.should == 0
end

it "should ensure peeping toms are reset after raised exception" do
lambda {
ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver) do
raise ArgumentError, "Michael, I've made a huge mistake"
end
}.should raise_error(ArgumentError)
ActiveRecord::Observer.peeping_toms.should == []
end
end
end

0 comments on commit c36be7c

Please sign in to comment.