francois-shoulda 2.0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +12 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +119 -0
  4. data/Rakefile +72 -0
  5. data/bin/convert_to_should_syntax +42 -0
  6. data/lib/shoulda.rb +17 -0
  7. data/lib/shoulda/action_mailer.rb +10 -0
  8. data/lib/shoulda/action_mailer/assertions.rb +39 -0
  9. data/lib/shoulda/active_record.rb +12 -0
  10. data/lib/shoulda/active_record/assertions.rb +96 -0
  11. data/lib/shoulda/active_record/macros.rb +752 -0
  12. data/lib/shoulda/assertions.rb +49 -0
  13. data/lib/shoulda/context.rb +306 -0
  14. data/lib/shoulda/controller.rb +30 -0
  15. data/lib/shoulda/controller/formats/html.rb +201 -0
  16. data/lib/shoulda/controller/formats/xml.rb +170 -0
  17. data/lib/shoulda/controller/helpers.rb +64 -0
  18. data/lib/shoulda/controller/macros.rb +316 -0
  19. data/lib/shoulda/controller/resource_options.rb +236 -0
  20. data/lib/shoulda/helpers.rb +10 -0
  21. data/lib/shoulda/macros.rb +74 -0
  22. data/lib/shoulda/private_helpers.rb +22 -0
  23. data/lib/shoulda/proc_extensions.rb +14 -0
  24. data/lib/shoulda/rails.rb +19 -0
  25. data/lib/shoulda/tasks.rb +3 -0
  26. data/lib/shoulda/tasks/list_tests.rake +24 -0
  27. data/lib/shoulda/tasks/yaml_to_shoulda.rake +28 -0
  28. data/rails/init.rb +1 -0
  29. data/test/README +36 -0
  30. data/test/fail_macros.rb +34 -0
  31. data/test/fixtures/addresses.yml +3 -0
  32. data/test/fixtures/friendships.yml +0 -0
  33. data/test/fixtures/posts.yml +5 -0
  34. data/test/fixtures/products.yml +0 -0
  35. data/test/fixtures/taggings.yml +0 -0
  36. data/test/fixtures/tags.yml +11 -0
  37. data/test/fixtures/users.yml +6 -0
  38. data/test/functional/posts_controller_test.rb +105 -0
  39. data/test/functional/users_controller_test.rb +38 -0
  40. data/test/other/context_test.rb +145 -0
  41. data/test/other/convert_to_should_syntax_test.rb +63 -0
  42. data/test/other/helpers_test.rb +183 -0
  43. data/test/other/private_helpers_test.rb +34 -0
  44. data/test/other/should_test.rb +266 -0
  45. data/test/rails_root/app/controllers/application.rb +25 -0
  46. data/test/rails_root/app/controllers/posts_controller.rb +85 -0
  47. data/test/rails_root/app/controllers/users_controller.rb +84 -0
  48. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  49. data/test/rails_root/app/helpers/posts_helper.rb +2 -0
  50. data/test/rails_root/app/helpers/users_helper.rb +2 -0
  51. data/test/rails_root/app/models/address.rb +7 -0
  52. data/test/rails_root/app/models/dog.rb +5 -0
  53. data/test/rails_root/app/models/flea.rb +3 -0
  54. data/test/rails_root/app/models/friendship.rb +4 -0
  55. data/test/rails_root/app/models/post.rb +12 -0
  56. data/test/rails_root/app/models/product.rb +12 -0
  57. data/test/rails_root/app/models/tag.rb +8 -0
  58. data/test/rails_root/app/models/tagging.rb +4 -0
  59. data/test/rails_root/app/models/user.rb +28 -0
  60. data/test/rails_root/app/views/layouts/posts.rhtml +17 -0
  61. data/test/rails_root/app/views/layouts/users.rhtml +17 -0
  62. data/test/rails_root/app/views/layouts/wide.html.erb +1 -0
  63. data/test/rails_root/app/views/posts/edit.rhtml +27 -0
  64. data/test/rails_root/app/views/posts/index.rhtml +25 -0
  65. data/test/rails_root/app/views/posts/new.rhtml +26 -0
  66. data/test/rails_root/app/views/posts/show.rhtml +18 -0
  67. data/test/rails_root/app/views/users/edit.rhtml +22 -0
  68. data/test/rails_root/app/views/users/index.rhtml +22 -0
  69. data/test/rails_root/app/views/users/new.rhtml +21 -0
  70. data/test/rails_root/app/views/users/show.rhtml +13 -0
  71. data/test/rails_root/config/boot.rb +109 -0
  72. data/test/rails_root/config/database.yml +4 -0
  73. data/test/rails_root/config/environment.rb +14 -0
  74. data/test/rails_root/config/environments/sqlite3.rb +0 -0
  75. data/test/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  76. data/test/rails_root/config/initializers/shoulda.rb +8 -0
  77. data/test/rails_root/config/routes.rb +6 -0
  78. data/test/rails_root/db/migrate/001_create_users.rb +18 -0
  79. data/test/rails_root/db/migrate/002_create_posts.rb +13 -0
  80. data/test/rails_root/db/migrate/003_create_taggings.rb +12 -0
  81. data/test/rails_root/db/migrate/004_create_tags.rb +11 -0
  82. data/test/rails_root/db/migrate/005_create_dogs.rb +12 -0
  83. data/test/rails_root/db/migrate/006_create_addresses.rb +14 -0
  84. data/test/rails_root/db/migrate/007_create_fleas.rb +11 -0
  85. data/test/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  86. data/test/rails_root/db/migrate/009_create_products.rb +17 -0
  87. data/test/rails_root/db/migrate/010_create_friendships.rb +14 -0
  88. data/test/rails_root/db/schema.rb +0 -0
  89. data/test/rails_root/public/404.html +30 -0
  90. data/test/rails_root/public/422.html +30 -0
  91. data/test/rails_root/public/500.html +30 -0
  92. data/test/rails_root/script/console +3 -0
  93. data/test/rails_root/script/generate +3 -0
  94. data/test/test_helper.rb +33 -0
  95. data/test/unit/address_test.rb +32 -0
  96. data/test/unit/dog_test.rb +7 -0
  97. data/test/unit/flea_test.rb +6 -0
  98. data/test/unit/friendship_test.rb +6 -0
  99. data/test/unit/post_test.rb +15 -0
  100. data/test/unit/product_test.rb +27 -0
  101. data/test/unit/tag_test.rb +15 -0
  102. data/test/unit/tagging_test.rb +6 -0
  103. data/test/unit/user_test.rb +56 -0
  104. metadata +197 -0
