public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
Revert the removal of should_be_restful.

This reverts commits 10303b28f38c76c83bf99037eb9b2cb05ffec737,
f018d0844403ccdf4d89758966a2914b4d317c55 and
66eaccf98ce4948f876f87fc0dbb51691eb8d7a7, which collectively removed
should_be_restful.  We need to deprecate should_be_restful for a good 
while
before we actually remove it from the codebase.

This also reverts the gem version back to 2.0.0.  We'll see if github has 
a
problem with that.
tsaleh (author)
Sat Sep 20 19:08:45 -0700 2008
commit  9a43008886c316ad42c3bd1a21e2d7dc8aae4549
tree    c37361caad8e5f9925df3d82689d0dca860518d8
parent  66eaccf98ce4948f876f87fc0dbb51691eb8d7a7
...
90
91
92
 
 
 
 
 
 
 
 
 
 
 
 
 
93
94
95
...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
0
@@ -90,6 +90,19 @@ Macros to test the most common controller patterns...
0
     end
0
   end
0
 
0
+Test entire controllers in a few lines...
0
+
0
+ class PostsControllerTest < Test::Unit::TestCase
0
+ should_be_restful do |resource|
0
+ resource.parent = :user
0
+
0
+ resource.create.params = { :title => "first post", :body => 'blah blah blah'}
0
+ resource.update.params = { :title => "changed" }
0
+ end
0
+ end
0
+
0
+should_be_restful generates 40 tests on the fly, for both html and xml requests.
0
+
0
 === Helpful Assertions (ThoughtBot::Shoulda::Assertions)
0
 
0
 More to come here, but have fun with what's there.
...
22
23
24
 
 
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
27
28
...
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
64
65
66
67
68
69
70
71
72
73
74
0
@@ -22,7 +22,53 @@ module ThoughtBot # :nodoc:
0
       # end
0
       #
0
       # Would produce 5 tests for the +show+ action
0
+ #
0
+ # Furthermore, the should_be_restful helper will create an entire set of tests which will verify that your
0
+ # controller responds restfully to a variety of requested formats.
0
       module Macros
0
+ # :section: should_be_restful
0
+ # Generates a full suite of tests for a restful controller.
0
+ #
0
+ # The following definition will generate tests for the +index+, +show+, +new+,
0
+ # +edit+, +create+, +update+ and +destroy+ actions, in both +html+ and +xml+ formats:
0
+ #
0
+ # should_be_restful do |resource|
0
+ # resource.parent = :user
0
+ #
0
+ # resource.create.params = { :title => "first post", :body => 'blah blah blah'}
0
+ # resource.update.params = { :title => "changed" }
0
+ # end
0
+ #
0
+ # This generates about 40 tests, all of the format:
0
+ # "on GET to :show should assign @user."
0
+ # "on GET to :show should not set the flash."
0
+ # "on GET to :show should render 'show' template."
0
+ # "on GET to :show should respond with success."
0
+ # "on GET to :show as xml should assign @user."
0
+ # "on GET to :show as xml should have ContentType set to 'application/xml'."
0
+ # "on GET to :show as xml should respond with success."
0
+ # "on GET to :show as xml should return <user/> as the root element."
0
+ # The +resource+ parameter passed into the block is a ResourceOptions object, and
0
+ # is used to configure the tests for the details of your resources.
0
+ #
0
+ def should_be_restful(&blk) # :yields: resource
0
+ resource = ResourceOptions.new
0
+ blk.call(resource)
0
+ resource.normalize!(self)
0
+
0
+ resource.formats.each do |format|
0
+ resource.actions.each do |action|
0
+ if self.respond_to? :"make_#{action}_#{format}_tests"
0
+ self.send(:"make_#{action}_#{format}_tests", resource)
0
+ else
0
+ should "test #{action} #{format}" do
0
+ flunk "Test for #{action} as #{format} not implemented"
0
+ end
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
         # :section: Test macros
0
         # Macro that creates a test asserting that the flash contains the given value.
0
         # val can be a String, a Regex, or nil (indicating that the flash should not be set)
...
1
2
3
 
