josevalim-inherited_resources 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -0
- data/README +30 -1
- data/lib/inherited_resources/base.rb +1 -3
- data/lib/inherited_resources/belongs_to_helpers.rb +6 -30
- data/lib/inherited_resources/class_methods.rb +241 -4
- data/lib/inherited_resources/polymorphic_helpers.rb +47 -2
- data/lib/inherited_resources/url_helpers.rb +2 -1
- data/test/belongs_to_test.rb +66 -76
- data/test/class_methods_test.rb +111 -2
- data/test/optional_belongs_to_test.rb +190 -0
- data/test/polymorphic_test.rb +111 -0
- data/test/singleton_test.rb +83 -0
- data/test/url_helpers_test.rb +36 -4
- data/test/views/products/edit.html.erb +1 -0
- data/test/views/products/index.html.erb +1 -0
- data/test/views/products/new.html.erb +1 -0
- data/test/views/products/show.html.erb +1 -0
- metadata +10 -7
- data/lib/inherited_resources/belongs_to.rb +0 -227
- data/test/belongs_to_base_test.rb +0 -268
- data/test/polymorphic_base_test.rb +0 -282
- data/test/singleton_base_test.rb +0 -226
data/test/singleton_base_test.rb
DELETED
@@ -1,226 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper'
|
2
|
-
|
3
|
-
# This test file is instead to test the how controller flow and actions
|
4
|
-
# using a belongs_to association. This is done using mocks a la rspec.
|
5
|
-
#
|
6
|
-
class Store
|
7
|
-
end
|
8
|
-
|
9
|
-
class Manager
|
10
|
-
def self.human_name; 'Manager'; end
|
11
|
-
end
|
12
|
-
|
13
|
-
class ManagersController < InheritedResources::Base
|
14
|
-
belongs_to :store, :singleton => true
|
15
|
-
end
|
16
|
-
|
17
|
-
# Create a TestHelper module with some helpers
|
18
|
-
module ManagerTestHelper
|
19
|
-
def setup
|
20
|
-
@controller = ManagersController.new
|
21
|
-
@controller.request = @request = ActionController::TestRequest.new
|
22
|
-
@controller.response = @response = ActionController::TestResponse.new
|
23
|
-
end
|
24
|
-
|
25
|
-
protected
|
26
|
-
def mock_store(stubs={})
|
27
|
-
@mock_store ||= mock(stubs)
|
28
|
-
end
|
29
|
-
|
30
|
-
def mock_manager(stubs={})
|
31
|
-
@mock_manager ||= mock(stubs)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class ShowActionSingletonTest < TEST_CLASS
|
36
|
-
include ManagerTestHelper
|
37
|
-
|
38
|
-
def test_expose_the_resquested_manager
|
39
|
-
Store.expects(:find).with('37').returns(mock_store)
|
40
|
-
mock_store.expects(:manager).returns(mock_manager)
|
41
|
-
get :show, :store_id => '37'
|
42
|
-
assert_equal mock_store, assigns(:store)
|
43
|
-
assert_equal mock_manager, assigns(:manager)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_controller_should_render_show
|
47
|
-
Store.stubs(:find).returns(mock_store(:manager => mock_manager))
|
48
|
-
get :show
|
49
|
-
assert_response :success
|
50
|
-
assert_equal 'Show HTML', @response.body.strip
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_render_exposed_manager_as_xml_when_mime_type_is_xml
|
54
|
-
@request.accept = 'application/xml'
|
55
|
-
Store.expects(:find).with('37').returns(mock_store)
|
56
|
-
mock_store.expects(:manager).returns(mock_manager)
|
57
|
-
mock_manager.expects(:to_xml).returns("Generated XML")
|
58
|
-
get :show, :id => '42', :store_id => '37'
|
59
|
-
assert_response :success
|
60
|
-
assert_equal 'Generated XML', @response.body
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class NewActionSingletonTest < TEST_CLASS
|
65
|
-
include ManagerTestHelper
|
66
|
-
|
67
|
-
def test_expose_a_new_manager
|
68
|
-
Store.expects(:find).with('37').returns(mock_store)
|
69
|
-
mock_store.expects(:build_manager).returns(mock_manager)
|
70
|
-
get :new, :store_id => '37'
|
71
|
-
assert_equal mock_store, assigns(:store)
|
72
|
-
assert_equal mock_manager, assigns(:manager)
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_controller_should_render_new
|
76
|
-
Store.stubs(:find).returns(mock_store)
|
77
|
-
mock_store.stubs(:build_manager).returns(mock_manager)
|
78
|
-
get :new
|
79
|
-
assert_response :success
|
80
|
-
assert_equal 'New HTML', @response.body.strip
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_render_exposed_a_new_manager_as_xml_when_mime_type_is_xml
|
84
|
-
@request.accept = 'application/xml'
|
85
|
-
Store.expects(:find).with('37').returns(mock_store)
|
86
|
-
mock_store.expects(:build_manager).returns(mock_manager)
|
87
|
-
mock_manager.expects(:to_xml).returns("Generated XML")
|
88
|
-
get :new, :store_id => '37'
|
89
|
-
assert_equal 'Generated XML', @response.body
|
90
|
-
assert_response :success
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
class EditActionSingletonTest < TEST_CLASS
|
95
|
-
include ManagerTestHelper
|
96
|
-
|
97
|
-
def test_expose_the_resquested_manager
|
98
|
-
Store.expects(:find).with('37').returns(mock_store)
|
99
|
-
mock_store.expects(:manager).returns(mock_manager)
|
100
|
-
get :edit, :store_id => '37'
|
101
|
-
assert_equal mock_store, assigns(:store)
|
102
|
-
assert_equal mock_manager, assigns(:manager)
|
103
|
-
assert_response :success
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_controller_should_render_edit
|
107
|
-
Store.stubs(:find).returns(mock_store)
|
108
|
-
mock_store.stubs(:manager).returns(mock_manager)
|
109
|
-
get :edit
|
110
|
-
assert_response :success
|
111
|
-
assert_equal 'Edit HTML', @response.body.strip
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
class CreateActionSingletonTest < TEST_CLASS
|
116
|
-
include ManagerTestHelper
|
117
|
-
|
118
|
-
def test_expose_a_newly_create_manager_when_saved_with_success
|
119
|
-
Store.expects(:find).with('37').returns(mock_store)
|
120
|
-
mock_store.expects(:build_manager).with({'these' => 'params'}).returns(mock_manager(:save => true))
|
121
|
-
post :create, :store_id => '37', :manager => {:these => 'params'}
|
122
|
-
assert_equal mock_store, assigns(:store)
|
123
|
-
assert_equal mock_manager, assigns(:manager)
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_redirect_to_the_created_manager
|
127
|
-
Store.stubs(:find).returns(mock_store)
|
128
|
-
mock_store.stubs(:build_manager).returns(mock_manager(:save => true))
|
129
|
-
@controller.expects(:resource_url).returns('http://test.host/').times(2)
|
130
|
-
post :create
|
131
|
-
assert_redirected_to 'http://test.host/'
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_show_flash_message_when_success
|
135
|
-
Store.stubs(:find).returns(mock_store)
|
136
|
-
mock_store.stubs(:build_manager).returns(mock_manager(:save => true))
|
137
|
-
post :create
|
138
|
-
assert_equal flash[:notice], 'Manager was successfully created.'
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_render_new_template_when_manager_cannot_be_saved
|
142
|
-
Store.stubs(:find).returns(mock_store)
|
143
|
-
mock_store.stubs(:build_manager).returns(mock_manager(:save => false, :errors => []))
|
144
|
-
post :create
|
145
|
-
assert_response :success
|
146
|
-
assert_template :new
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_dont_show_flash_message_when_manager_cannot_be_saved
|
150
|
-
Store.stubs(:find).returns(mock_store)
|
151
|
-
mock_store.stubs(:build_manager).returns(mock_manager(:save => false, :errors => []))
|
152
|
-
post :create
|
153
|
-
assert flash.empty?
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
class UpdateActionSingletonTest < TEST_CLASS
|
158
|
-
include ManagerTestHelper
|
159
|
-
|
160
|
-
def test_update_the_requested_object
|
161
|
-
Store.expects(:find).with('37').returns(mock_store(:manager => mock_manager))
|
162
|
-
mock_manager.expects(:update_attributes).with({'these' => 'params'}).returns(true)
|
163
|
-
put :update, :store_id => '37', :manager => {:these => 'params'}
|
164
|
-
assert_equal mock_store, assigns(:store)
|
165
|
-
assert_equal mock_manager, assigns(:manager)
|
166
|
-
end
|
167
|
-
|
168
|
-
def test_redirect_to_the_created_manager
|
169
|
-
Store.expects(:find).returns(mock_store(:manager => mock_manager))
|
170
|
-
mock_manager.stubs(:update_attributes).returns(true)
|
171
|
-
@controller.expects(:resource_url).returns('http://test.host/')
|
172
|
-
put :update
|
173
|
-
assert_redirected_to 'http://test.host/'
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_show_flash_message_when_success
|
177
|
-
Store.expects(:find).returns(mock_store(:manager => mock_manager))
|
178
|
-
mock_manager.stubs(:update_attributes).returns(true)
|
179
|
-
put :update
|
180
|
-
assert_equal flash[:notice], 'Manager was successfully updated.'
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_render_edit_template_when_manager_cannot_be_saved
|
184
|
-
Store.expects(:find).returns(mock_store(:manager => mock_manager(:errors => [])))
|
185
|
-
mock_manager.stubs(:update_attributes).returns(false)
|
186
|
-
put :update
|
187
|
-
assert_response :success
|
188
|
-
assert_template :edit
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_dont_show_flash_message_when_manager_cannot_be_saved
|
192
|
-
Store.expects(:find).returns(mock_store(:manager => mock_manager(:errors => [])))
|
193
|
-
mock_manager.stubs(:update_attributes).returns(false)
|
194
|
-
put :update
|
195
|
-
assert flash.empty?
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
class DestroyActionSingletonTest < TEST_CLASS
|
200
|
-
include ManagerTestHelper
|
201
|
-
|
202
|
-
def test_the_resquested_manager_is_destroyed
|
203
|
-
Store.expects(:find).with('37').returns(mock_store)
|
204
|
-
mock_store.expects(:manager).returns(mock_manager)
|
205
|
-
mock_manager.expects(:destroy)
|
206
|
-
delete :destroy, :store_id => '37'
|
207
|
-
assert_equal mock_store, assigns(:store)
|
208
|
-
assert_equal mock_manager, assigns(:manager)
|
209
|
-
end
|
210
|
-
|
211
|
-
def test_show_flash_message
|
212
|
-
Store.stubs(:find).returns(mock_store)
|
213
|
-
mock_store.stubs(:manager).returns(mock_manager(:destroy => true))
|
214
|
-
delete :destroy
|
215
|
-
assert_equal flash[:notice], 'Manager was successfully destroyed.'
|
216
|
-
end
|
217
|
-
|
218
|
-
def test_redirects_to_store_show
|
219
|
-
Store.stubs(:find).returns(mock_store)
|
220
|
-
mock_store.stubs(:manager).returns(mock_manager(:destroy => true))
|
221
|
-
@controller.expects(:collection_url).returns('http://test.host/')
|
222
|
-
delete :destroy
|
223
|
-
assert_redirected_to 'http://test.host/'
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|