giraffesoft-resource_controller 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README +1 -0
- data/README.rdoc +282 -0
- data/Rakefile +35 -0
- data/generators/scaffold_resource/USAGE +29 -0
- data/generators/scaffold_resource/scaffold_resource_generator.rb +179 -0
- data/generators/scaffold_resource/templates/controller.rb +2 -0
- data/generators/scaffold_resource/templates/fixtures.yml +10 -0
- data/generators/scaffold_resource/templates/functional_test.rb +57 -0
- data/generators/scaffold_resource/templates/helper.rb +2 -0
- data/generators/scaffold_resource/templates/migration.rb +15 -0
- data/generators/scaffold_resource/templates/model.rb +2 -0
- data/generators/scaffold_resource/templates/old_migration.rb +13 -0
- data/generators/scaffold_resource/templates/rspec/functional_spec.rb +255 -0
- data/generators/scaffold_resource/templates/rspec/helper_spec.rb +11 -0
- data/generators/scaffold_resource/templates/rspec/routing_spec.rb +61 -0
- data/generators/scaffold_resource/templates/rspec/unit_spec.rb +11 -0
- data/generators/scaffold_resource/templates/rspec/views/edit_spec.rb +28 -0
- data/generators/scaffold_resource/templates/rspec/views/index_spec.rb +26 -0
- data/generators/scaffold_resource/templates/rspec/views/new_spec.rb +30 -0
- data/generators/scaffold_resource/templates/rspec/views/show_spec.rb +25 -0
- data/generators/scaffold_resource/templates/shoulda_functional_test.rb +19 -0
- data/generators/scaffold_resource/templates/unit_test.rb +7 -0
- data/generators/scaffold_resource/templates/view__form.erb +6 -0
- data/generators/scaffold_resource/templates/view__form.haml +5 -0
- data/generators/scaffold_resource/templates/view_edit.erb +16 -0
- data/generators/scaffold_resource/templates/view_edit.haml +11 -0
- data/generators/scaffold_resource/templates/view_index.erb +22 -0
- data/generators/scaffold_resource/templates/view_index.haml +19 -0
- data/generators/scaffold_resource/templates/view_new.erb +12 -0
- data/generators/scaffold_resource/templates/view_new.haml +9 -0
- data/generators/scaffold_resource/templates/view_show.erb +9 -0
- data/generators/scaffold_resource/templates/view_show.haml +9 -0
- data/init.rb +1 -0
- data/lib/resource_controller/accessors.rb +77 -0
- data/lib/resource_controller/action_options.rb +40 -0
- data/lib/resource_controller/actions.rb +75 -0
- data/lib/resource_controller/base.rb +15 -0
- data/lib/resource_controller/class_methods.rb +22 -0
- data/lib/resource_controller/controller.rb +63 -0
- data/lib/resource_controller/failable_action_options.rb +25 -0
- data/lib/resource_controller/helpers/current_objects.rb +69 -0
- data/lib/resource_controller/helpers/internal.rb +76 -0
- data/lib/resource_controller/helpers/nested.rb +45 -0
- data/lib/resource_controller/helpers/urls.rb +124 -0
- data/lib/resource_controller/helpers.rb +28 -0
- data/lib/resource_controller/response_collector.rb +27 -0
- data/lib/resource_controller/version.rb +9 -0
- data/lib/resource_controller.rb +15 -0
- data/lib/tasks/gem.rake +67 -0
- data/lib/urligence.rb +50 -0
- data/rails/init.rb +6 -0
- data/test/Rakefile +10 -0
- data/test/app/controllers/application.rb +7 -0
- data/test/app/controllers/cms/options_controller.rb +3 -0
- data/test/app/controllers/cms/products_controller.rb +3 -0
- data/test/app/controllers/comments_controller.rb +3 -0
- data/test/app/controllers/people_controller.rb +9 -0
- data/test/app/controllers/photos_controller.rb +11 -0
- data/test/app/controllers/posts_controller.rb +10 -0
- data/test/app/controllers/projects_controller.rb +3 -0
- data/test/app/controllers/somethings_controller.rb +3 -0
- data/test/app/controllers/tags_controller.rb +13 -0
- data/test/app/controllers/users_controller.rb +12 -0
- data/test/app/helpers/application_helper.rb +3 -0
- data/test/app/helpers/cms/products_helper.rb +2 -0
- data/test/app/helpers/comments_helper.rb +2 -0
- data/test/app/helpers/options_helper.rb +2 -0
- data/test/app/helpers/people_helper.rb +2 -0
- data/test/app/helpers/photos_helper.rb +2 -0
- data/test/app/helpers/posts_helper.rb +2 -0
- data/test/app/helpers/projects_helper.rb +2 -0
- data/test/app/helpers/somethings_helper.rb +2 -0
- data/test/app/helpers/tags_helper.rb +2 -0
- data/test/app/helpers/users_helper.rb +2 -0
- data/test/app/models/account.rb +3 -0
- data/test/app/models/comment.rb +3 -0
- data/test/app/models/option.rb +3 -0
- data/test/app/models/photo.rb +4 -0
- data/test/app/models/post.rb +3 -0
- data/test/app/models/product.rb +3 -0
- data/test/app/models/project.rb +2 -0
- data/test/app/models/something.rb +2 -0
- data/test/app/models/tag.rb +3 -0
- data/test/app/views/cms/options/edit.rhtml +17 -0
- data/test/app/views/cms/options/index.rhtml +20 -0
- data/test/app/views/cms/options/new.rhtml +16 -0
- data/test/app/views/cms/options/show.rhtml +8 -0
- data/test/app/views/cms/products/edit.rhtml +17 -0
- data/test/app/views/cms/products/index.rhtml +20 -0
- data/test/app/views/cms/products/new.rhtml +16 -0
- data/test/app/views/cms/products/show.rhtml +8 -0
- data/test/app/views/comments/edit.rhtml +27 -0
- data/test/app/views/comments/index.rhtml +24 -0
- data/test/app/views/comments/new.rhtml +26 -0
- data/test/app/views/comments/show.rhtml +18 -0
- data/test/app/views/layouts/application.rhtml +17 -0
- data/test/app/views/layouts/comments.rhtml +17 -0
- data/test/app/views/layouts/options.rhtml +17 -0
- data/test/app/views/layouts/people.rhtml +17 -0
- data/test/app/views/layouts/photos.rhtml +17 -0
- data/test/app/views/layouts/projects.rhtml +17 -0
- data/test/app/views/layouts/somethings.rhtml +17 -0
- data/test/app/views/layouts/tags.rhtml +17 -0
- data/test/app/views/people/edit.rhtml +17 -0
- data/test/app/views/people/index.rhtml +20 -0
- data/test/app/views/people/new.rhtml +16 -0
- data/test/app/views/people/show.rhtml +8 -0
- data/test/app/views/photos/edit.rhtml +17 -0
- data/test/app/views/photos/index.rhtml +20 -0
- data/test/app/views/photos/new.rhtml +16 -0
- data/test/app/views/photos/show.rhtml +8 -0
- data/test/app/views/posts/edit.rhtml +22 -0
- data/test/app/views/posts/index.rhtml +22 -0
- data/test/app/views/posts/new.rhtml +21 -0
- data/test/app/views/posts/show.rhtml +13 -0
- data/test/app/views/projects/edit.rhtml +17 -0
- data/test/app/views/projects/index.rhtml +20 -0
- data/test/app/views/projects/new.rhtml +16 -0
- data/test/app/views/projects/show.rhtml +8 -0
- data/test/app/views/somethings/edit.rhtml +17 -0
- data/test/app/views/somethings/index.rhtml +20 -0
- data/test/app/views/somethings/new.rhtml +16 -0
- data/test/app/views/somethings/show.rhtml +8 -0
- data/test/app/views/tags/edit.rhtml +17 -0
- data/test/app/views/tags/index.rhtml +20 -0
- data/test/app/views/tags/index.rjs +0 -0
- data/test/app/views/tags/new.rhtml +16 -0
- data/test/app/views/tags/show.rhtml +8 -0
- data/test/app/views/users/edit.rhtml +17 -0
- data/test/app/views/users/index.rhtml +20 -0
- data/test/app/views/users/new.rhtml +16 -0
- data/test/app/views/users/show.rhtml +8 -0
- data/test/config/boot.rb +109 -0
- data/test/config/database.yml +16 -0
- data/test/config/environment.rb +64 -0
- data/test/config/environments/development.rb +21 -0
- data/test/config/environments/test.rb +19 -0
- data/test/config/routes.rb +51 -0
- data/test/db/migrate/001_create_posts.rb +12 -0
- data/test/db/migrate/002_create_products.rb +11 -0
- data/test/db/migrate/003_create_comments.rb +13 -0
- data/test/db/migrate/004_create_options.rb +12 -0
- data/test/db/migrate/005_create_photos.rb +11 -0
- data/test/db/migrate/006_create_tags.rb +17 -0
- data/test/db/migrate/007_create_somethings.rb +11 -0
- data/test/db/migrate/008_create_accounts.rb +11 -0
- data/test/db/migrate/009_add_account_id_to_photos.rb +9 -0
- data/test/db/migrate/010_create_projects.rb +11 -0
- data/test/db/schema.rb +65 -0
- data/test/script/console +3 -0
- data/test/script/destroy +3 -0
- data/test/script/generate +3 -0
- data/test/script/server +3 -0
- data/test/test/fixtures/accounts.yml +7 -0
- data/test/test/fixtures/comments.yml +11 -0
- data/test/test/fixtures/options.yml +9 -0
- data/test/test/fixtures/photos.yml +9 -0
- data/test/test/fixtures/photos_tags.yml +3 -0
- data/test/test/fixtures/posts.yml +9 -0
- data/test/test/fixtures/products.yml +7 -0
- data/test/test/fixtures/projects.yml +7 -0
- data/test/test/fixtures/somethings.yml +7 -0
- data/test/test/fixtures/tags.yml +7 -0
- data/test/test/functional/cms/options_controller_test.rb +23 -0
- data/test/test/functional/cms/products_controller_test.rb +23 -0
- data/test/test/functional/comments_controller_test.rb +26 -0
- data/test/test/functional/people_controller_test.rb +34 -0
- data/test/test/functional/photos_controller_test.rb +130 -0
- data/test/test/functional/posts_controller_test.rb +34 -0
- data/test/test/functional/projects_controller_test.rb +18 -0
- data/test/test/functional/somethings_controller_test.rb +28 -0
- data/test/test/functional/tags_controller_test.rb +64 -0
- data/test/test/functional/users_controller_test.rb +24 -0
- data/test/test/test_helper.rb +12 -0
- data/test/test/unit/accessors_test.rb +110 -0
- data/test/test/unit/account_test.rb +7 -0
- data/test/test/unit/action_options_test.rb +109 -0
- data/test/test/unit/base_test.rb +11 -0
- data/test/test/unit/comment_test.rb +10 -0
- data/test/test/unit/failable_action_options_test.rb +77 -0
- data/test/test/unit/helpers/current_objects_test.rb +127 -0
- data/test/test/unit/helpers/internal_test.rb +106 -0
- data/test/test/unit/helpers/nested_test.rb +82 -0
- data/test/test/unit/helpers/urls_test.rb +71 -0
- data/test/test/unit/helpers_test.rb +25 -0
- data/test/test/unit/option_test.rb +10 -0
- data/test/test/unit/photo_test.rb +10 -0
- data/test/test/unit/post_test.rb +10 -0
- data/test/test/unit/project_test.rb +10 -0
- data/test/test/unit/response_collector_test.rb +49 -0
- data/test/test/unit/something_test.rb +10 -0
- data/test/test/unit/tag_test.rb +10 -0
- data/test/test/unit/urligence_test.rb +203 -0
- data/test/vendor/plugins/shoulda/Rakefile +32 -0
- data/test/vendor/plugins/shoulda/bin/convert_to_should_syntax +40 -0
- data/test/vendor/plugins/shoulda/init.rb +3 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb +580 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/color.rb +77 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb +467 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb +201 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb +170 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/gem/proc_extensions.rb +14 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb +239 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/general.rb +118 -0
- data/test/vendor/plugins/shoulda/lib/shoulda/private_helpers.rb +22 -0
- data/test/vendor/plugins/shoulda/lib/shoulda.rb +43 -0
- metadata +312 -0
@@ -0,0 +1,467 @@
|
|
1
|
+
module ThoughtBot # :nodoc:
|
2
|
+
module Shoulda # :nodoc:
|
3
|
+
module Controller
|
4
|
+
def self.included(other) # :nodoc:
|
5
|
+
other.class_eval do
|
6
|
+
extend ThoughtBot::Shoulda::Controller::ClassMethods
|
7
|
+
include ThoughtBot::Shoulda::Controller::InstanceMethods
|
8
|
+
ThoughtBot::Shoulda::Controller::ClassMethods::VALID_FORMATS.each do |format|
|
9
|
+
include "ThoughtBot::Shoulda::Controller::#{format.to_s.upcase}".constantize
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# = Macro test helpers for your controllers
|
15
|
+
#
|
16
|
+
# By using the macro helpers you can quickly and easily create concise and easy to read test suites.
|
17
|
+
#
|
18
|
+
# This code segment:
|
19
|
+
# context "on GET to :show for first record" do
|
20
|
+
# setup do
|
21
|
+
# get :show, :id => 1
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# should_assign_to :user
|
25
|
+
# should_respond_with :success
|
26
|
+
# should_render_template :show
|
27
|
+
# should_not_set_the_flash
|
28
|
+
#
|
29
|
+
# should "do something else really cool" do
|
30
|
+
# assert_equal 1, assigns(:user).id
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# Would produce 5 tests for the +show+ action
|
35
|
+
#
|
36
|
+
# Furthermore, the should_be_restful helper will create an entire set of tests which will verify that your
|
37
|
+
# controller responds restfully to a variety of requested formats.
|
38
|
+
module ClassMethods
|
39
|
+
# Formats tested by #should_be_restful. Defaults to [:html, :xml]
|
40
|
+
VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym) # :doc:
|
41
|
+
VALID_FORMATS.each {|f| require "shoulda/controller_tests/formats/#{f}.rb"}
|
42
|
+
|
43
|
+
# Actions tested by #should_be_restful
|
44
|
+
VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc:
|
45
|
+
|
46
|
+
# A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
|
47
|
+
#
|
48
|
+
# Example:
|
49
|
+
# class UsersControllerTest < Test::Unit::TestCase
|
50
|
+
# load_all_fixtures
|
51
|
+
#
|
52
|
+
# def setup
|
53
|
+
# ...normal setup code...
|
54
|
+
# @user = User.find(:first)
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# should_be_restful do |resource|
|
58
|
+
# resource.identifier = :id
|
59
|
+
# resource.klass = User
|
60
|
+
# resource.object = :user
|
61
|
+
# resource.parent = []
|
62
|
+
# resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
|
63
|
+
# resource.formats = [:html, :xml]
|
64
|
+
#
|
65
|
+
# resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
|
66
|
+
# resource.update.params = { :name => "sue" }
|
67
|
+
#
|
68
|
+
# resource.create.redirect = "user_url(@user)"
|
69
|
+
# resource.update.redirect = "user_url(@user)"
|
70
|
+
# resource.destroy.redirect = "users_url"
|
71
|
+
#
|
72
|
+
# resource.create.flash = /created/i
|
73
|
+
# resource.update.flash = /updated/i
|
74
|
+
# resource.destroy.flash = /removed/i
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# Whenever possible, the resource attributes will be set to sensible defaults.
|
79
|
+
#
|
80
|
+
class ResourceOptions
|
81
|
+
# Configuration options for the create, update, destroy actions under should_be_restful
|
82
|
+
class ActionOptions
|
83
|
+
# String evaled to get the target of the redirection.
|
84
|
+
# All of the instance variables set by the controller will be available to the
|
85
|
+
# evaled code.
|
86
|
+
#
|
87
|
+
# Example:
|
88
|
+
# resource.create.redirect = "user_url(@user.company, @user)"
|
89
|
+
#
|
90
|
+
# Defaults to a generated url based on the name of the controller, the action, and the resource.parents list.
|
91
|
+
attr_accessor :redirect
|
92
|
+
|
93
|
+
# String or Regexp describing a value expected in the flash. Will match against any flash key.
|
94
|
+
#
|
95
|
+
# Defaults:
|
96
|
+
# destroy:: /removed/
|
97
|
+
# create:: /created/
|
98
|
+
# update:: /updated/
|
99
|
+
attr_accessor :flash
|
100
|
+
|
101
|
+
# Hash describing the params that should be sent in with this action.
|
102
|
+
attr_accessor :params
|
103
|
+
end
|
104
|
+
|
105
|
+
# Configuration options for the denied actions under should_be_restful
|
106
|
+
#
|
107
|
+
# Example:
|
108
|
+
# context "The public" do
|
109
|
+
# setup do
|
110
|
+
# @request.session[:logged_in] = false
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# should_be_restful do |resource|
|
114
|
+
# resource.parent = :user
|
115
|
+
#
|
116
|
+
# resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
|
117
|
+
# resource.denied.flash = /get outta here/i
|
118
|
+
# resource.denied.redirect = 'new_session_url'
|
119
|
+
# end
|
120
|
+
# end
|
121
|
+
#
|
122
|
+
class DeniedOptions
|
123
|
+
# String evaled to get the target of the redirection.
|
124
|
+
# All of the instance variables set by the controller will be available to the
|
125
|
+
# evaled code.
|
126
|
+
#
|
127
|
+
# Example:
|
128
|
+
# resource.create.redirect = "user_url(@user.company, @user)"
|
129
|
+
attr_accessor :redirect
|
130
|
+
|
131
|
+
# String or Regexp describing a value expected in the flash. Will match against any flash key.
|
132
|
+
#
|
133
|
+
# Example:
|
134
|
+
# resource.create.flash = /created/
|
135
|
+
attr_accessor :flash
|
136
|
+
|
137
|
+
# Actions that should be denied (only used by resource.denied). <i>Note that these actions will
|
138
|
+
# only be tested if they are also listed in +resource.actions+</i>
|
139
|
+
# The special value of :all will deny all of the REST actions.
|
140
|
+
attr_accessor :actions
|
141
|
+
end
|
142
|
+
|
143
|
+
# Name of key in params that references the primary key.
|
144
|
+
# Will almost always be :id (default), unless you are using a plugin or have patched rails.
|
145
|
+
attr_accessor :identifier
|
146
|
+
|
147
|
+
# Name of the ActiveRecord class this resource is responsible for. Automatically determined from
|
148
|
+
# test class if not explicitly set. UserTest => "User"
|
149
|
+
attr_accessor :klass
|
150
|
+
|
151
|
+
# Name of the instantiated ActiveRecord object that should be used by some of the tests.
|
152
|
+
# Defaults to the underscored name of the AR class. CompanyManager => :company_manager
|
153
|
+
attr_accessor :object
|
154
|
+
|
155
|
+
# Name of the parent AR objects. Can be set as parent= or parents=, and can take either
|
156
|
+
# the name of the parent resource (if there's only one), or an array of names (if there's
|
157
|
+
# more than one).
|
158
|
+
#
|
159
|
+
# Example:
|
160
|
+
# # in the routes...
|
161
|
+
# map.resources :companies do
|
162
|
+
# map.resources :people do
|
163
|
+
# map.resources :limbs
|
164
|
+
# end
|
165
|
+
# end
|
166
|
+
#
|
167
|
+
# # in the tests...
|
168
|
+
# class PeopleControllerTest < Test::Unit::TestCase
|
169
|
+
# should_be_restful do |resource|
|
170
|
+
# resource.parent = :companies
|
171
|
+
# end
|
172
|
+
# end
|
173
|
+
#
|
174
|
+
# class LimbsControllerTest < Test::Unit::TestCase
|
175
|
+
# should_be_restful do |resource|
|
176
|
+
# resource.parents = [:companies, :people]
|
177
|
+
# end
|
178
|
+
# end
|
179
|
+
attr_accessor :parent
|
180
|
+
alias parents parent
|
181
|
+
alias parents= parent=
|
182
|
+
|
183
|
+
# Actions that should be tested. Must be a subset of VALID_ACTIONS (default).
|
184
|
+
# Tests for each actionw will only be generated if the action is listed here.
|
185
|
+
# The special value of :all will test all of the REST actions.
|
186
|
+
#
|
187
|
+
# Example (for a read-only controller):
|
188
|
+
# resource.actions = [:show, :index]
|
189
|
+
attr_accessor :actions
|
190
|
+
|
191
|
+
# Formats that should be tested. Must be a subset of VALID_FORMATS (default).
|
192
|
+
# Each action will be tested against the formats listed here. The special value
|
193
|
+
# of :all will test all of the supported formats.
|
194
|
+
#
|
195
|
+
# Example:
|
196
|
+
# resource.actions = [:html, :xml]
|
197
|
+
attr_accessor :formats
|
198
|
+
|
199
|
+
# ActionOptions object specifying options for the create action.
|
200
|
+
attr_accessor :create
|
201
|
+
|
202
|
+
# ActionOptions object specifying options for the update action.
|
203
|
+
attr_accessor :update
|
204
|
+
|
205
|
+
# ActionOptions object specifying options for the desrtoy action.
|
206
|
+
attr_accessor :destroy
|
207
|
+
|
208
|
+
# DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
|
209
|
+
attr_accessor :denied
|
210
|
+
|
211
|
+
def initialize # :nodoc:
|
212
|
+
@create = ActionOptions.new
|
213
|
+
@update = ActionOptions.new
|
214
|
+
@destroy = ActionOptions.new
|
215
|
+
@denied = DeniedOptions.new
|
216
|
+
|
217
|
+
@create.flash ||= /created/i
|
218
|
+
@update.flash ||= /updated/i
|
219
|
+
@destroy.flash ||= /removed/i
|
220
|
+
@denied.flash ||= /denied/i
|
221
|
+
|
222
|
+
@create.params ||= {}
|
223
|
+
@update.params ||= {}
|
224
|
+
|
225
|
+
@actions = VALID_ACTIONS
|
226
|
+
@formats = VALID_FORMATS
|
227
|
+
@denied.actions = []
|
228
|
+
end
|
229
|
+
|
230
|
+
def normalize!(target) # :nodoc:
|
231
|
+
@denied.actions = VALID_ACTIONS if @denied.actions == :all
|
232
|
+
@actions = VALID_ACTIONS if @actions == :all
|
233
|
+
@formats = VALID_FORMATS if @formats == :all
|
234
|
+
|
235
|
+
@denied.actions = @denied.actions.map(&:to_sym)
|
236
|
+
@actions = @actions.map(&:to_sym)
|
237
|
+
@formats = @formats.map(&:to_sym)
|
238
|
+
|
239
|
+
ensure_valid_members(@actions, VALID_ACTIONS, 'actions')
|
240
|
+
ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions')
|
241
|
+
ensure_valid_members(@formats, VALID_FORMATS, 'formats')
|
242
|
+
|
243
|
+
@identifier ||= :id
|
244
|
+
@klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
|
245
|
+
@object ||= @klass.name.tableize.singularize
|
246
|
+
@parent ||= []
|
247
|
+
@parent = [@parent] unless @parent.is_a? Array
|
248
|
+
|
249
|
+
collection_helper = [@parent, @object.to_s.pluralize, 'url'].flatten.join('_')
|
250
|
+
collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ')
|
251
|
+
@destroy.redirect ||= "#{collection_helper}(#{collection_args})"
|
252
|
+
|
253
|
+
member_helper = [@parent, @object, 'url'].flatten.join('_')
|
254
|
+
member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ')
|
255
|
+
@create.redirect ||= "#{member_helper}(#{member_args})"
|
256
|
+
@update.redirect ||= "#{member_helper}(#{member_args})"
|
257
|
+
@denied.redirect ||= "new_session_url"
|
258
|
+
end
|
259
|
+
|
260
|
+
private
|
261
|
+
|
262
|
+
def ensure_valid_members(ary, valid_members, name) # :nodoc:
|
263
|
+
invalid = ary - valid_members
|
264
|
+
raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty?
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
# :section: should_be_restful
|
269
|
+
# Generates a full suite of tests for a restful controller.
|
270
|
+
#
|
271
|
+
# The following definition will generate tests for the +index+, +show+, +new+,
|
272
|
+
# +edit+, +create+, +update+ and +destroy+ actions, in both +html+ and +xml+ formats:
|
273
|
+
#
|
274
|
+
# should_be_restful do |resource|
|
275
|
+
# resource.parent = :user
|
276
|
+
#
|
277
|
+
# resource.create.params = { :title => "first post", :body => 'blah blah blah'}
|
278
|
+
# resource.update.params = { :title => "changed" }
|
279
|
+
# end
|
280
|
+
#
|
281
|
+
# This generates about 40 tests, all of the format:
|
282
|
+
# "on GET to :show should assign @user."
|
283
|
+
# "on GET to :show should not set the flash."
|
284
|
+
# "on GET to :show should render 'show' template."
|
285
|
+
# "on GET to :show should respond with success."
|
286
|
+
# "on GET to :show as xml should assign @user."
|
287
|
+
# "on GET to :show as xml should have ContentType set to 'application/xml'."
|
288
|
+
# "on GET to :show as xml should respond with success."
|
289
|
+
# "on GET to :show as xml should return <user/> as the root element."
|
290
|
+
# The +resource+ parameter passed into the block is a ResourceOptions object, and
|
291
|
+
# is used to configure the tests for the details of your resources.
|
292
|
+
#
|
293
|
+
def should_be_restful(&blk) # :yields: resource
|
294
|
+
resource = ResourceOptions.new
|
295
|
+
blk.call(resource)
|
296
|
+
resource.normalize!(self)
|
297
|
+
|
298
|
+
resource.formats.each do |format|
|
299
|
+
resource.actions.each do |action|
|
300
|
+
if self.respond_to? :"make_#{action}_#{format}_tests"
|
301
|
+
self.send(:"make_#{action}_#{format}_tests", resource)
|
302
|
+
else
|
303
|
+
should "test #{action} #{format}" do
|
304
|
+
flunk "Test for #{action} as #{format} not implemented"
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
# :section: Test macros
|
312
|
+
|
313
|
+
# Macro that creates a test asserting that the flash contains the given value.
|
314
|
+
# val can be a String, a Regex, or nil (indicating that the flash should not be set)
|
315
|
+
#
|
316
|
+
# Example:
|
317
|
+
#
|
318
|
+
# should_set_the_flash_to "Thank you for placing this order."
|
319
|
+
# should_set_the_flash_to /created/i
|
320
|
+
# should_set_the_flash_to nil
|
321
|
+
def should_set_the_flash_to(val)
|
322
|
+
if val
|
323
|
+
should "have #{val.inspect} in the flash" do
|
324
|
+
assert_contains flash.values, val, ", Flash: #{flash.inspect}"
|
325
|
+
end
|
326
|
+
else
|
327
|
+
should "not set the flash" do
|
328
|
+
assert_equal({}, flash, "Flash was set to:\n#{flash.inspect}")
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
# Macro that creates a test asserting that the flash is empty. Same as
|
334
|
+
# @should_set_the_flash_to nil@
|
335
|
+
def should_not_set_the_flash
|
336
|
+
should_set_the_flash_to nil
|
337
|
+
end
|
338
|
+
|
339
|
+
# Macro that creates a test asserting that the controller assigned to @name
|
340
|
+
#
|
341
|
+
# Example:
|
342
|
+
#
|
343
|
+
# should_assign_to :user
|
344
|
+
def should_assign_to(name)
|
345
|
+
should "assign @#{name}" do
|
346
|
+
assert assigns(name.to_sym), "The action isn't assigning to @#{name}"
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
# Macro that creates a test asserting that the controller did not assign to @name
|
351
|
+
#
|
352
|
+
# Example:
|
353
|
+
#
|
354
|
+
# should_not_assign_to :user
|
355
|
+
def should_not_assign_to(name)
|
356
|
+
should "not assign to @#{name}" do
|
357
|
+
assert !assigns(name.to_sym), "@#{name} was visible"
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
# Macro that creates a test asserting that the controller responded with a 'response' status code.
|
362
|
+
# Example:
|
363
|
+
#
|
364
|
+
# should_respond_with :success
|
365
|
+
def should_respond_with(response)
|
366
|
+
should "respond with #{response}" do
|
367
|
+
assert_response response
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
# Macro that creates a test asserting that the controller rendered the given template.
|
372
|
+
# Example:
|
373
|
+
#
|
374
|
+
# should_render_template :new
|
375
|
+
def should_render_template(template)
|
376
|
+
should "render template #{template.inspect}" do
|
377
|
+
assert_template template.to_s
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
# Macro that creates a test asserting that the controller returned a redirect to the given path.
|
382
|
+
# The given string is evaled to produce the resulting redirect path. All of the instance variables
|
383
|
+
# set by the controller are available to the evaled string.
|
384
|
+
# Example:
|
385
|
+
#
|
386
|
+
# should_redirect_to '"/"'
|
387
|
+
# should_redirect_to "users_url(@user)"
|
388
|
+
def should_redirect_to(url)
|
389
|
+
should "redirect to #{url.inspect}" do
|
390
|
+
instantiate_variables_from_assigns do
|
391
|
+
assert_redirected_to eval(url, self.send(:binding), __FILE__, __LINE__)
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
# Macro that creates a test asserting that the rendered view contains a <form> element.
|
397
|
+
def should_render_a_form
|
398
|
+
should "display a form" do
|
399
|
+
assert_select "form", true, "The template doesn't contain a <form> element"
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
module InstanceMethods # :nodoc:
|
405
|
+
|
406
|
+
private # :enddoc:
|
407
|
+
|
408
|
+
SPECIAL_INSTANCE_VARIABLES = %w{
|
409
|
+
_cookies
|
410
|
+
_flash
|
411
|
+
_headers
|
412
|
+
_params
|
413
|
+
_request
|
414
|
+
_response
|
415
|
+
_session
|
416
|
+
action_name
|
417
|
+
before_filter_chain_aborted
|
418
|
+
cookies
|
419
|
+
flash
|
420
|
+
headers
|
421
|
+
ignore_missing_templates
|
422
|
+
logger
|
423
|
+
params
|
424
|
+
request
|
425
|
+
request_origin
|
426
|
+
response
|
427
|
+
session
|
428
|
+
template
|
429
|
+
template_class
|
430
|
+
template_root
|
431
|
+
url
|
432
|
+
variables_added
|
433
|
+
}.map(&:to_s)
|
434
|
+
|
435
|
+
def instantiate_variables_from_assigns(*names, &blk)
|
436
|
+
old = {}
|
437
|
+
names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty?
|
438
|
+
names.each do |name|
|
439
|
+
old[name] = instance_variable_get("@#{name}")
|
440
|
+
instance_variable_set("@#{name}", assigns(name.to_sym))
|
441
|
+
end
|
442
|
+
blk.call
|
443
|
+
names.each do |name|
|
444
|
+
instance_variable_set("@#{name}", old[name])
|
445
|
+
end
|
446
|
+
end
|
447
|
+
|
448
|
+
def get_existing_record(res) # :nodoc:
|
449
|
+
returning(instance_variable_get("@#{res.object}")) do |record|
|
450
|
+
assert(record, "This test requires you to set @#{res.object} in your setup block")
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
454
|
+
def make_parent_params(resource, record = nil, parent_names = nil) # :nodoc:
|
455
|
+
parent_names ||= resource.parents.reverse
|
456
|
+
return {} if parent_names == [] # Base case
|
457
|
+
parent_name = parent_names.shift
|
458
|
+
parent = record ? record.send(parent_name) : parent_name.to_s.classify.constantize.find(:first)
|
459
|
+
|
460
|
+
{ :"#{parent_name}_id" => parent.to_param }.merge(make_parent_params(resource, parent, parent_names))
|
461
|
+
end
|
462
|
+
|
463
|
+
end
|
464
|
+
end
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
@@ -0,0 +1,201 @@
|
|
1
|
+
module ThoughtBot # :nodoc:
|
2
|
+
module Shoulda # :nodoc:
|
3
|
+
module Controller # :nodoc:
|
4
|
+
module HTML # :nodoc: all
|
5
|
+
def self.included(other)
|
6
|
+
other.class_eval do
|
7
|
+
extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def controller_name_from_class
|
13
|
+
self.name.gsub(/Test/, '')
|
14
|
+
end
|
15
|
+
|
16
|
+
def make_show_html_tests(res)
|
17
|
+
context "on GET to #{controller_name_from_class}#show" do
|
18
|
+
setup do
|
19
|
+
record = get_existing_record(res)
|
20
|
+
parent_params = make_parent_params(res, record)
|
21
|
+
get :show, parent_params.merge({ res.identifier => record.to_param })
|
22
|
+
end
|
23
|
+
|
24
|
+
if res.denied.actions.include?(:show)
|
25
|
+
should_not_assign_to res.object
|
26
|
+
should_redirect_to res.denied.redirect
|
27
|
+
should_set_the_flash_to res.denied.flash
|
28
|
+
else
|
29
|
+
should_assign_to res.object
|
30
|
+
should_respond_with :success
|
31
|
+
should_render_template :show
|
32
|
+
should_not_set_the_flash
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def make_edit_html_tests(res)
|
38
|
+
context "on GET to #{controller_name_from_class}#edit" do
|
39
|
+
setup do
|
40
|
+
@record = get_existing_record(res)
|
41
|
+
parent_params = make_parent_params(res, @record)
|
42
|
+
get :edit, parent_params.merge({ res.identifier => @record.to_param })
|
43
|
+
end
|
44
|
+
|
45
|
+
if res.denied.actions.include?(:edit)
|
46
|
+
should_not_assign_to res.object
|
47
|
+
should_redirect_to res.denied.redirect
|
48
|
+
should_set_the_flash_to res.denied.flash
|
49
|
+
else
|
50
|
+
should_assign_to res.object
|
51
|
+
should_respond_with :success
|
52
|
+
should_render_template :edit
|
53
|
+
should_not_set_the_flash
|
54
|
+
should_render_a_form
|
55
|
+
should "set @#{res.object} to requested instance" do
|
56
|
+
assert_equal @record, assigns(res.object)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def make_index_html_tests(res)
|
63
|
+
context "on GET to #{controller_name_from_class}#index" do
|
64
|
+
setup do
|
65
|
+
record = get_existing_record(res) rescue nil
|
66
|
+
parent_params = make_parent_params(res, record)
|
67
|
+
get(:index, parent_params)
|
68
|
+
end
|
69
|
+
|
70
|
+
if res.denied.actions.include?(:index)
|
71
|
+
should_not_assign_to res.object.to_s.pluralize
|
72
|
+
should_redirect_to res.denied.redirect
|
73
|
+
should_set_the_flash_to res.denied.flash
|
74
|
+
else
|
75
|
+
should_respond_with :success
|
76
|
+
should_assign_to res.object.to_s.pluralize
|
77
|
+
should_render_template :index
|
78
|
+
should_not_set_the_flash
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def make_new_html_tests(res)
|
84
|
+
context "on GET to #{controller_name_from_class}#new" do
|
85
|
+
setup do
|
86
|
+
record = get_existing_record(res) rescue nil
|
87
|
+
parent_params = make_parent_params(res, record)
|
88
|
+
get(:new, parent_params)
|
89
|
+
end
|
90
|
+
|
91
|
+
if res.denied.actions.include?(:new)
|
92
|
+
should_not_assign_to res.object
|
93
|
+
should_redirect_to res.denied.redirect
|
94
|
+
should_set_the_flash_to res.denied.flash
|
95
|
+
else
|
96
|
+
should_respond_with :success
|
97
|
+
should_assign_to res.object
|
98
|
+
should_not_set_the_flash
|
99
|
+
should_render_template :new
|
100
|
+
should_render_a_form
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def make_destroy_html_tests(res)
|
106
|
+
context "on DELETE to #{controller_name_from_class}#destroy" do
|
107
|
+
setup do
|
108
|
+
@record = get_existing_record(res)
|
109
|
+
parent_params = make_parent_params(res, @record)
|
110
|
+
delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
|
111
|
+
end
|
112
|
+
|
113
|
+
if res.denied.actions.include?(:destroy)
|
114
|
+
should_redirect_to res.denied.redirect
|
115
|
+
should_set_the_flash_to res.denied.flash
|
116
|
+
|
117
|
+
should "not destroy record" do
|
118
|
+
assert_nothing_raised { assert @record.reload }
|
119
|
+
end
|
120
|
+
else
|
121
|
+
should_set_the_flash_to res.destroy.flash
|
122
|
+
if res.destroy.redirect.is_a? Symbol
|
123
|
+
should_respond_with res.destroy.redirect
|
124
|
+
else
|
125
|
+
should_redirect_to res.destroy.redirect
|
126
|
+
end
|
127
|
+
|
128
|
+
should "destroy record" do
|
129
|
+
assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
|
130
|
+
@record.reload
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def make_create_html_tests(res)
|
138
|
+
context "on POST to #{controller_name_from_class}#create with #{res.create.params.inspect}" do
|
139
|
+
setup do
|
140
|
+
record = get_existing_record(res) rescue nil
|
141
|
+
parent_params = make_parent_params(res, record)
|
142
|
+
@count = res.klass.count
|
143
|
+
post :create, parent_params.merge(res.object => res.create.params)
|
144
|
+
end
|
145
|
+
|
146
|
+
if res.denied.actions.include?(:create)
|
147
|
+
should_redirect_to res.denied.redirect
|
148
|
+
should_set_the_flash_to res.denied.flash
|
149
|
+
should_not_assign_to res.object
|
150
|
+
|
151
|
+
should "not create new record" do
|
152
|
+
assert_equal @count, res.klass.count
|
153
|
+
end
|
154
|
+
else
|
155
|
+
should_assign_to res.object
|
156
|
+
should_set_the_flash_to res.create.flash
|
157
|
+
if res.create.redirect.is_a? Symbol
|
158
|
+
should_respond_with res.create.redirect
|
159
|
+
else
|
160
|
+
should_redirect_to res.create.redirect
|
161
|
+
end
|
162
|
+
|
163
|
+
should "not have errors on @#{res.object}" do
|
164
|
+
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def make_update_html_tests(res)
|
171
|
+
context "on PUT to #{controller_name_from_class}#update with #{res.create.params.inspect}" do
|
172
|
+
setup do
|
173
|
+
@record = get_existing_record(res)
|
174
|
+
parent_params = make_parent_params(res, @record)
|
175
|
+
put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
|
176
|
+
end
|
177
|
+
|
178
|
+
if res.denied.actions.include?(:update)
|
179
|
+
should_not_assign_to res.object
|
180
|
+
should_redirect_to res.denied.redirect
|
181
|
+
should_set_the_flash_to res.denied.flash
|
182
|
+
else
|
183
|
+
should_assign_to res.object
|
184
|
+
should_set_the_flash_to(res.update.flash)
|
185
|
+
if res.update.redirect.is_a? Symbol
|
186
|
+
should_respond_with res.update.redirect
|
187
|
+
else
|
188
|
+
should_redirect_to res.update.redirect
|
189
|
+
end
|
190
|
+
|
191
|
+
should "not have errors on @#{res.object}" do
|
192
|
+
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|