4
5
6
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
10
...
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
0
@@ -1,10 +1,234 @@
0
 module ThoughtBot # :nodoc:
0
   module Shoulda # :nodoc:
0
     module Controller
0
+ # Formats tested by #should_be_restful. Defaults to [:html, :xml]
0
       VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym) # :doc:
0
       VALID_FORMATS.each {|f| require "shoulda/controller/formats/#{f}"}
0
 
0
+ # Actions tested by #should_be_restful
0
       VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc:
0
+
0
+ # A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
0
+ #
0
+ # Example:
0
+ # class UsersControllerTest < Test::Unit::TestCase
0
+ # fixtures :all
0
+ #
0
+ # def setup
0
+ # ...normal setup code...
0
+ # @user = User.find(:first)
0
+ # end
0
+ #
0
+ # should_be_restful do |resource|
0
+ # resource.identifier = :id
0
+ # resource.klass = User
0
+ # resource.object = :user
0
+ # resource.parent = []
0
+ # resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
0
+ # resource.formats = [:html, :xml]
0
+ #
0
+ # resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
0
+ # resource.update.params = { :name => "sue" }
0
+ #
0
+ # resource.create.redirect = "user_url(@user)"
0
+ # resource.update.redirect = "user_url(@user)"
0
+ # resource.destroy.redirect = "users_url"
0
+ #
0
+ # resource.create.flash = /created/i
0
+ # resource.update.flash = /updated/i
0
+ # resource.destroy.flash = /removed/i
0
+ # end
0
+ # end
0
+ #
0
+ # Whenever possible, the resource attributes will be set to sensible defaults.
0
+ #
0
+ class ResourceOptions
0
+ # Configuration options for the create, update, destroy actions under should_be_restful
0
+ class ActionOptions
0
+ # String evaled to get the target of the redirection.
0
+ # All of the instance variables set by the controller will be available to the
0
+ # evaled code.
0
+ #
0
+ # Example:
0
+ # resource.create.redirect = "user_url(@user.company, @user)"
0
+ #
0
+ # Defaults to a generated url based on the name of the controller, the action, and the resource.parents list.
0
+ attr_accessor :redirect
0
+
0
+ # String or Regexp describing a value expected in the flash. Will match against any flash key.
0
+ #
0
+ # Defaults:
0
+ # destroy:: /removed/
0
+ # create:: /created/
0
+ # update:: /updated/
0
+ attr_accessor :flash
0
+
0
+ # Hash describing the params that should be sent in with this action.
0
+ attr_accessor :params
0
+ end
0
+
0
+ # Configuration options for the denied actions under should_be_restful
0
+ #
0
+ # Example:
0
+ # context "The public" do
0
+ # setup do
0
+ # @request.session[:logged_in] = false
0
+ # end
0
+ #
0
+ # should_be_restful do |resource|
0
+ # resource.parent = :user
0
+ #
0
+ # resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
0
+ # resource.denied.flash = /get outta here/i
0
+ # resource.denied.redirect = 'new_session_url'
0
+ # end
0
+ # end
0
+ #
0
+ class DeniedOptions
0
+ # String evaled to get the target of the redirection.
0
+ # All of the instance variables set by the controller will be available to the
0
+ # evaled code.
0
+ #
0
+ # Example:
0
+ # resource.create.redirect = "user_url(@user.company, @user)"
0
+ attr_accessor :redirect
0
+
0
+ # String or Regexp describing a value expected in the flash. Will match against any flash key.
0
+ #
0
+ # Example:
0
+ # resource.create.flash = /created/
0
+ attr_accessor :flash
0
+
0
+ # Actions that should be denied (only used by resource.denied). <i>Note that these actions will
0
+ # only be tested if they are also listed in +resource.actions+</i>
0
+ # The special value of :all will deny all of the REST actions.
0
+ attr_accessor :actions
0
+ end
0
+
0
+ # Name of key in params that references the primary key.
0
+ # Will almost always be :id (default), unless you are using a plugin or have patched rails.
0
+ attr_accessor :identifier
0
+
0
+ # Name of the ActiveRecord class this resource is responsible for. Automatically determined from
0
+ # test class if not explicitly set. UserTest => "User"
0
+ attr_accessor :klass
0
+
0
+ # Name of the instantiated ActiveRecord object that should be used by some of the tests.
0
+ # Defaults to the underscored name of the AR class. CompanyManager => :company_manager
0
+ attr_accessor :object
0
+
0
+ # Name of the parent AR objects. Can be set as parent= or parents=, and can take either
0
+ # the name of the parent resource (if there's only one), or an array of names (if there's
0
+ # more than one).
0
+ #
0
+ # Example:
0
+ # # in the routes...
0
+ # map.resources :companies do
0
+ # map.resources :people do
0
+ # map.resources :limbs
0
+ # end
0
+ # end
0
+ #
0
+ # # in the tests...
0
+ # class PeopleControllerTest < Test::Unit::TestCase
0
+ # should_be_restful do |resource|
0
+ # resource.parent = :companies
0
+ # end
0
+ # end
0
+ #
0
+ # class LimbsControllerTest < Test::Unit::TestCase
0
+ # should_be_restful do |resource|
0
+ # resource.parents = [:companies, :people]
0
+ # end
0
+ # end
0
+ attr_accessor :parent
0
+ alias parents parent
0
+ alias parents= parent=
0
+
0
+ # Actions that should be tested. Must be a subset of VALID_ACTIONS (default).
0
+ # Tests for each actionw will only be generated if the action is listed here.
0
+ # The special value of :all will test all of the REST actions.
0
+ #
0
+ # Example (for a read-only controller):
0
+ # resource.actions = [:show, :index]
0
+ attr_accessor :actions
0
+
0
+ # Formats that should be tested. Must be a subset of VALID_FORMATS (default).
0
+ # Each action will be tested against the formats listed here. The special value
0
+ # of :all will test all of the supported formats.
0
+ #
0
+ # Example:
0
+ # resource.actions = [:html, :xml]
0
+ attr_accessor :formats
0
+
0
+ # ActionOptions object specifying options for the create action.
0
+ attr_accessor :create
0
+
0
+ # ActionOptions object specifying options for the update action.
0
+ attr_accessor :update
0
+
0
+ # ActionOptions object specifying options for the desrtoy action.
0
+ attr_accessor :destroy
0
+
0
+ # DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
0
+ attr_accessor :denied
0
+
0
+ def initialize # :nodoc:
0
+ @create = ActionOptions.new
0
+ @update = ActionOptions.new
0
+ @destroy = ActionOptions.new
0
+ @denied = DeniedOptions.new
0
+
0
+ @create.flash ||= /created/i
0
+ @update.flash ||= /updated/i
0
+ @destroy.flash ||= /removed/i
0
+ @denied.flash ||= /denied/i
0
+
0
+ @create.params ||= {}
0
+ @update.params ||= {}
0
+
0
+ @actions = VALID_ACTIONS
0
+ @formats = VALID_FORMATS
0
+ @denied.actions = []
0
+ end
0
+
0
+ def normalize!(target) # :nodoc:
0
+ @denied.actions = VALID_ACTIONS if @denied.actions == :all
0
+ @actions = VALID_ACTIONS if @actions == :all
0
+ @formats = VALID_FORMATS if @formats == :all
0
+
0
+ @denied.actions = @denied.actions.map(&:to_sym)
0
+ @actions = @actions.map(&:to_sym)
0
+ @formats = @formats.map(&:to_sym)
0
+
0
+ ensure_valid_members(@actions, VALID_ACTIONS, 'actions')
0
+ ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions')
0
+ ensure_valid_members(@formats, VALID_FORMATS, 'formats')
0
+
0
+ @identifier ||= :id
0
+ @klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
0
+ @object ||= @klass.name.tableize.singularize
0
+ @parent ||= []
0
+ @parent = [@parent] unless @parent.is_a? Array
0
+
0
+ collection_helper = [@parent, @object.to_s.pluralize, 'url'].flatten.join('_')
0
+ collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ')
0
+ @destroy.redirect ||= "#{collection_helper}(#{collection_args})"
0
+
0
+ member_helper = [@parent, @object, 'url'].flatten.join('_')
0
+ member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ')
0
+ @create.redirect ||= "#{member_helper}(#{member_args})"
0
+ @update.redirect ||= "#{member_helper}(#{member_args})"
0
+ @denied.redirect ||= "new_session_url"
0
+ end
0
+
0
+ private
0
+
0
+ def ensure_valid_members(ary, valid_members, name) # :nodoc:
0
+ invalid = ary - valid_members
0
+ raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty?
0
+ end
0
+ end
0
     end
