shoulda 2.9.1 → 2.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +27 -5
- data/Rakefile +2 -2
- data/lib/shoulda.rb +1 -1
- data/lib/shoulda/{controller.rb → action_controller.rb} +6 -8
- data/lib/shoulda/{controller → action_controller}/helpers.rb +1 -16
- data/lib/shoulda/action_controller/macros.rb +277 -0
- data/lib/shoulda/action_controller/matchers.rb +37 -0
- data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
- data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
- data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
- data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +70 -0
- data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
- data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
- data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +83 -0
- data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
- data/lib/shoulda/action_view.rb +10 -0
- data/lib/shoulda/action_view/macros.rb +56 -0
- data/lib/shoulda/active_record/macros.rb +8 -13
- data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +1 -1
- data/lib/shoulda/rails.rb +4 -3
- data/lib/shoulda/rspec.rb +7 -5
- data/test/functional/posts_controller_test.rb +28 -22
- data/test/functional/users_controller_test.rb +0 -19
- data/test/matchers/{allow_mass_assignment_of_matcher_test.rb → active_record/allow_mass_assignment_of_matcher_test.rb} +1 -1
- data/test/matchers/{allow_value_matcher_test.rb → active_record/allow_value_matcher_test.rb} +1 -1
- data/test/matchers/{association_matcher_test.rb → active_record/association_matcher_test.rb} +1 -1
- data/test/matchers/{ensure_inclusion_of_matcher_test.rb → active_record/ensure_inclusion_of_matcher_test.rb} +1 -1
- data/test/matchers/{ensure_length_of_matcher_test.rb → active_record/ensure_length_of_matcher_test.rb} +1 -1
- data/test/matchers/{have_db_column_matcher_test.rb → active_record/have_db_column_matcher_test.rb} +1 -1
- data/test/matchers/{have_index_matcher_test.rb → active_record/have_index_matcher_test.rb} +1 -1
- data/test/matchers/{have_named_scope_matcher_test.rb → active_record/have_named_scope_matcher_test.rb} +1 -1
- data/test/matchers/{have_readonly_attributes_matcher_test.rb → active_record/have_readonly_attributes_matcher_test.rb} +1 -1
- data/test/matchers/{validate_acceptance_of_matcher_test.rb → active_record/validate_acceptance_of_matcher_test.rb} +1 -1
- data/test/matchers/{validate_numericality_of_matcher_test.rb → active_record/validate_numericality_of_matcher_test.rb} +1 -1
- data/test/matchers/{validate_presence_of_matcher_test.rb → active_record/validate_presence_of_matcher_test.rb} +1 -1
- data/test/matchers/{validate_uniqueness_of_matcher_test.rb → active_record/validate_uniqueness_of_matcher_test.rb} +8 -2
- data/test/matchers/controller/assign_to_matcher_test.rb +35 -0
- data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
- data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
- data/test/matchers/controller/respond_with_content_type_matcher_test.rb +27 -0
- data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
- data/test/matchers/controller/route_matcher_test.rb +58 -0
- data/test/matchers/controller/set_session_matcher_test.rb +27 -0
- data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
- data/test/model_builder.rb +47 -2
- data/test/rails_root/app/models/user.rb +2 -1
- data/test/rails_root/config/environment.rb +1 -1
- data/test/rspec_test.rb +207 -0
- data/test/unit/user_test.rb +10 -1
- metadata +38 -22
- data/lib/shoulda/controller/formats/html.rb +0 -199
- data/lib/shoulda/controller/formats/xml.rb +0 -168
- data/lib/shoulda/controller/macros.rb +0 -336
- data/lib/shoulda/controller/resource_options.rb +0 -233
@@ -1,233 +0,0 @@
|
|
1
|
-
module Shoulda # :nodoc:
|
2
|
-
module Controller
|
3
|
-
# Formats tested by #should_be_restful. Defaults to [:html, :xml]
|
4
|
-
VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym) # :doc:
|
5
|
-
VALID_FORMATS.each {|f| require "shoulda/controller/formats/#{f}"}
|
6
|
-
|
7
|
-
# Actions tested by #should_be_restful
|
8
|
-
VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc:
|
9
|
-
|
10
|
-
# A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
|
11
|
-
#
|
12
|
-
# Example:
|
13
|
-
# class UsersControllerTest < Test::Unit::TestCase
|
14
|
-
# fixtures :all
|
15
|
-
#
|
16
|
-
# def setup
|
17
|
-
# ...normal setup code...
|
18
|
-
# @user = User.find(:first)
|
19
|
-
# end
|
20
|
-
#
|
21
|
-
# should_be_restful do |resource|
|
22
|
-
# resource.identifier = :id
|
23
|
-
# resource.klass = User
|
24
|
-
# resource.object = :user
|
25
|
-
# resource.parent = []
|
26
|
-
# resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
|
27
|
-
# resource.formats = [:html, :xml]
|
28
|
-
#
|
29
|
-
# resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
|
30
|
-
# resource.update.params = { :name => "sue" }
|
31
|
-
#
|
32
|
-
# resource.create.redirect = "user_url(@user)"
|
33
|
-
# resource.update.redirect = "user_url(@user)"
|
34
|
-
# resource.destroy.redirect = "users_url"
|
35
|
-
#
|
36
|
-
# resource.create.flash = /created/i
|
37
|
-
# resource.update.flash = /updated/i
|
38
|
-
# resource.destroy.flash = /removed/i
|
39
|
-
# end
|
40
|
-
# end
|
41
|
-
#
|
42
|
-
# Whenever possible, the resource attributes will be set to sensible defaults.
|
43
|
-
#
|
44
|
-
class ResourceOptions
|
45
|
-
# Configuration options for the create, update, destroy actions under should_be_restful
|
46
|
-
class ActionOptions
|
47
|
-
# String evaled to get the target of the redirection.
|
48
|
-
# All of the instance variables set by the controller will be available to the
|
49
|
-
# evaled code.
|
50
|
-
#
|
51
|
-
# Example:
|
52
|
-
# resource.create.redirect = "user_url(@user.company, @user)"
|
53
|
-
#
|
54
|
-
# Defaults to a generated url based on the name of the controller, the action, and the resource.parents list.
|
55
|
-
attr_accessor :redirect
|
56
|
-
|
57
|
-
# String or Regexp describing a value expected in the flash. Will match against any flash key.
|
58
|
-
#
|
59
|
-
# Defaults:
|
60
|
-
# destroy:: /removed/
|
61
|
-
# create:: /created/
|
62
|
-
# update:: /updated/
|
63
|
-
attr_accessor :flash
|
64
|
-
|
65
|
-
# Hash describing the params that should be sent in with this action.
|
66
|
-
attr_accessor :params
|
67
|
-
end
|
68
|
-
|
69
|
-
# Configuration options for the denied actions under should_be_restful
|
70
|
-
#
|
71
|
-
# Example:
|
72
|
-
# context "The public" do
|
73
|
-
# setup do
|
74
|
-
# @request.session[:logged_in] = false
|
75
|
-
# end
|
76
|
-
#
|
77
|
-
# should_be_restful do |resource|
|
78
|
-
# resource.parent = :user
|
79
|
-
#
|
80
|
-
# resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
|
81
|
-
# resource.denied.flash = /get outta here/i
|
82
|
-
# resource.denied.redirect = 'new_session_url'
|
83
|
-
# end
|
84
|
-
# end
|
85
|
-
#
|
86
|
-
class DeniedOptions
|
87
|
-
# String evaled to get the target of the redirection.
|
88
|
-
# All of the instance variables set by the controller will be available to the
|
89
|
-
# evaled code.
|
90
|
-
#
|
91
|
-
# Example:
|
92
|
-
# resource.create.redirect = "user_url(@user.company, @user)"
|
93
|
-
attr_accessor :redirect
|
94
|
-
|
95
|
-
# String or Regexp describing a value expected in the flash. Will match against any flash key.
|
96
|
-
#
|
97
|
-
# Example:
|
98
|
-
# resource.create.flash = /created/
|
99
|
-
attr_accessor :flash
|
100
|
-
|
101
|
-
# Actions that should be denied (only used by resource.denied). <i>Note that these actions will
|
102
|
-
# only be tested if they are also listed in +resource.actions+</i>
|
103
|
-
# The special value of :all will deny all of the REST actions.
|
104
|
-
attr_accessor :actions
|
105
|
-
end
|
106
|
-
|
107
|
-
# Name of key in params that references the primary key.
|
108
|
-
# Will almost always be :id (default), unless you are using a plugin or have patched rails.
|
109
|
-
attr_accessor :identifier
|
110
|
-
|
111
|
-
# Name of the ActiveRecord class this resource is responsible for. Automatically determined from
|
112
|
-
# test class if not explicitly set. UserTest => "User"
|
113
|
-
attr_accessor :klass
|
114
|
-
|
115
|
-
# Name of the instantiated ActiveRecord object that should be used by some of the tests.
|
116
|
-
# Defaults to the underscored name of the AR class. CompanyManager => :company_manager
|
117
|
-
attr_accessor :object
|
118
|
-
|
119
|
-
# Name of the parent AR objects. Can be set as parent= or parents=, and can take either
|
120
|
-
# the name of the parent resource (if there's only one), or an array of names (if there's
|
121
|
-
# more than one).
|
122
|
-
#
|
123
|
-
# Example:
|
124
|
-
# # in the routes...
|
125
|
-
# map.resources :companies do
|
126
|
-
# map.resources :people do
|
127
|
-
# map.resources :limbs
|
128
|
-
# end
|
129
|
-
# end
|
130
|
-
#
|
131
|
-
# # in the tests...
|
132
|
-
# class PeopleControllerTest < Test::Unit::TestCase
|
133
|
-
# should_be_restful do |resource|
|
134
|
-
# resource.parent = :companies
|
135
|
-
# end
|
136
|
-
# end
|
137
|
-
#
|
138
|
-
# class LimbsControllerTest < Test::Unit::TestCase
|
139
|
-
# should_be_restful do |resource|
|
140
|
-
# resource.parents = [:companies, :people]
|
141
|
-
# end
|
142
|
-
# end
|
143
|
-
attr_accessor :parent
|
144
|
-
alias parents parent
|
145
|
-
alias parents= parent=
|
146
|
-
|
147
|
-
# Actions that should be tested. Must be a subset of VALID_ACTIONS (default).
|
148
|
-
# Tests for each actionw will only be generated if the action is listed here.
|
149
|
-
# The special value of :all will test all of the REST actions.
|
150
|
-
#
|
151
|
-
# Example (for a read-only controller):
|
152
|
-
# resource.actions = [:show, :index]
|
153
|
-
attr_accessor :actions
|
154
|
-
|
155
|
-
# Formats that should be tested. Must be a subset of VALID_FORMATS (default).
|
156
|
-
# Each action will be tested against the formats listed here. The special value
|
157
|
-
# of :all will test all of the supported formats.
|
158
|
-
#
|
159
|
-
# Example:
|
160
|
-
# resource.actions = [:html, :xml]
|
161
|
-
attr_accessor :formats
|
162
|
-
|
163
|
-
# ActionOptions object specifying options for the create action.
|
164
|
-
attr_accessor :create
|
165
|
-
|
166
|
-
# ActionOptions object specifying options for the update action.
|
167
|
-
attr_accessor :update
|
168
|
-
|
169
|
-
# ActionOptions object specifying options for the desrtoy action.
|
170
|
-
attr_accessor :destroy
|
171
|
-
|
172
|
-
# DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
|
173
|
-
attr_accessor :denied
|
174
|
-
|
175
|
-
def initialize # :nodoc:
|
176
|
-
@create = ActionOptions.new
|
177
|
-
@update = ActionOptions.new
|
178
|
-
@destroy = ActionOptions.new
|
179
|
-
@denied = DeniedOptions.new
|
180
|
-
|
181
|
-
@create.flash ||= /created/i
|
182
|
-
@update.flash ||= /updated/i
|
183
|
-
@destroy.flash ||= /removed/i
|
184
|
-
@denied.flash ||= /denied/i
|
185
|
-
|
186
|
-
@create.params ||= {}
|
187
|
-
@update.params ||= {}
|
188
|
-
|
189
|
-
@actions = VALID_ACTIONS
|
190
|
-
@formats = VALID_FORMATS
|
191
|
-
@denied.actions = []
|
192
|
-
end
|
193
|
-
|
194
|
-
def normalize!(target) # :nodoc:
|
195
|
-
@denied.actions = VALID_ACTIONS if @denied.actions == :all
|
196
|
-
@actions = VALID_ACTIONS if @actions == :all
|
197
|
-
@formats = VALID_FORMATS if @formats == :all
|
198
|
-
|
199
|
-
@denied.actions = @denied.actions.map(&:to_sym)
|
200
|
-
@actions = @actions.map(&:to_sym)
|
201
|
-
@formats = @formats.map(&:to_sym)
|
202
|
-
|
203
|
-
ensure_valid_members(@actions, VALID_ACTIONS, 'actions')
|
204
|
-
ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions')
|
205
|
-
ensure_valid_members(@formats, VALID_FORMATS, 'formats')
|
206
|
-
|
207
|
-
@identifier ||= :id
|
208
|
-
@klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
|
209
|
-
@object ||= @klass.name.tableize.singularize
|
210
|
-
@parent ||= []
|
211
|
-
@parent = [@parent] unless @parent.is_a? Array
|
212
|
-
|
213
|
-
collection_helper = [@parent, @object.to_s.pluralize, 'url'].flatten.join('_')
|
214
|
-
collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ')
|
215
|
-
@destroy.redirect ||= "#{collection_helper}(#{collection_args})"
|
216
|
-
|
217
|
-
member_helper = [@parent, @object, 'url'].flatten.join('_')
|
218
|
-
member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ')
|
219
|
-
@create.redirect ||= "#{member_helper}(#{member_args})"
|
220
|
-
@update.redirect ||= "#{member_helper}(#{member_args})"
|
221
|
-
@denied.redirect ||= "new_session_url"
|
222
|
-
end
|
223
|
-
|
224
|
-
private
|
225
|
-
|
226
|
-
def ensure_valid_members(ary, valid_members, name) # :nodoc:
|
227
|
-
invalid = ary - valid_members
|
228
|
-
raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty?
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|