emmanuel-inherited_resources 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Student;
4
+ def self.human_name; 'Student'; end
5
+ end
6
+
7
+ class ApplicationController < ActionController::Base
8
+ include InheritedResources::DSL
9
+ end
10
+
11
+ class StudentsController < ApplicationController
12
+ inherit_resources
13
+ respond_to :html, :xml
14
+
15
+ def edit
16
+ edit! do |format|
17
+ format.xml { render :text => 'Render XML' }
18
+ end
19
+ end
20
+
21
+ def new
22
+ @something = 'magical'
23
+ new!
24
+ end
25
+
26
+ create!(:location => "http://test.host/") do |success, failure|
27
+ success.html { render :text => "I won't redirect!" }
28
+ failure.xml { render :text => "I shouldn't be rendered" }
29
+ end
30
+
31
+ update! do |success, failure|
32
+ success.html { redirect_to(resource_url) }
33
+ failure.html { render :text => "I won't render!" }
34
+ end
35
+
36
+ destroy! do |format|
37
+ format.html { render :text => "Destroyed!" }
38
+ end
39
+ end
40
+
41
+ class AliasesTest < ActionController::TestCase
42
+ tests StudentsController
43
+
44
+ def test_assignments_before_calling_alias
45
+ Student.stubs(:new).returns(mock_student)
46
+ get :new
47
+ assert_response :success
48
+ assert_equal 'magical', assigns(:something)
49
+ end
50
+
51
+ def test_controller_should_render_new
52
+ Student.stubs(:new).returns(mock_student)
53
+ get :new
54
+ assert_response :success
55
+ assert_equal 'New HTML', @response.body.strip
56
+ end
57
+
58
+ def test_expose_the_resquested_user_on_edit
59
+ Student.expects(:find).with('42').returns(mock_student)
60
+ get :edit, :id => '42'
61
+ assert_equal mock_student, assigns(:student)
62
+ assert_response :success
63
+ end
64
+
65
+ def test_controller_should_render_edit
66
+ Student.stubs(:find).returns(mock_student)
67
+ get :edit
68
+ assert_response :success
69
+ assert_equal 'Edit HTML', @response.body.strip
70
+ end
71
+
72
+ def test_render_xml_when_it_is_given_as_a_block
73
+ @request.accept = 'application/xml'
74
+ Student.stubs(:find).returns(mock_student)
75
+ get :edit
76
+ assert_response :success
77
+ assert_equal 'Render XML', @response.body
78
+ end
79
+
80
+ def test_is_not_redirected_on_create_with_success_if_success_block_is_given
81
+ Student.stubs(:new).returns(mock_student(:save => true))
82
+ @controller.stubs(:resource_url).returns('http://test.host/')
83
+ post :create
84
+ assert_response :success
85
+ assert_equal "I won't redirect!", @response.body
86
+ end
87
+
88
+ def test_dumb_responder_quietly_receives_everything_on_failure
89
+ @request.accept = 'text/html'
90
+ Student.stubs(:new).returns(mock_student(:save => false, :errors => {:some => :error}))
91
+ @controller.stubs(:resource_url).returns('http://test.host/')
92
+ post :create
93
+ assert_response :success
94
+ assert_equal "New HTML", @response.body.strip
95
+ end
96
+
97
+ def test_html_is_the_default_when_only_xml_is_overwriten
98
+ @request.accept = '*/*'
99
+ Student.stubs(:new).returns(mock_student(:save => false, :errors => {:some => :error}))
100
+ @controller.stubs(:resource_url).returns('http://test.host/')
101
+ post :create
102
+ assert_response :success
103
+ assert_equal "New HTML", @response.body.strip
104
+ end
105
+
106
+ def test_wont_render_edit_template_on_update_with_failure_if_failure_block_is_given
107
+ Student.stubs(:find).returns(mock_student(:update_attributes => false))
108
+ put :update
109
+ assert_response :success
110
+ assert_equal "I won't render!", @response.body
111
+ end
112
+
113
+ def test_dumb_responder_quietly_receives_everything_on_success
114
+ Student.stubs(:find).returns(mock_student(:update_attributes => true))
115
+ @controller.stubs(:resource_url).returns('http://test.host/')
116
+ put :update, :id => '42', :student => {:these => 'params'}
117
+ assert_equal mock_student, assigns(:student)
118
+ end
119
+
120
+ def test_block_is_called_when_student_is_destroyed
121
+ Student.stubs(:find).returns(mock_student(:destroy => true))
122
+ delete :destroy
123
+ assert_response :success
124
+ assert_equal "Destroyed!", @response.body
125
+ end
126
+
127
+ def test_options_are_used_in_respond_with
128
+ @request.accept = "application/xml"
129
+ Student.stubs(:new).returns(mock_student(:save => true, :to_xml => "XML"))
130
+ post :create
131
+ assert_equal "http://test.host/", @response.location
132
+ end
133
+
134
+ protected
135
+ def mock_student(stubs={})
136
+ @mock_student ||= mock(stubs)
137
+ end
138
+ end
139
+
@@ -0,0 +1,76 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Pet
4
+ def self.human_name; 'Pet'; end
5
+ end
6
+
7
+ class PetsController < InheritedResources::Base
8
+ attr_accessor :current_user
9
+
10
+ def edit
11
+ @pet = 'new pet'
12
+ edit!
13
+ end
14
+
15
+ protected
16
+ def collection
17
+ @pets ||= end_of_association_chain.all
18
+ end
19
+
20
+ def begin_of_association_chain
21
+ @current_user
22
+ end
23
+ end
24
+
25
+ class AssociationChainBaseHelpersTest < ActionController::TestCase
26
+ tests PetsController
27
+
28
+ def setup
29
+ @controller.current_user = mock()
30
+ end
31
+
32
+ def test_begin_of_association_chain_is_called_on_index
33
+ @controller.current_user.expects(:pets).returns(Pet)
34
+ Pet.expects(:all).returns(mock_pet)
35
+ get :index
36
+ assert_response :success
37
+ assert 'Index HTML', @response.body.strip
38
+ end
39
+
40
+ def test_begin_of_association_chain_is_called_on_new
41
+ @controller.current_user.expects(:pets).returns(Pet)
42
+ Pet.expects(:build).returns(mock_pet)
43
+ get :new
44
+ assert_response :success
45
+ assert 'New HTML', @response.body.strip
46
+ end
47
+
48
+ def test_begin_of_association_chain_is_called_on_show
49
+ @controller.current_user.expects(:pets).returns(Pet)
50
+ Pet.expects(:find).with('47').returns(mock_pet)
51
+ get :show, :id => '47'
52
+ assert_response :success
53
+ assert 'Show HTML', @response.body.strip
54
+ end
55
+
56
+ def test_instance_variable_should_not_be_set_if_already_defined
57
+ @controller.current_user.expects(:pets).never
58
+ Pet.expects(:find).never
59
+ get :edit
60
+ assert_response :success
61
+ assert_equal 'new pet', assigns(:pet)
62
+ end
63
+
64
+ def test_model_is_not_initialized_with_nil
65
+ @controller.current_user.expects(:pets).returns(Pet)
66
+ Pet.expects(:build).with({}).returns(mock_pet)
67
+ get :new
68
+ assert mock_pet, assigns(:pet)
69
+ end
70
+
71
+ protected
72
+ def mock_pet(stubs={})
73
+ @mock_pet ||= mock(stubs)
74
+ end
75
+
76
+ end
data/test/base_test.rb ADDED
@@ -0,0 +1,219 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class User
4
+ def self.human_name; 'User'; end
5
+ end
6
+
7
+ class AccountsController < InheritedResources::Base
8
+ end
9
+
10
+ class UsersController < AccountsController
11
+ respond_to :html, :xml
12
+ end
13
+
14
+ module UserTestHelper
15
+ def setup
16
+ @controller = UsersController.new
17
+ @controller.request = @request = ActionController::TestRequest.new
18
+ @controller.response = @response = ActionController::TestResponse.new
19
+ end
20
+
21
+ protected
22
+ def mock_user(stubs={})
23
+ @mock_user ||= mock(stubs)
24
+ end
25
+ end
26
+
27
+ class IndexActionBaseTest < ActionController::TestCase
28
+ include UserTestHelper
29
+
30
+ def test_expose_all_users_as_instance_variable
31
+ User.expects(:find).with(:all).returns([mock_user])
32
+ get :index
33
+ assert_equal [mock_user], assigns(:users)
34
+ end
35
+
36
+ def test_controller_should_render_index
37
+ User.stubs(:find).returns([mock_user])
38
+ get :index
39
+ assert_response :success
40
+ assert_equal 'Index HTML', @response.body.strip
41
+ end
42
+
43
+ def test_render_all_users_as_xml_when_mime_type_is_xml
44
+ @request.accept = 'application/xml'
45
+ User.expects(:find).with(:all).returns(mock_user)
46
+ mock_user.expects(:to_xml).returns('Generated XML')
47
+ get :index
48
+ assert_response :success
49
+ assert_equal 'Generated XML', @response.body
50
+ end
51
+ end
52
+
53
+ class ShowActionBaseTest < ActionController::TestCase
54
+ include UserTestHelper
55
+
56
+ def test_expose_the_resquested_user
57
+ User.expects(:find).with('42').returns(mock_user)
58
+ get :show, :id => '42'
59
+ assert_equal mock_user, assigns(:user)
60
+ end
61
+
62
+ def test_controller_should_render_show
63
+ User.stubs(:find).returns(mock_user)
64
+ get :show
65
+ assert_response :success
66
+ assert_equal 'Show HTML', @response.body.strip
67
+ end
68
+
69
+ def test_render_exposed_user_as_xml_when_mime_type_is_xml
70
+ @request.accept = 'application/xml'
71
+ User.expects(:find).with('42').returns(mock_user)
72
+ mock_user.expects(:to_xml).returns("Generated XML")
73
+ get :show, :id => '42'
74
+ assert_response :success
75
+ assert_equal 'Generated XML', @response.body
76
+ end
77
+ end
78
+
79
+ class NewActionBaseTest < ActionController::TestCase
80
+ include UserTestHelper
81
+
82
+ def test_expose_a_new_user
83
+ User.expects(:new).returns(mock_user)
84
+ get :new
85
+ assert_equal mock_user, assigns(:user)
86
+ end
87
+
88
+ def test_controller_should_render_new
89
+ User.stubs(:new).returns(mock_user)
90
+ get :new
91
+ assert_response :success
92
+ assert_equal 'New HTML', @response.body.strip
93
+ end
94
+
95
+ def test_render_exposed_a_new_user_as_xml_when_mime_type_is_xml
96
+ @request.accept = 'application/xml'
97
+ User.expects(:new).returns(mock_user)
98
+ mock_user.expects(:to_xml).returns("Generated XML")
99
+ get :new
100
+ assert_response :success
101
+ assert_equal 'Generated XML', @response.body
102
+ end
103
+ end
104
+
105
+ class EditActionBaseTest < ActionController::TestCase
106
+ include UserTestHelper
107
+
108
+ def test_expose_the_resquested_user
109
+ User.expects(:find).with('42').returns(mock_user)
110
+ get :edit, :id => '42'
111
+ assert_response :success
112
+ assert_equal mock_user, assigns(:user)
113
+ end
114
+
115
+ def test_controller_should_render_edit
116
+ User.stubs(:find).returns(mock_user)
117
+ get :edit
118
+ assert_response :success
119
+ assert_equal 'Edit HTML', @response.body.strip
120
+ end
121
+ end
122
+
123
+ class CreateActionBaseTest < ActionController::TestCase
124
+ include UserTestHelper
125
+
126
+ def test_expose_a_newly_create_user_when_saved_with_success
127
+ User.expects(:new).with({'these' => 'params'}).returns(mock_user(:save => true))
128
+ post :create, :user => {:these => 'params'}
129
+ assert_equal mock_user, assigns(:user)
130
+ end
131
+
132
+ def test_redirect_to_the_created_user
133
+ User.stubs(:new).returns(mock_user(:save => true))
134
+ @controller.expects(:resource_url).returns('http://test.host/')
135
+ post :create
136
+ assert_redirected_to 'http://test.host/'
137
+ end
138
+
139
+ def test_show_flash_message_when_success
140
+ User.stubs(:new).returns(mock_user(:save => true))
141
+ post :create
142
+ assert_equal flash[:notice], 'User was successfully created.'
143
+ end
144
+
145
+ def test_render_new_template_when_user_cannot_be_saved
146
+ User.stubs(:new).returns(mock_user(:save => false, :errors => {:some => :error}))
147
+ post :create
148
+ assert_response :success
149
+ assert_template :new
150
+ end
151
+
152
+ def test_dont_show_flash_message_when_user_cannot_be_saved
153
+ User.stubs(:new).returns(mock_user(:save => false, :errors => {:some => :error}))
154
+ post :create
155
+ assert flash.empty?
156
+ end
157
+ end
158
+
159
+ class UpdateActionBaseTest < ActionController::TestCase
160
+ include UserTestHelper
161
+
162
+ def test_update_the_requested_object
163
+ User.expects(:find).with('42').returns(mock_user)
164
+ mock_user.expects(:update_attributes).with({'these' => 'params'}).returns(true)
165
+ put :update, :id => '42', :user => {:these => 'params'}
166
+ assert_equal mock_user, assigns(:user)
167
+ end
168
+
169
+ def test_redirect_to_the_created_user
170
+ User.stubs(:find).returns(mock_user(:update_attributes => 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
+ User.stubs(:find).returns(mock_user(:update_attributes => true))
178
+ put :update
179
+ assert_equal flash[:notice], 'User was successfully updated.'
180
+ end
181
+
182
+ def test_render_edit_template_when_user_cannot_be_saved
183
+ User.stubs(:find).returns(mock_user(:update_attributes => false, :errors => {:some => :error}))
184
+ put :update
185
+ assert_response :success
186
+ assert_template :edit
187
+ end
188
+
189
+ def test_dont_show_flash_message_when_user_cannot_be_saved
190
+ User.stubs(:find).returns(mock_user(:update_attributes => false, :errors => {:some => :error}))
191
+ put :update
192
+ assert flash.empty?
193
+ end
194
+ end
195
+
196
+ class DestroyActionBaseTest < ActionController::TestCase
197
+ include UserTestHelper
198
+
199
+ def test_the_resquested_user_is_destroyed
200
+ User.expects(:find).with('42').returns(mock_user)
201
+ mock_user.expects(:destroy)
202
+ delete :destroy, :id => '42'
203
+ assert_equal mock_user, assigns(:user)
204
+ end
205
+
206
+ def test_show_flash_message
207
+ User.stubs(:find).returns(mock_user(:destroy => true))
208
+ delete :destroy
209
+ assert_equal flash[:notice], 'User was successfully destroyed.'
210
+ end
211
+
212
+ def test_redirects_to_users_list
213
+ User.stubs(:find).returns(mock_user(:destroy => true))
214
+ @controller.expects(:collection_url).returns('http://test.host/')
215
+ delete :destroy
216
+ assert_redirected_to 'http://test.host/'
217
+ end
218
+ end
219
+
@@ -0,0 +1,87 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Post
4
+ end
5
+
6
+ class Comment
7
+ def self.human_name; 'Comment'; end
8
+ end
9
+
10
+ class CommentsController < InheritedResources::Base
11
+ belongs_to :post
12
+ end
13
+
14
+ class BelongsToTest < ActionController::TestCase
15
+ tests CommentsController
16
+
17
+ def setup
18
+ Post.expects(:find).with('37').returns(mock_post)
19
+ mock_post.expects(:comments).returns(Comment)
20
+
21
+ @controller.stubs(:resource_url).returns('/')
22
+ @controller.stubs(:collection_url).returns('/')
23
+ end
24
+
25
+ def test_expose_all_comments_as_instance_variable_on_index
26
+ Comment.expects(:find).with(:all).returns([mock_comment])
27
+ get :index, :post_id => '37'
28
+ assert_equal mock_post, assigns(:post)
29
+ assert_equal [mock_comment], assigns(:comments)
30
+ end
31
+
32
+ def test_expose_the_resquested_comment_on_show
33
+ Comment.expects(:find).with('42').returns(mock_comment)
34
+ get :show, :id => '42', :post_id => '37'
35
+ assert_equal mock_post, assigns(:post)
36
+ assert_equal mock_comment, assigns(:comment)
37
+ end
38
+
39
+ def test_expose_a_new_comment_on_new
40
+ Comment.expects(:build).returns(mock_comment)
41
+ get :new, :post_id => '37'
42
+ assert_equal mock_post, assigns(:post)
43
+ assert_equal mock_comment, assigns(:comment)
44
+ end
45
+
46
+ def test_expose_the_resquested_comment_on_edit
47
+ Comment.expects(:find).with('42').returns(mock_comment)
48
+ get :edit, :id => '42', :post_id => '37'
49
+ assert_equal mock_post, assigns(:post)
50
+ assert_equal mock_comment, assigns(:comment)
51
+ end
52
+
53
+ def test_expose_a_newly_create_comment_on_create
54
+ Comment.expects(:build).with({'these' => 'params'}).returns(mock_comment(:save => true))
55
+ post :create, :post_id => '37', :comment => {:these => 'params'}
56
+ assert_equal mock_post, assigns(:post)
57
+ assert_equal mock_comment, assigns(:comment)
58
+ end
59
+
60
+ def test_update_the_requested_object_on_update
61
+ Comment.expects(:find).with('42').returns(mock_comment)
62
+ mock_comment.expects(:update_attributes).with({'these' => 'params'}).returns(true)
63
+ put :update, :id => '42', :post_id => '37', :comment => {:these => 'params'}
64
+ assert_equal mock_post, assigns(:post)
65
+ assert_equal mock_comment, assigns(:comment)
66
+ end
67
+
68
+ def test_the_resquested_comment_is_destroyed_on_destroy
69
+ Comment.expects(:find).with('42').returns(mock_comment)
70
+ mock_comment.expects(:destroy)
71
+ delete :destroy, :id => '42', :post_id => '37'
72
+ assert_equal mock_post, assigns(:post)
73
+ assert_equal mock_comment, assigns(:comment)
74
+ end
75
+
76
+ protected
77
+
78
+ def mock_post(stubs={})
79
+ @mock_post ||= mock(stubs)
80
+ end
81
+
82
+ def mock_comment(stubs={})
83
+ @mock_comment ||= mock(stubs)
84
+ end
85
+
86
+ end
87
+
@@ -0,0 +1,137 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Book; end
4
+ class Folder; end
5
+
6
+ class BooksController < InheritedResources::Base
7
+ actions :index, :show
8
+ end
9
+
10
+ class ReadersController < InheritedResources::Base
11
+ actions :all, :except => [ :edit, :update ]
12
+ end
13
+
14
+ class FoldersController < InheritedResources::Base
15
+ end
16
+
17
+ class Dean
18
+ def self.human_name; 'Dean'; end
19
+ end
20
+
21
+ class SchoolsController < InheritedResources::Base
22
+ has_scope :by_city
23
+ has_scope :featured, :boolean => true, :only => :index, :key => :by_featured
24
+ end
25
+
26
+ class DeansController < InheritedResources::Base
27
+ belongs_to :school
28
+ end
29
+
30
+
31
+ class ActionsClassMethodTest < ActiveSupport::TestCase
32
+ def test_actions_are_undefined
33
+ action_methods = BooksController.send(:action_methods)
34
+ assert_equal 2, action_methods.size
35
+
36
+ ['index', 'show'].each do |action|
37
+ assert action_methods.include? action
38
+ end
39
+ end
40
+
41
+ def test_actions_are_undefined_when_except_option_is_given
42
+ action_methods = ReadersController.send(:action_methods)
43
+ assert_equal 5, action_methods.size
44
+
45
+ ['index', 'new', 'show', 'create', 'destroy'].each do |action|
46
+ assert action_methods.include? action
47
+ end
48
+ end
49
+ end
50
+
51
+
52
+ class DefaultsClassMethodTest < ActiveSupport::TestCase
53
+ def test_resource_class_is_set_to_nil_when_resource_model_cannot_be_found
54
+ assert_nil ReadersController.send(:resource_class)
55
+ end
56
+
57
+ def test_defaults_are_set
58
+ assert Folder, FoldersController.send(:resource_class)
59
+ assert :folder, FoldersController.send(:resources_configuration)[:self][:instance_name]
60
+ assert :folders, FoldersController.send(:resources_configuration)[:self][:collection_name]
61
+ end
62
+
63
+ def test_defaults_can_be_overwriten
64
+ BooksController.send(:defaults, :resource_class => String, :instance_name => 'string', :collection_name => 'strings')
65
+
66
+ assert String, BooksController.send(:resource_class)
67
+ assert :string, BooksController.send(:resources_configuration)[:self][:instance_name]
68
+ assert :strings, BooksController.send(:resources_configuration)[:self][:collection_name]
69
+
70
+ BooksController.send(:defaults, :class_name => 'Fixnum', :instance_name => :fixnum, :collection_name => :fixnums)
71
+
72
+ assert String, BooksController.send(:resource_class)
73
+ assert :string, BooksController.send(:resources_configuration)[:self][:instance_name]
74
+ assert :strings, BooksController.send(:resources_configuration)[:self][:collection_name]
75
+ end
76
+
77
+ def test_defaults_raises_invalid_key
78
+ assert_raise ArgumentError do
79
+ BooksController.send(:defaults, :boom => String)
80
+ end
81
+ end
82
+
83
+ def test_url_helpers_are_recreated_when_defaults_change
84
+ BooksController.expects(:create_resources_url_helpers!).returns(true).once
85
+ BooksController.send(:defaults, :instance_name => 'string', :collection_name => 'strings')
86
+ end
87
+ end
88
+
89
+ class BelongsToErrorsTest < ActiveSupport::TestCase
90
+ def test_belongs_to_raise_errors_with_invalid_arguments
91
+ assert_raise ArgumentError do
92
+ DeansController.send(:belongs_to)
93
+ end
94
+
95
+ assert_raise ArgumentError do
96
+ DeansController.send(:belongs_to, :nice, :invalid_key => '')
97
+ end
98
+ end
99
+
100
+ def test_belongs_to_raises_an_error_when_multiple_associations_are_given_with_options
101
+ assert_raise ArgumentError do
102
+ DeansController.send(:belongs_to, :arguments, :with_options, :parent_class => Professor)
103
+ end
104
+ end
105
+
106
+ def test_url_helpers_are_recreated_just_once_when_belongs_to_is_called_with_block
107
+ DeansController.expects(:create_resources_url_helpers!).returns(true).once
108
+ DeansController.send(:belongs_to, :school) do
109
+ belongs_to :association
110
+ end
111
+ ensure
112
+ DeansController.send(:parents_symbols=, [:school])
113
+ end
114
+
115
+ def test_url_helpers_are_recreated_just_once_when_belongs_to_is_called_with_multiple_blocks
116
+ DeansController.expects(:create_resources_url_helpers!).returns(true).once
117
+ DeansController.send(:belongs_to, :school) do
118
+ belongs_to :association do
119
+ belongs_to :nested
120
+ end
121
+ end
122
+ ensure
123
+ DeansController.send(:parents_symbols=, [:school])
124
+ end
125
+ end
126
+
127
+ class HasScopeClassMethods < ActiveSupport::TestCase
128
+ def test_scope_configuration_is_stored_as_hashes
129
+ config = SchoolsController.send(:scopes_configuration)
130
+
131
+ assert config.key?(:by_city)
132
+ assert config.key?(:featured)
133
+
134
+ assert_equal config[:by_city], { :as => :by_city, :only => [], :except => [] }
135
+ assert_equal config[:featured], { :as => :by_featured, :only => [ :index ], :except => [], :boolean => true }
136
+ end
137
+ end