0
   end
0
 end
...
1
2
3
 
4
5
6
7
 
8
9
10
...
1
2
 
3
4
5
6
 
7
8
9
10
0
@@ -1,10 +1,10 @@
0
 Gem::Specification.new do |s|
0
   s.name = %q{shoulda}
0
- s.version = "2.0.1"
0
+ s.version = "2.0.0"
0
 
0
   s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
0
   s.authors = ["Tammer Saleh"]
0
- s.date = %q{2008-09-20}
0
+ s.date = %q{2008-09-14}
0
   s.default_executable = %q{convert_to_should_syntax}
0
   s.email = %q{tsaleh@thoughtbot.com}
0
   s.executables = ["convert_to_should_syntax"]
...
33
34
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
37
38
39
40
 
 
 
 
 
 
 
41
42
43
...
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
64
0
@@ -33,11 +33,32 @@ class PostsControllerTest < Test::Unit::TestCase
0
   should_route :get, '/users/5/posts/new', :action => :new, :user_id => 5
0
   should_route :put, '/users/5/posts/1', :action => :update, :id => 1, :user_id => 5
0
 
0
+ context "The public" do
0
+ setup do
0
+ @request.session[:logged_in] = false
0
+ end
0
+
0
+ should_be_restful do |resource|
0
+ resource.parent = :user
0
+
0
+ resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
0
+ resource.denied.flash = /what/i
0
+ resource.denied.redirect = '"/"'
0
+ end
0
+ end
0
+
0
   context "Logged in" do
