diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-07-11 16:59:11 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-07-11 16:59:11 -0500 |
commit | c863388039ff6e97447be86184d41fbf30f0f389 (patch) | |
tree | f629b4909c3e0e854cc85c42093d5bb846111ca4 /activeresource/test | |
parent | e83a05af076637cc78f0408f5810d5f8f965e10c (diff) | |
download | rails-c863388039ff6e97447be86184d41fbf30f0f389.tar.gz rails-c863388039ff6e97447be86184d41fbf30f0f389.tar.bz2 rails-c863388039ff6e97447be86184d41fbf30f0f389.zip |
Add observing hooks to ARes
Diffstat (limited to 'activeresource/test')
-rw-r--r-- | activeresource/test/observing_test.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/activeresource/test/observing_test.rb b/activeresource/test/observing_test.rb new file mode 100644 index 0000000000..334b256772 --- /dev/null +++ b/activeresource/test/observing_test.rb @@ -0,0 +1,53 @@ +require 'abstract_unit' + +class ObservingTest < Test::Unit::TestCase + cattr_accessor :history + + class PersonObserver < ActiveModel::Observer + observe :person + + %w( after_create after_destroy after_save after_update + before_create before_destroy before_save before_update).each do |method| + define_method(method) { log method } + end + + private + def log(method) + (ObservingTest.history ||= []) << method.to_sym + end + end + + def setup + @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/people/1.xml", {}, @matz + mock.post "/people.xml", {}, @matz, 201, 'Location' => '/people/1.xml' + mock.put "/people/1.xml", {}, nil, 204 + mock.delete "/people/1.xml", {}, nil, 200 + end + + PersonObserver.instance + end + + def teardown + self.history = nil + end + + def test_create_fires_save_and_create_notifications + rick = Person.create(:name => 'Rick') + assert_equal [:before_save, :before_create, :after_create, :after_save], self.history + end + + def test_update_fires_save_and_update_notifications + person = Person.find(1) + person.save + assert_equal [:before_save, :before_update, :after_update, :after_save], self.history + end + + def test_destroy_fires_destroy_notifications + person = Person.find(1) + person.destroy + assert_equal [:before_destroy, :after_destroy], self.history + end +end |