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