0
     setup do
0
       @request.session[:logged_in] = true
0
     end
0
 
0
+ should_be_restful do |resource|
0
+ resource.parent = :user
0
+
0
+ resource.create.params = { :title => "first post", :body => 'blah blah blah'}
0
+ resource.update.params = { :title => "changed" }
0
+ end
0
+
0
     context "viewing posts for a user" do
0
       setup do
0
         get :index, :user_id => users(:first)
...
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
...
14
15
16
 
 
17
18
19
20
21
22
23
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26
27
28
29
30
31
 
 
 
 
32
33
34
35
 
36
0
@@ -14,30 +14,23 @@ class UsersControllerTest < Test::Unit::TestCase
0
     @user = User.find(:first)
0
   end
0
 
0
- context "on GET to #index" do
0
- setup { get :index }
0
+ should_be_restful do |resource|
0
+ resource.identifier = :id
0
+ resource.klass = User
0
+ resource.object = :user
0
+ resource.parent = []
0
+ resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
0
+ resource.formats = [:html, :xml]
0
 
0
- should_respond_with :success
0
- should_render_with_layout 'users'
0
- should_render_template :index
0
- should_assign_to :users
0
- end
0
-
0
- context "on GET to #index.xml" do
0
- setup { get :index, :format => 'xml' }
0
-
0
- should_respond_with :success
0
- should_respond_with_xml_for
0
- should_assign_to :users
0
- end
0
-
0
- context "on GET to #show" do
0
- setup { get :show, :id => @user }
0
+ resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13, :ssn => "123456789"}
0
+ resource.update.params = { :name => "sue" }
0
+
0
+ resource.create.redirect = "user_url(@user)"
0
+ resource.update.redirect = "user_url(@user)"
0
+ resource.destroy.redirect = "users_url"
0
 
0
- should_respond_with :success
0
- should_render_with_layout 'users'
0
- should_render_template :show
0
- should_assign_to :user
0
+ resource.create.flash = /created/i
0
+ resource.update.flash = /updated/i
0
+ resource.destroy.flash = /removed/i
0
   end
0
-
0
 end

Comments

    No one has commented yet.