2009-07-11 17:59:11 -04:00
|
|
|
require 'abstract_unit'
|
2009-09-05 20:24:43 -04:00
|
|
|
require 'fixtures/person'
|
|
|
|
require 'active_support/core_ext/hash/conversions'
|
2009-07-11 17:59:11 -04:00
|
|
|
|
|
|
|
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|
|
2009-10-04 04:57:35 -04:00
|
|
|
define_method(method) { |*| log method }
|
2009-07-11 17:59:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def log(method)
|
|
|
|
(ObservingTest.history ||= []) << method.to_sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
2011-05-17 19:30:43 -04:00
|
|
|
@matz = { 'person' => { :id => 1, :name => 'Matz' } }.to_json
|
2009-07-11 17:59:11 -04:00
|
|
|
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2011-05-17 19:30:43 -04:00
|
|
|
mock.get "/people/1.json", {}, @matz
|
|
|
|
mock.post "/people.json", {}, @matz, 201, 'Location' => '/people/1.json'
|
|
|
|
mock.put "/people/1.json", {}, nil, 204
|
|
|
|
mock.delete "/people/1.json", {}, nil, 200
|
2009-07-11 17:59:11 -04:00
|
|
|
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
|