-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathactive_resource_observer_test.rb
63 lines (49 loc) · 1.73 KB
/
active_resource_observer_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'minitest/autorun'
require 'active_resource'
require 'rails/observers/active_resource/observing'
ActiveResource::Base.prepend ActiveResource::Observing
require 'active_support/core_ext/hash/conversions'
class Person < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
end
class ActiveResourceObservingTest < ActiveSupport::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)
(ActiveResourceObservingTest.history ||= []) << method.to_sym
end
end
def setup
@matz = { 'person' => { :id => 1, :name => 'Matz' } }.to_json
ActiveResource::HttpMock.respond_to do |mock|
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
end
PersonObserver.instance
end
def teardown
self.history = nil
end
def test_create_fires_save_and_create_notifications
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