josevalim-inherited_resources 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,268 +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 Post
7
- end
8
-
9
- class Comment
10
- def self.human_name; 'Comment'; end
11
- end
12
-
13
- class CommentsController < InheritedResources::Base
14
- belongs_to :post
15
- end
16
-
17
- # Create a TestHelper module with some helpers
18
- module CommentTestHelper
19
- def setup
20
- @controller = CommentsController.new
21
- @controller.request = @request = ActionController::TestRequest.new
22
- @controller.response = @response = ActionController::TestResponse.new
23
- end
24
-
25
- protected
26
- def mock_post(stubs={})
27
- @mock_post ||= mock(stubs)
28
- end
29
-
30
- def mock_comment(stubs={})
31
- @mock_comment ||= mock(stubs)
32
- end
33
- end
34
-
35
- class IndexActionBelongsToTest < TEST_CLASS
36
- include CommentTestHelper
37
-
38
- def test_expose_all_comments_as_instance_variable
39
- Post.expects(:find).with('37').returns(mock_post)
40
- mock_post.expects(:comments).returns(Comment)
41
- Comment.expects(:find).with(:all).returns([mock_comment])
42
- get :index, :post_id => '37'
43
- assert_equal mock_post, assigns(:post)
44
- assert_equal [mock_comment], assigns(:comments)
45
- end
46
-
47
- def test_controller_should_render_index
48
- Post.stubs(:find).returns(mock_post(:comments => Comment))
49
- Comment.stubs(:find).returns([mock_comment])
50
- get :index
51
- assert_response :success
52
- assert_equal 'Index HTML', @response.body.strip
53
- end
54
-
55
- def test_render_all_comments_as_xml_when_mime_type_is_xml
56
- @request.accept = 'application/xml'
57
- Post.expects(:find).with('37').returns(mock_post)
58
- mock_post.expects(:comments).returns(Comment)
59
- Comment.expects(:find).with(:all).returns(mock_comment)
60
- mock_comment.expects(:to_xml).returns('Generated XML')
61
- get :index, :post_id => '37'
62
- assert_response :success
63
- assert_equal 'Generated XML', @response.body
64
- end
65
- end
66
-
67
- class ShowActionBelongsToTest < TEST_CLASS
68
- include CommentTestHelper
69
-
70
- def test_expose_the_resquested_comment
71
- Post.expects(:find).with('37').returns(mock_post)
72
- mock_post.expects(:comments).returns(Comment)
73
- Comment.expects(:find).with('42').returns(mock_comment)
74
- get :show, :id => '42', :post_id => '37'
75
- assert_equal mock_post, assigns(:post)
76
- assert_equal mock_comment, assigns(:comment)
77
- end
78
-
79
- def test_controller_should_render_show
80
- Post.stubs(:find).returns(mock_post(:comments => Comment))
81
- Comment.stubs(:find).returns(mock_comment)
82
- get :show
83
- assert_response :success
84
- assert_equal 'Show HTML', @response.body.strip
85
- end
86
-
87
- def test_render_exposed_comment_as_xml_when_mime_type_is_xml
88
- @request.accept = 'application/xml'
89
- Post.expects(:find).with('37').returns(mock_post)
90
- mock_post.expects(:comments).returns(Comment)
91
- Comment.expects(:find).with('42').returns(mock_comment)
92
- mock_comment.expects(:to_xml).returns("Generated XML")
93
- get :show, :id => '42', :post_id => '37'
94
- assert_response :success
95
- assert_equal 'Generated XML', @response.body
96
- end
97
- end
98
-
99
- class NewActionBelongsToTest < TEST_CLASS
100
- include CommentTestHelper
101
-
102
- def test_expose_a_new_comment
103
- Post.expects(:find).with('37').returns(mock_post)
104
- mock_post.expects(:comments).returns(Comment)
105
- Comment.expects(:build).returns(mock_comment)
106
- get :new, :post_id => '37'
107
- assert_equal mock_post, assigns(:post)
108
- assert_equal mock_comment, assigns(:comment)
109
- end
110
-
111
- def test_controller_should_render_new
112
- Post.stubs(:find).returns(mock_post(:comments => Comment))
113
- Comment.stubs(:build).returns(mock_comment)
114
- get :new
115
- assert_response :success
116
- assert_equal 'New HTML', @response.body.strip
117
- end
118
-
119
- def test_render_exposed_a_new_comment_as_xml_when_mime_type_is_xml
120
- @request.accept = 'application/xml'
121
- Post.expects(:find).with('37').returns(mock_post)
122
- mock_post.expects(:comments).returns(Comment)
123
- Comment.expects(:build).returns(mock_comment)
124
- mock_comment.expects(:to_xml).returns("Generated XML")
125
- get :new, :post_id => '37'
126
- assert_equal 'Generated XML', @response.body
127
- assert_response :success
128
- end
129
- end
130
-
131
- class EditActionBelongsToTest < TEST_CLASS
132
- include CommentTestHelper
133
-
134
- def test_expose_the_resquested_comment
135
- Post.expects(:find).with('37').returns(mock_post)
136
- mock_post.expects(:comments).returns(Comment)
137
- Comment.expects(:find).with('42').returns(mock_comment)
138
- get :edit, :id => '42', :post_id => '37'
139
- assert_equal mock_post, assigns(:post)
140
- assert_equal mock_comment, assigns(:comment)
141
- assert_response :success
142
- end
143
-
144
- def test_controller_should_render_edit
145
- Post.stubs(:find).returns(mock_post(:comments => Comment))
146
- Comment.stubs(:find).returns(mock_comment)
147
- get :edit
148
- assert_response :success
149
- assert_equal 'Edit HTML', @response.body.strip
150
- end
151
- end
152
-
153
- class CreateActionBelongsToTest < TEST_CLASS
154
- include CommentTestHelper
155
-
156
- def test_expose_a_newly_create_comment_when_saved_with_success
157
- Post.expects(:find).with('37').returns(mock_post)
158
- mock_post.expects(:comments).returns(Comment)
159
- Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(:save => true))
160
- post :create, :post_id => '37', :comment => {:these => 'params'}
161
- assert_equal mock_post, assigns(:post)
162
- assert_equal mock_comment, assigns(:comment)
163
- end
164
-
165
- def test_redirect_to_the_created_comment
166
- Post.stubs(:find).returns(mock_post(:comments => Comment))
167
- Comment.stubs(:build).returns(mock_comment(:save => true))
168
- @controller.expects(:resource_url).returns('http://test.host/').times(2)
169
- post :create
170
- assert_redirected_to 'http://test.host/'
171
- end
172
-
173
- def test_show_flash_message_when_success
174
- Post.stubs(:find).returns(mock_post(:comments => Comment))
175
- Comment.stubs(:build).returns(mock_comment(:save => true))
176
- post :create
177
- assert_equal flash[:notice], 'Comment was successfully created.'
178
- end
179
-
180
- def test_render_new_template_when_comment_cannot_be_saved
181
- Post.stubs(:find).returns(mock_post(:comments => Comment))
182
- Comment.stubs(:build).returns(mock_comment(:save => false, :errors => []))
183
- post :create
184
- assert_response :success
185
- assert_template :new
186
- end
187
-
188
- def test_dont_show_flash_message_when_comment_cannot_be_saved
189
- Post.stubs(:find).returns(mock_post(:comments => Comment))
190
- Comment.stubs(:build).returns(mock_comment(:save => false, :errors => []))
191
- post :create
192
- assert flash.empty?
193
- end
194
- end
195
-
196
- class UpdateActionBelongsToTest < TEST_CLASS
197
- include CommentTestHelper
198
-
199
- def test_update_the_requested_object
200
- Post.expects(:find).with('37').returns(mock_post)
201
- mock_post.expects(:comments).returns(Comment)
202
- Comment.expects(:find).with('42').returns(mock_comment)
203
- mock_comment.expects(:update_attributes).with({'these' => 'params'}).returns(true)
204
- put :update, :id => '42', :post_id => '37', :comment => {:these => 'params'}
205
- assert_equal mock_post, assigns(:post)
206
- assert_equal mock_comment, assigns(:comment)
207
- end
208
-
209
- def test_redirect_to_the_created_comment
210
- Post.stubs(:find).returns(mock_post(:comments => Comment))
211
- Comment.stubs(:find).returns(mock_comment(:update_attributes => true))
212
- @controller.expects(:resource_url).returns('http://test.host/')
213
- put :update
214
- assert_redirected_to 'http://test.host/'
215
- end
216
-
217
- def test_show_flash_message_when_success
218
- Post.stubs(:find).returns(mock_post(:comments => Comment))
219
- Comment.stubs(:find).returns(mock_comment(:update_attributes => true))
220
- put :update
221
- assert_equal flash[:notice], 'Comment was successfully updated.'
222
- end
223
-
224
- def test_render_edit_template_when_comment_cannot_be_saved
225
- Post.stubs(:find).returns(mock_post(:comments => Comment))
226
- Comment.stubs(:find).returns(mock_comment(:update_attributes => false, :errors => []))
227
- put :update
228
- assert_response :success
229
- assert_template :edit
230
- end
231
-
232
- def test_dont_show_flash_message_when_comment_cannot_be_saved
233
- Post.stubs(:find).returns(mock_post(:comments => Comment))
234
- Comment.stubs(:find).returns(mock_comment(:update_attributes => false, :errors => []))
235
- put :update
236
- assert flash.empty?
237
- end
238
- end
239
-
240
- class DestroyActionBelongsToTest < TEST_CLASS
241
- include CommentTestHelper
242
-
243
- def test_the_resquested_comment_is_destroyed
244
- Post.expects(:find).with('37').returns(mock_post)
245
- mock_post.expects(:comments).returns(Comment)
246
- Comment.expects(:find).with('42').returns(mock_comment)
247
- mock_comment.expects(:destroy)
248
- delete :destroy, :id => '42', :post_id => '37'
249
- assert_equal mock_post, assigns(:post)
250
- assert_equal mock_comment, assigns(:comment)
251
- end
252
-
253
- def test_show_flash_message
254
- Post.stubs(:find).returns(mock_post(:comments => Comment))
255
- Comment.stubs(:find).returns(mock_comment(:destroy => true))
256
- delete :destroy
257
- assert_equal flash[:notice], 'Comment was successfully destroyed.'
258
- end
259
-
260
- def test_redirects_to_comments_list
261
- Post.stubs(:find).returns(mock_post(:comments => Comment))
262
- Comment.stubs(:find).returns(mock_comment(:destroy => true))
263
- @controller.expects(:collection_url).returns('http://test.host/')
264
- delete :destroy
265
- assert_redirected_to 'http://test.host/'
266
- end
267
- end
268
-
@@ -1,282 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class Factory; end
4
- class Company; end
5
-
6
- class Employee
7
- def self.human_name; 'Employee'; end
8
- end
9
-
10
- class EmployeesController < InheritedResources::Base
11
- belongs_to :factory, :company, :polymorphic => true
12
- end
13
-
14
- # Create a TestHelper module with some helpers
15
- module EmployeeTestHelper
16
- def setup
17
- @controller = EmployeesController.new
18
- @controller.request = @request = ActionController::TestRequest.new
19
- @controller.response = @response = ActionController::TestResponse.new
20
- end
21
-
22
- protected
23
- def mock_factory(stubs={})
24
- @mock_factory ||= mock(stubs)
25
- end
26
-
27
- def mock_employee(stubs={})
28
- @mock_employee ||= mock(stubs)
29
- end
30
- end
31
-
32
- class IndexActionPolymorphicTest < TEST_CLASS
33
- include EmployeeTestHelper
34
-
35
- def test_expose_all_employees_as_instance_variable
36
- Factory.expects(:find).with('37').returns(mock_factory)
37
- mock_factory.expects(:employees).returns(Employee)
38
- Employee.expects(:find).with(:all).returns([mock_employee])
39
- get :index, :factory_id => '37'
40
- assert_equal mock_factory, assigns(:factory)
41
- assert_equal [mock_employee], assigns(:employees)
42
- end
43
-
44
- def test_controller_should_render_index
45
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
46
- Employee.stubs(:find).returns([mock_employee])
47
- get :index, :factory_id => '37'
48
- assert_response :success
49
- assert_equal 'Index HTML', @response.body.strip
50
- end
51
-
52
- def test_render_all_employees_as_xml_when_mime_type_is_xml
53
- @request.accept = 'application/xml'
54
- Factory.expects(:find).with('37').returns(mock_factory)
55
- mock_factory.expects(:employees).returns(Employee)
56
- Employee.expects(:find).with(:all).returns(mock_employee)
57
- mock_employee.expects(:to_xml).returns('Generated XML')
58
- get :index, :factory_id => '37'
59
- assert_response :success
60
- assert_equal 'Generated XML', @response.body
61
- end
62
- end
63
-
64
- class ShowActionPolymorphicTest < TEST_CLASS
65
- include EmployeeTestHelper
66
-
67
- def test_expose_the_resquested_employee
68
- Factory.expects(:find).with('37').returns(mock_factory)
69
- mock_factory.expects(:employees).returns(Employee)
70
- Employee.expects(:find).with('42').returns(mock_employee)
71
- get :show, :id => '42', :factory_id => '37'
72
- assert_equal mock_factory, assigns(:factory)
73
- assert_equal mock_employee, assigns(:employee)
74
- end
75
-
76
- def test_controller_should_render_show
77
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
78
- Employee.stubs(:find).returns(mock_employee)
79
- get :show, :factory_id => '37'
80
- assert_response :success
81
- assert_equal 'Show HTML', @response.body.strip
82
- end
83
-
84
- def test_render_exposed_employee_as_xml_when_mime_type_is_xml
85
- @request.accept = 'application/xml'
86
- Factory.expects(:find).with('37').returns(mock_factory)
87
- mock_factory.expects(:employees).returns(Employee)
88
- Employee.expects(:find).with('42').returns(mock_employee)
89
- mock_employee.expects(:to_xml).returns("Generated XML")
90
- get :show, :id => '42', :factory_id => '37'
91
- assert_response :success
92
- assert_equal 'Generated XML', @response.body
93
- end
94
- end
95
-
96
- class NewActionPolymorphicTest < TEST_CLASS
97
- include EmployeeTestHelper
98
-
99
- def test_expose_a_new_employee
100
- Factory.expects(:find).with('37').returns(mock_factory)
101
- mock_factory.expects(:employees).returns(Employee)
102
- Employee.expects(:build).returns(mock_employee)
103
- get :new, :factory_id => '37'
104
- assert_equal mock_factory, assigns(:factory)
105
- assert_equal mock_employee, assigns(:employee)
106
- end
107
-
108
- def test_controller_should_render_new
109
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
110
- Employee.stubs(:build).returns(mock_employee)
111
- get :new, :factory_id => '37'
112
- assert_response :success
113
- assert_equal 'New HTML', @response.body.strip
114
- end
115
-
116
- def test_render_exposed_a_new_employee_as_xml_when_mime_type_is_xml
117
- @request.accept = 'application/xml'
118
- Factory.expects(:find).with('37').returns(mock_factory)
119
- mock_factory.expects(:employees).returns(Employee)
120
- Employee.expects(:build).returns(mock_employee)
121
- mock_employee.expects(:to_xml).returns("Generated XML")
122
- get :new, :factory_id => '37'
123
- assert_equal 'Generated XML', @response.body
124
- assert_response :success
125
- end
126
- end
127
-
128
- class EditActionPolymorphicTest < TEST_CLASS
129
- include EmployeeTestHelper
130
-
131
- def test_expose_the_resquested_employee
132
- Factory.expects(:find).with('37').returns(mock_factory)
133
- mock_factory.expects(:employees).returns(Employee)
134
- Employee.expects(:find).with('42').returns(mock_employee)
135
- get :edit, :id => '42', :factory_id => '37'
136
- assert_equal mock_factory, assigns(:factory)
137
- assert_equal mock_employee, assigns(:employee)
138
- assert_response :success
139
- end
140
-
141
- def test_controller_should_render_edit
142
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
143
- Employee.stubs(:find).returns(mock_employee)
144
- get :edit, :factory_id => '37'
145
- assert_response :success
146
- assert_equal 'Edit HTML', @response.body.strip
147
- end
148
- end
149
-
150
- class CreateActionPolymorphicTest < TEST_CLASS
151
- include EmployeeTestHelper
152
-
153
- def test_expose_a_newly_create_employee_when_saved_with_success
154
- Factory.expects(:find).with('37').returns(mock_factory)
155
- mock_factory.expects(:employees).returns(Employee)
156
- Employee.expects(:build).with({'these' => 'params'}).returns(mock_employee(:save => true))
157
- post :create, :factory_id => '37', :employee => {:these => 'params'}
158
- assert_equal mock_factory, assigns(:factory)
159
- assert_equal mock_employee, assigns(:employee)
160
- end
161
-
162
- def test_redirect_to_the_created_employee
163
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
164
- Employee.stubs(:build).returns(mock_employee(:save => true))
165
- @controller.expects(:resource_url).returns('http://test.host/').times(2)
166
- post :create, :factory_id => '37'
167
- assert_redirected_to 'http://test.host/'
168
- end
169
-
170
- def test_show_flash_message_when_success
171
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
172
- Employee.stubs(:build).returns(mock_employee(:save => true))
173
- post :create, :factory_id => '37'
174
- assert_equal flash[:notice], 'Employee was successfully created.'
175
- end
176
-
177
- def test_render_new_template_when_employee_cannot_be_saved
178
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
179
- Employee.stubs(:build).returns(mock_employee(:save => false, :errors => []))
180
- post :create, :factory_id => '37'
181
- assert_response :success
182
- assert_template :new
183
- end
184
-
185
- def test_dont_show_flash_message_when_employee_cannot_be_saved
186
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
187
- Employee.stubs(:build).returns(mock_employee(:save => false, :errors => []))
188
- post :create, :factory_id => '37'
189
- assert flash.empty?
190
- end
191
- end
192
-
193
- class UpdateActionPolymorphicTest < TEST_CLASS
194
- include EmployeeTestHelper
195
-
196
- def test_update_the_requested_object
197
- Factory.expects(:find).with('37').returns(mock_factory)
198
- mock_factory.expects(:employees).returns(Employee)
199
- Employee.expects(:find).with('42').returns(mock_employee)
200
- mock_employee.expects(:update_attributes).with({'these' => 'params'}).returns(true)
201
- put :update, :id => '42', :factory_id => '37', :employee => {:these => 'params'}
202
- assert_equal mock_factory, assigns(:factory)
203
- assert_equal mock_employee, assigns(:employee)
204
- end
205
-
206
- def test_redirect_to_the_created_employee
207
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
208
- Employee.stubs(:find).returns(mock_employee(:update_attributes => true))
209
- @controller.expects(:resource_url).returns('http://test.host/')
210
- put :update, :factory_id => '37'
211
- assert_redirected_to 'http://test.host/'
212
- end
213
-
214
- def test_show_flash_message_when_success
215
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
216
- Employee.stubs(:find).returns(mock_employee(:update_attributes => true))
217
- put :update, :factory_id => '37'
218
- assert_equal flash[:notice], 'Employee was successfully updated.'
219
- end
220
-
221
- def test_render_edit_template_when_employee_cannot_be_saved
222
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
223
- Employee.stubs(:find).returns(mock_employee(:update_attributes => false, :errors => []))
224
- put :update, :factory_id => '37'
225
- assert_response :success
226
- assert_template :edit
227
- end
228
-
229
- def test_dont_show_flash_message_when_employee_cannot_be_saved
230
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
231
- Employee.stubs(:find).returns(mock_employee(:update_attributes => false, :errors => []))
232
- put :update, :factory_id => '37'
233
- assert flash.empty?
234
- end
235
- end
236
-
237
- class DestroyActionPolymorphicTest < TEST_CLASS
238
- include EmployeeTestHelper
239
-
240
- def test_the_resquested_employee_is_destroyed
241
- Factory.expects(:find).with('37').returns(mock_factory)
242
- mock_factory.expects(:employees).returns(Employee)
243
- Employee.expects(:find).with('42').returns(mock_employee)
244
- mock_employee.expects(:destroy)
245
- delete :destroy, :id => '42', :factory_id => '37'
246
- assert_equal mock_factory, assigns(:factory)
247
- assert_equal mock_employee, assigns(:employee)
248
- end
249
-
250
- def test_show_flash_message
251
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
252
- Employee.stubs(:find).returns(mock_employee(:destroy => true))
253
- delete :destroy, :factory_id => '37'
254
- assert_equal flash[:notice], 'Employee was successfully destroyed.'
255
- end
256
-
257
- def test_redirects_to_employees_list
258
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
259
- Employee.stubs(:find).returns(mock_employee(:destroy => true))
260
- @controller.expects(:collection_url).returns('http://test.host/')
261
- delete :destroy, :factory_id => '37'
262
- assert_redirected_to 'http://test.host/'
263
- end
264
- end
265
- class PolymorphicHelpersTest < TEST_CLASS
266
- include EmployeeTestHelper
267
-
268
- def test_polymorphic_helpers
269
- new_factory = Factory.new
270
- Factory.expects(:find).with('37').returns(new_factory)
271
- new_factory.expects(:employees).returns(Employee)
272
- Employee.expects(:find).with(:all).returns([mock_employee])
273
- get :index, :factory_id => '37'
274
-
275
- assert @controller.send(:parent?)
276
- assert_equal :factory, assigns(:parent_type)
277
- assert_equal :factory, @controller.send(:parent_type)
278
- assert_equal Factory, @controller.send(:parent_class)
279
- assert_equal new_factory, assigns(:factory)
280
- assert_equal new_factory, @controller.send(:parent)
281
- end
282
- end