1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/test/cases/observing_test.rb

182 lines
5 KiB
Ruby
Raw Normal View History

require 'cases/helper'
2008-06-28 02:29:03 -04:00
2009-07-21 01:13:26 -04:00
class ObservedModel
include ActiveModel::Observing
2008-06-28 02:29:03 -04:00
class Observer
end
end
class FooObserver < ActiveModel::Observer
class << self
public :new
end
2008-06-28 02:29:03 -04:00
attr_accessor :stub
2012-04-29 16:30:54 -04:00
def on_spec(record, *args)
stub.event_with(record, *args) if stub
2008-06-28 02:29:03 -04:00
end
def around_save(record)
yield :in_around_save
end
2008-06-28 02:29:03 -04:00
end
2009-07-21 01:13:26 -04:00
class Foo
include ActiveModel::Observing
2008-06-28 02:29:03 -04:00
end
class ObservingTest < ActiveModel::TestCase
2008-06-28 02:29:03 -04:00
def setup
ObservedModel.observers.clear
end
test "initializes model with no cached observers" do
assert ObservedModel.observers.empty?, "Not empty: #{ObservedModel.observers.inspect}"
end
2008-06-28 02:29:03 -04:00
test "stores cached observers in an array" do
ObservedModel.observers << :foo
assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
end
2008-06-28 02:29:03 -04:00
test "flattens array of assigned cached observers" do
ObservedModel.observers = [[:foo], :bar]
assert ObservedModel.observers.include?(:foo), ":foo not in #{ObservedModel.observers.inspect}"
assert ObservedModel.observers.include?(:bar), ":bar not in #{ObservedModel.observers.inspect}"
end
test "uses an ObserverArray so observers can be disabled" do
ObservedModel.observers = [:foo, :bar]
assert ObservedModel.observers.is_a?(ActiveModel::ObserverArray)
end
test "instantiates observer names passed as strings" do
ObservedModel.observers << 'foo_observer'
FooObserver.expects(:instance)
ObservedModel.instantiate_observers
end
test "instantiates observer names passed as symbols" do
ObservedModel.observers << :foo_observer
FooObserver.expects(:instance)
ObservedModel.instantiate_observers
end
test "instantiates observer classes" do
ObservedModel.observers << ObservedModel::Observer
ObservedModel::Observer.expects(:instance)
ObservedModel.instantiate_observers
2008-06-28 02:29:03 -04:00
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
2008-06-28 02:29:03 -04:00
test "passes observers to subclasses" do
FooObserver.instance
bar = Class.new(Foo)
assert_equal Foo.observers_count, bar.observers_count
2008-06-28 02:29:03 -04:00
end
end
class ObserverTest < ActiveModel::TestCase
2008-06-28 02:29:03 -04:00
def setup
ObservedModel.observers = :foo_observer
FooObserver.singleton_class.instance_eval do
alias_method :original_observed_classes, :observed_classes
end
end
def teardown
FooObserver.singleton_class.instance_eval do
2009-09-05 20:10:21 -04:00
undef_method :observed_classes
alias_method :observed_classes, :original_observed_classes
end
2008-06-28 02:29:03 -04:00
end
test "guesses implicit observable model name" do
assert_equal Foo, FooObserver.observed_class
2008-06-28 02:29:03 -04:00
end
test "tracks implicit observable models" do
instance = FooObserver.new
assert_equal [Foo], instance.observed_classes
2008-06-28 02:29:03 -04:00
end
2008-06-28 02:29:03 -04:00
test "tracks explicit observed model class" do
FooObserver.observe ObservedModel
instance = FooObserver.new
2012-04-29 22:02:02 -04:00
assert_equal [ObservedModel], instance.observed_classes
2008-06-28 02:29:03 -04:00
end
2008-06-28 02:29:03 -04:00
test "tracks explicit observed model as string" do
FooObserver.observe 'observed_model'
instance = FooObserver.new
2012-04-29 22:02:02 -04:00
assert_equal [ObservedModel], instance.observed_classes
2008-06-28 02:29:03 -04:00
end
2008-06-28 02:29:03 -04:00
test "tracks explicit observed model as symbol" do
FooObserver.observe :observed_model
instance = FooObserver.new
2012-04-29 22:02:02 -04:00
assert_equal [ObservedModel], instance.observed_classes
2008-06-28 02:29:03 -04:00
end
2008-06-28 02:29:03 -04:00
test "calls existing observer event" do
foo = Foo.new
FooObserver.instance.stub = stub
FooObserver.instance.stub.expects(:event_with).with(foo)
2012-04-30 14:20:42 -04:00
Foo.notify_observers(:on_spec, foo)
end
test "calls existing observer event from the instance" do
foo = Foo.new
FooObserver.instance.stub = stub
FooObserver.instance.stub.expects(:event_with).with(foo)
foo.notify_observers(:on_spec)
2008-06-28 02:29:03 -04:00
end
2012-04-29 16:30:54 -04:00
test "passes extra arguments" do
foo = Foo.new
FooObserver.instance.stub = stub
FooObserver.instance.stub.expects(:event_with).with(foo, :bar)
Foo.send(:notify_observers, :on_spec, foo, :bar)
end
2008-06-28 02:29:03 -04:00
test "skips nonexistent observer event" do
foo = Foo.new
2012-04-30 14:20:42 -04:00
Foo.notify_observers(:whatever, foo)
2008-06-28 02:29:03 -04:00
end
test "update passes a block on to the observer" do
yielded_value = nil
FooObserver.instance.update(:around_save, Foo.new) do |val|
yielded_value = val
end
assert_equal :in_around_save, yielded_value
end
test "observe redefines observed_classes class method" do
class BarObserver < ActiveModel::Observer
observe :foo
end
assert_equal [Foo], BarObserver.observed_classes
BarObserver.observe(ObservedModel)
assert_equal [ObservedModel], BarObserver.observed_classes
end
end