@@ -0,0 +1,49 @@
1
+ module ThoughtBot # :nodoc:
2
+ module Shoulda # :nodoc:
3
+ module Assertions
4
+ # Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
5
+ #
6
+ # assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes
7
+ def assert_same_elements(a1, a2, msg = nil)
8
+ [:select, :inject, :size].each do |m|
9
+ [a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
10
+ end
11
+
12
+ assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
13
+ assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
14
+
15
+ assert_equal(a1h, a2h, msg)
16
+ end
17
+
18
+ # Asserts that the given collection contains item x. If x is a regular expression, ensure that
19
+ # at least one element from the collection matches x. +extra_msg+ is appended to the error message if the assertion fails.
20
+ #
21
+ # assert_contains(['a', '1'], /\d/) => passes
22
+ # assert_contains(['a', '1'], 'a') => passes
23
+ # assert_contains(['a', '1'], /not there/) => fails
24
+ def assert_contains(collection, x, extra_msg = "")
25
+ collection = [collection] unless collection.is_a?(Array)
26
+ msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
27
+ case x
28
+ when Regexp
29
+ assert(collection.detect { |e| e =~ x }, msg)
30
+ else
31
+ assert(collection.include?(x), msg)
32
+ end
33
+ end
34
+
35
+ # Asserts that the given collection does not contain item x. If x is a regular expression, ensure that
36
+ # none of the elements from the collection match x.
37
+ def assert_does_not_contain(collection, x, extra_msg = "")
38
+ collection = [collection] unless collection.is_a?(Array)
39
+ msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
40
+ case x
41
+ when Regexp
42
+ assert(!collection.detect { |e| e =~ x }, msg)
43
+ else
44
+ assert(!collection.include?(x), msg)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,306 @@
1
+ module Thoughtbot # :nodoc:
2
+ module Shoulda
3
+ VERSION = '2.0.5'
4
+
5
+ class << self
6
+ attr_accessor :contexts
7
+ def contexts # :nodoc:
8
+ @contexts ||= []
9
+ end
10
+
11
+ def current_context # :nodoc:
12
+ self.contexts.last
13
+ end
14
+
15
+ def add_context(context) # :nodoc:
16
+ self.contexts.push(context)
17
+ end
18
+
19
+ def remove_context # :nodoc:
20
+ self.contexts.pop
21
+ end
22
+ end
23
+
24
+ # == Should statements
25
+ #
26
+ # Should statements are just syntactic sugar over normal Test::Unit test methods. A should block
27
+ # contains all the normal code and assertions you're used to seeing, with the added benefit that
28
+ # they can be wrapped inside context blocks (see below).
29
+ #
30
+ # === Example:
31
+ #
32
+ # class UserTest << Test::Unit::TestCase
33
+ #
34
+ # def setup
35
+ # @user = User.new("John", "Doe")
36
+ # end
37
+ #
38
+ # should "return its full name"
39
+ # assert_equal 'John Doe', @user.full_name
40
+ # end
41
+ #
42
+ # end
43
+ #
44
+ # ...will produce the following test:
45
+ # * <tt>"test: User should return its full name. "</tt>
46
+ #
47
+ # Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
48
+ #
49
+ # Should statements can also take a Proc as a <tt>:before </tt>option. This proc runs after any
50
+ # parent context's setups but before the current context's setup.
51
+ #
52
+ # === Example:
53
+ #
54
+ # context "Some context" do
55
+ # setup { puts("I run after the :before proc") }
56
+ #
57
+ # should "run a :before proc", :before => lambda { puts("I run before the setup") } do
58
+ # assert true
59
+ # end
60
+ # end
61
+
62
+ def should(name, options = {}, &blk)
63
+ if Shoulda.current_context
64
+ block_given? ? Shoulda.current_context.should(name, options, &blk) : Should.current_context.should_eventually(name)
65
+ else
66
+ context_name = self.name.gsub(/Test/, "")
67
+ context = Thoughtbot::Shoulda::Context.new(context_name, self) do
68
+ block_given? ? should(name, options, &blk) : should_eventually(name)
69
+ end
70
+ context.build
71
+ end
72
+ end
73
+
74
+ # == Before statements
75
+ #
76
+ # Before statements are should statements that run before the current
77
+ # context's setup. These are especially useful when setting expectations.
78
+ #
79
+ # === Example:
80
+ #
81
+ # class UserControllerTest < Test::Unit::TestCase
82
+ # context "the index action" do
83
+ # setup do
84
+ # @users = [Factory(:user)]
85
+ # User.stubs(:find).returns(@users)
86
+ # end
87
+ #
88
+ # context "on GET" do
89
+ # setup { get :index }
90
+ #
91
+ # should_respond_with :success
92
+ #
93
+ # # runs before "get :index"
94
+ # before_should "find all users" do
95
+ # User.expects(:find).with(:all).returns(@users)
96
+ # end
97
+ # end
98
+ # end
99
+ # end
100
+ def before_should(name, &blk)
101
+ should(name, :before => blk) { assert true }
102
+ end
103
+
104
+ # Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
105
+ def should_eventually(name, options = {}, &blk)
106
+ context_name = self.name.gsub(/Test/, "")
107
+ context = Thoughtbot::Shoulda::Context.new(context_name, self) do
108
+ should_eventually(name, &blk)
109
+ end
110
+ context.build
111
+ end
112
+
113
+ # == Contexts
114
+ #
115
+ # A context block groups should statements under a common set of setup/teardown methods.
116
+ # Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
117
+ # and readability of your test code.
118
+ #
119
+ # A context block can contain setup, should, should_eventually, and teardown blocks.
120
+ #
121
+ # class UserTest << Test::Unit::TestCase
122
+ # context "A User instance" do
123
+ # setup do
124
+ # @user = User.find(:first)
125
+ # end
126
+ #
127
+ # should "return its full name"
128
+ # assert_equal 'John Doe', @user.full_name
129
+ # end
130
+ # end
131
+ # end
132
+ #
133
+ # This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
134
+ #
135
+ # Contexts may be nested. Nested contexts run their setup blocks from out to in before each
136
+ # should statement. They then run their teardown blocks from in to out after each should statement.
137
+ #
138
+ # class UserTest << Test::Unit::TestCase
139
+ # context "A User instance" do
140
+ # setup do
141
+ # @user = User.find(:first)
142
+ # end
143
+ #
144
+ # should "return its full name"
145
+ # assert_equal 'John Doe', @user.full_name
146
+ # end
147
+ #
148
+ # context "with a profile" do
149
+ # setup do
150
+ # @user.profile = Profile.find(:first)
151
+ # end
152
+ #
153
+ # should "return true when sent :has_profile?"
154
+ # assert @user.has_profile?
155
+ # end
156
+ # end
157
+ # end
158
+ # end
159
+ #
160
+ # This code will produce the following methods
161
+ # * <tt>"test: A User instance should return its full name. "</tt>
162
+ # * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
163
+ #
164
+ # <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
165
+ # tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
166
+
167
+ def context(name, &blk)
168
+ if Shoulda.current_context
169
+ Shoulda.current_context.context(name, &blk)
170
+ else
171
+ context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
172
+ context.build
173
+ end
174
+ end
175
+
176
+ class Context # :nodoc:
177
+
178
+ attr_accessor :name # my name
179
+ attr_accessor :parent # may be another context, or the original test::unit class.
180
+ attr_accessor :subcontexts # array of contexts nested under myself
181
+ attr_accessor :setup_blocks # blocks given via setup methods
182
+ attr_accessor :teardown_blocks # blocks given via teardown methods
183
+ attr_accessor :shoulds # array of hashes representing the should statements
184
+ attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
185
+
186
+ def initialize(name, parent, &blk)
187
+ Shoulda.add_context(self)
188
+ self.name = name
189
+ self.parent = parent
190
+ self.setup_blocks = []
191
+ self.teardown_blocks = []
192
+ self.shoulds = []
193
+ self.should_eventuallys = []
194
+ self.subcontexts = []
195
+
196
+ merge_block(&blk)
197
+ Shoulda.remove_context
198
+ end
199
+
200
+ def merge_block(&blk)
201
+ blk.bind(self).call
202
+ end
203
+
204
+ def context(name, &blk)
205
+ self.subcontexts << Context.new(name, self, &blk)
206
+ end
207
+
208
+ def setup(&blk)
209
+ self.setup_blocks << blk
210
+ end
211
+
212
+ def teardown(&blk)
213
+ self.teardown_blocks << blk
214
+ end
215
+
216
+ def should(name, options = {}, &blk)
217
+ if block_given?
218
+ self.shoulds << { :name => name, :before => options[:before], :block => blk }
219
+ else
220
+ self.should_eventuallys << { :name => name }
221
+ end
222
+ end
223
+
224
+ def should_eventually(name, &blk)
225
+ self.should_eventuallys << { :name => name, :block => blk }
226
+ end
227
+
228
+ def full_name
229
+ parent_name = parent.full_name if am_subcontext?
230
+ return [parent_name, name].join(" ").strip
231
+ end
232
+
233
+ def am_subcontext?
234
+ parent.is_a?(self.class) # my parent is the same class as myself.
235
+ end
236
+
237
+ def test_unit_class
238
+ am_subcontext? ? parent.test_unit_class : parent
239
+ end
240
+
241
+ def create_test_from_should_hash(should)
242
+ test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
243
+
244
+ if test_unit_class.instance_methods.include?(test_name.to_s)
245
+ warn " * WARNING: '#{test_name}' is already defined"
246
+ end
247
+
248
+ context = self
249
+ test_unit_class.send(:define_method, test_name) do
250
+ begin
251
+ context.run_parent_setup_blocks(self)
252
+ should[:before].bind(self).call if should[:before]
253
+ context.run_current_setup_blocks(self)
254
+ should[:block].bind(self).call
255
+ ensure
256
+ context.run_all_teardown_blocks(self)
257
+ end
258
+ end
259
+ end
260
+
261
+ def run_all_setup_blocks(binding)
262
+ run_parent_setup_blocks(binding)
263
+ run_current_setup_blocks(binding)
264
+ end
265
+
266
+ def run_parent_setup_blocks(binding)
267
+ self.parent.run_all_setup_blocks(binding) if am_subcontext?
268
+ end
269
+
270
+ def run_current_setup_blocks(binding)
271
+ setup_blocks.each do |setup_block|
272
+ setup_block.bind(binding).call
273
+ end
274
+ end
275
+
276
+ def run_all_teardown_blocks(binding)
277
+ teardown_blocks.reverse.each do |teardown_block|
278
+ teardown_block.bind(binding).call
279
+ end
280
+ self.parent.run_all_teardown_blocks(binding) if am_subcontext?
281
+ end
282
+
283
+ def print_should_eventuallys
284
+ should_eventuallys.each do |should|
285
+ test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
286
+ puts " * DEFERRED: " + test_name
287
+ end
288
+ end
289
+
290
+ def build
291
+ shoulds.each do |should|
292
+ create_test_from_should_hash(should)
293
+ end
294
+
295
+ subcontexts.each { |context| context.build }
296
+
297
+ print_should_eventuallys
298
+ end
299
+
300
+ def method_missing(method, *args, &blk)
301
+ test_unit_class.send(method, *args, &blk)
302
+ end
303
+
304
+ end
305
+ end
306
+ end
@@ -0,0 +1,30 @@
1
+ require 'shoulda'
2
+ require 'shoulda/controller/helpers'
3
+ require 'shoulda/controller/resource_options'
4
+ require 'shoulda/controller/macros'
5
+
6
+ module Test # :nodoc: all
7
+ module Unit
8
+ class TestCase
9
+ extend ThoughtBot::Shoulda::Controller::Macros
10
+ include ThoughtBot::Shoulda::Controller::Helpers
11
+ ThoughtBot::Shoulda::Controller::VALID_FORMATS.each do |format|
12
+ include "ThoughtBot::Shoulda::Controller::#{format.to_s.upcase}".constantize
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ require 'shoulda/active_record/assertions'
19
+ require 'shoulda/action_mailer/assertions'
20
+
21
+ module ActionController #:nodoc: all
22
+ module Integration
23
+ class Session
24
+ include ThoughtBot::Shoulda::Assertions
25
+ include ThoughtBot::Shoulda::Helpers
26
+ include ThoughtBot::Shoulda::ActiveRecord::Assertions
27
+ include ThoughtBot::Shoulda::ActionMailer::Assertions
28
+ end
29
+ end
30
+ end
@@ -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