nimboids-shoulda 2.11.3

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.
Files changed (258) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +154 -0
  4. data/Rakefile +74 -0
  5. data/bin/convert_to_should_syntax +42 -0
  6. data/lib/shoulda/action_controller/macros.rb +221 -0
  7. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +114 -0
  8. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +74 -0
  9. data/lib/shoulda/action_controller/matchers/redirect_to_matcher.rb +62 -0
  10. data/lib/shoulda/action_controller/matchers/render_template_matcher.rb +54 -0
  11. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +99 -0
  12. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +74 -0
  13. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +85 -0
  14. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  15. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +98 -0
  16. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +100 -0
  17. data/lib/shoulda/action_controller/matchers.rb +39 -0
  18. data/lib/shoulda/action_controller.rb +34 -0
  19. data/lib/shoulda/action_mailer/assertions.rb +42 -0
  20. data/lib/shoulda/action_mailer/matchers/have_sent_email.rb +110 -0
  21. data/lib/shoulda/action_mailer/matchers.rb +22 -0
  22. data/lib/shoulda/action_mailer.rb +13 -0
  23. data/lib/shoulda/active_record/assertions.rb +69 -0
  24. data/lib/shoulda/active_record/helpers.rb +32 -0
  25. data/lib/shoulda/active_record/macros.rb +457 -0
  26. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  27. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +110 -0
  28. data/lib/shoulda/active_record/matchers/association_matcher.rb +275 -0
  29. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  30. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  31. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  32. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +112 -0
  33. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  34. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  35. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +65 -0
  36. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  37. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  38. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  39. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  40. data/lib/shoulda/active_record/matchers.rb +42 -0
  41. data/lib/shoulda/active_record.rb +16 -0
  42. data/lib/shoulda/assertions.rb +79 -0
  43. data/lib/shoulda/autoload_macros.rb +46 -0
  44. data/lib/shoulda/context.rb +441 -0
  45. data/lib/shoulda/helpers.rb +8 -0
  46. data/lib/shoulda/integrations/rspec.rb +13 -0
  47. data/lib/shoulda/integrations/rspec2.rb +22 -0
  48. data/lib/shoulda/integrations/test_unit.rb +22 -0
  49. data/lib/shoulda/macros.rb +161 -0
  50. data/lib/shoulda/private_helpers.rb +13 -0
  51. data/lib/shoulda/proc_extensions.rb +14 -0
  52. data/lib/shoulda/rails.rb +8 -0
  53. data/lib/shoulda/tasks/list_tests.rake +29 -0
  54. data/lib/shoulda/tasks/yaml_to_shoulda.rake +28 -0
  55. data/lib/shoulda/tasks.rb +3 -0
  56. data/lib/shoulda/version.rb +4 -0
  57. data/lib/shoulda.rb +9 -0
  58. data/rails/init.rb +8 -0
  59. data/test/README +36 -0
  60. data/test/fail_macros.rb +55 -0
  61. data/test/fixtures/addresses.yml +3 -0
  62. data/test/fixtures/friendships.yml +0 -0
  63. data/test/fixtures/posts.yml +5 -0
  64. data/test/fixtures/products.yml +0 -0
  65. data/test/fixtures/taggings.yml +0 -0
  66. data/test/fixtures/tags.yml +9 -0
  67. data/test/fixtures/users.yml +6 -0
  68. data/test/functional/posts_controller_test.rb +121 -0
  69. data/test/functional/users_controller_test.rb +19 -0
  70. data/test/matchers/action_mailer/have_sent_email_test.rb +76 -0
  71. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +74 -0
  72. data/test/matchers/active_record/allow_value_matcher_test.rb +64 -0
  73. data/test/matchers/active_record/association_matcher_test.rb +462 -0
  74. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +81 -0
  75. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  76. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  77. data/test/matchers/active_record/have_db_index_matcher_test.rb +91 -0
  78. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  79. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  80. data/test/matchers/active_record/validate_format_of_matcher_test.rb +39 -0
  81. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  82. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  83. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  84. data/test/matchers/controller/assign_to_matcher_test.rb +55 -0
  85. data/test/matchers/controller/filter_param_matcher_test.rb +40 -0
  86. data/test/matchers/controller/redirect_to_matcher_test.rb +37 -0
  87. data/test/matchers/controller/render_template_matcher_test.rb +37 -0
  88. data/test/matchers/controller/render_with_layout_matcher_test.rb +47 -0
  89. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +32 -0
  90. data/test/matchers/controller/respond_with_matcher_test.rb +96 -0
  91. data/test/matchers/controller/route_matcher_test.rb +75 -0
  92. data/test/matchers/controller/set_session_matcher_test.rb +48 -0
  93. data/test/matchers/controller/set_the_flash_matcher.rb +95 -0
  94. data/test/other/autoload_macro_test.rb +18 -0
  95. data/test/other/context_test.rb +372 -0
  96. data/test/other/convert_to_should_syntax_test.rb +63 -0
  97. data/test/other/helpers_test.rb +317 -0
  98. data/test/other/private_helpers_test.rb +32 -0
  99. data/test/other/should_test.rb +271 -0
  100. data/test/rails2_model_builder.rb +130 -0
  101. data/test/rails2_root/app/controllers/application_controller.rb +22 -0
  102. data/test/rails2_root/app/controllers/posts_controller.rb +87 -0
  103. data/test/rails2_root/app/controllers/users_controller.rb +84 -0
  104. data/test/rails2_root/app/helpers/application_helper.rb +3 -0
  105. data/test/rails2_root/app/helpers/posts_helper.rb +2 -0
  106. data/test/rails2_root/app/helpers/users_helper.rb +2 -0
  107. data/test/rails2_root/app/models/address.rb +7 -0
  108. data/test/rails2_root/app/models/flea.rb +11 -0
  109. data/test/rails2_root/app/models/friendship.rb +4 -0
  110. data/test/rails2_root/app/models/notifier.rb +8 -0
  111. data/test/rails2_root/app/models/pets/cat.rb +7 -0
  112. data/test/rails2_root/app/models/pets/dog.rb +10 -0
  113. data/test/rails2_root/app/models/post.rb +12 -0
  114. data/test/rails2_root/app/models/product.rb +12 -0
  115. data/test/rails2_root/app/models/profile.rb +2 -0
  116. data/test/rails2_root/app/models/registration.rb +2 -0
  117. data/test/rails2_root/app/models/tag.rb +8 -0
  118. data/test/rails2_root/app/models/tagging.rb +4 -0
  119. data/test/rails2_root/app/models/treat.rb +3 -0
  120. data/test/rails2_root/app/models/user.rb +32 -0
  121. data/test/rails2_root/app/views/layouts/posts.rhtml +19 -0
  122. data/test/rails2_root/app/views/layouts/users.rhtml +17 -0
  123. data/test/rails2_root/app/views/layouts/wide.html.erb +1 -0
  124. data/test/rails2_root/app/views/notifier/the_email.html.erb +1 -0
  125. data/test/rails2_root/app/views/posts/edit.rhtml +27 -0
  126. data/test/rails2_root/app/views/posts/index.rhtml +25 -0
  127. data/test/rails2_root/app/views/posts/new.rhtml +26 -0
  128. data/test/rails2_root/app/views/posts/show.rhtml +18 -0
  129. data/test/rails2_root/app/views/users/edit.rhtml +22 -0
  130. data/test/rails2_root/app/views/users/index.rhtml +22 -0
  131. data/test/rails2_root/app/views/users/new.rhtml +21 -0
  132. data/test/rails2_root/app/views/users/show.rhtml +13 -0
  133. data/test/rails2_root/config/boot.rb +110 -0
  134. data/test/rails2_root/config/database.yml +4 -0
  135. data/test/rails2_root/config/environment.rb +17 -0
  136. data/test/rails2_root/config/environments/test.rb +23 -0
  137. data/test/rails2_root/config/initializers/new_rails_defaults.rb +15 -0
  138. data/test/rails2_root/config/initializers/shoulda.rb +8 -0
  139. data/test/rails2_root/config/routes.rb +6 -0
  140. data/test/rails2_root/db/migrate/001_create_users.rb +19 -0
  141. data/test/rails2_root/db/migrate/002_create_posts.rb +13 -0
  142. data/test/rails2_root/db/migrate/003_create_taggings.rb +12 -0
  143. data/test/rails2_root/db/migrate/004_create_tags.rb +11 -0
  144. data/test/rails2_root/db/migrate/005_create_dogs.rb +12 -0
  145. data/test/rails2_root/db/migrate/006_create_addresses.rb +14 -0
  146. data/test/rails2_root/db/migrate/007_create_fleas.rb +11 -0
  147. data/test/rails2_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  148. data/test/rails2_root/db/migrate/009_create_products.rb +17 -0
  149. data/test/rails2_root/db/migrate/010_create_friendships.rb +14 -0
  150. data/test/rails2_root/db/migrate/011_create_treats.rb +12 -0
  151. data/test/rails2_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  152. data/test/rails2_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  153. data/test/rails2_root/db/migrate/20090513104502_create_cats.rb +12 -0
  154. data/test/rails2_root/db/schema.rb +0 -0
  155. data/test/rails2_root/public/404.html +30 -0
  156. data/test/rails2_root/public/422.html +30 -0
  157. data/test/rails2_root/public/500.html +30 -0
  158. data/test/rails2_root/script/console +3 -0
  159. data/test/rails2_root/script/generate +3 -0
  160. data/test/rails2_root/test/shoulda_macros/custom_macro.rb +6 -0
  161. data/test/rails2_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  162. data/test/rails2_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  163. data/test/rails2_test_helper.rb +6 -0
  164. data/test/rails3_model_builder.rb +118 -0
  165. data/test/rails3_root/Gemfile +28 -0
  166. data/test/rails3_root/README +244 -0
  167. data/test/rails3_root/Rakefile +10 -0
  168. data/test/rails3_root/app/controllers/application_controller.rb +22 -0
  169. data/test/rails3_root/app/controllers/posts_controller.rb +87 -0
  170. data/test/rails3_root/app/controllers/users_controller.rb +82 -0
  171. data/test/rails3_root/app/helpers/application_helper.rb +2 -0
  172. data/test/rails3_root/app/models/address.rb +7 -0
  173. data/test/rails3_root/app/models/flea.rb +11 -0
  174. data/test/rails3_root/app/models/friendship.rb +4 -0
  175. data/test/rails3_root/app/models/notifier.rb +8 -0
  176. data/test/rails3_root/app/models/pets/cat.rb +7 -0
  177. data/test/rails3_root/app/models/pets/dog.rb +10 -0
  178. data/test/rails3_root/app/models/post.rb +12 -0
  179. data/test/rails3_root/app/models/product.rb +12 -0
  180. data/test/rails3_root/app/models/profile.rb +2 -0
  181. data/test/rails3_root/app/models/registration.rb +2 -0
  182. data/test/rails3_root/app/models/tag.rb +8 -0
  183. data/test/rails3_root/app/models/tagging.rb +4 -0
  184. data/test/rails3_root/app/models/treat.rb +3 -0
  185. data/test/rails3_root/app/models/user.rb +32 -0
  186. data/test/rails3_root/app/views/layouts/application.html.erb +14 -0
  187. data/test/rails3_root/app/views/layouts/posts.rhtml +19 -0
  188. data/test/rails3_root/app/views/layouts/users.rhtml +17 -0
  189. data/test/rails3_root/app/views/layouts/wide.html.erb +1 -0
  190. data/test/rails3_root/app/views/notifier/the_email.html.erb +1 -0
  191. data/test/rails3_root/app/views/posts/edit.rhtml +27 -0
  192. data/test/rails3_root/app/views/posts/index.rhtml +25 -0
  193. data/test/rails3_root/app/views/posts/new.rhtml +24 -0
  194. data/test/rails3_root/app/views/posts/show.rhtml +18 -0
  195. data/test/rails3_root/app/views/users/edit.rhtml +22 -0
  196. data/test/rails3_root/app/views/users/index.rhtml +22 -0
  197. data/test/rails3_root/app/views/users/new.rhtml +21 -0
  198. data/test/rails3_root/app/views/users/show.rhtml +13 -0
  199. data/test/rails3_root/config/application.rb +46 -0
  200. data/test/rails3_root/config/boot.rb +6 -0
  201. data/test/rails3_root/config/database.yml +22 -0
  202. data/test/rails3_root/config/environment.rb +5 -0
  203. data/test/rails3_root/config/environments/development.rb +19 -0
  204. data/test/rails3_root/config/environments/production.rb +42 -0
  205. data/test/rails3_root/config/environments/test.rb +32 -0
  206. data/test/rails3_root/config/initializers/backtrace_silencers.rb +7 -0
  207. data/test/rails3_root/config/initializers/inflections.rb +10 -0
  208. data/test/rails3_root/config/initializers/mime_types.rb +5 -0
  209. data/test/rails3_root/config/initializers/secret_token.rb +7 -0
  210. data/test/rails3_root/config/initializers/session_store.rb +8 -0
  211. data/test/rails3_root/config/locales/en.yml +5 -0
  212. data/test/rails3_root/config/routes.rb +4 -0
  213. data/test/rails3_root/config.ru +4 -0
  214. data/test/rails3_root/db/migrate/001_create_users.rb +19 -0
  215. data/test/rails3_root/db/migrate/002_create_posts.rb +13 -0
  216. data/test/rails3_root/db/migrate/003_create_taggings.rb +12 -0
  217. data/test/rails3_root/db/migrate/004_create_tags.rb +11 -0
  218. data/test/rails3_root/db/migrate/005_create_dogs.rb +12 -0
  219. data/test/rails3_root/db/migrate/006_create_addresses.rb +14 -0
  220. data/test/rails3_root/db/migrate/007_create_fleas.rb +11 -0
  221. data/test/rails3_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  222. data/test/rails3_root/db/migrate/009_create_products.rb +17 -0
  223. data/test/rails3_root/db/migrate/010_create_friendships.rb +14 -0
  224. data/test/rails3_root/db/migrate/011_create_treats.rb +12 -0
  225. data/test/rails3_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  226. data/test/rails3_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  227. data/test/rails3_root/db/migrate/20090513104502_create_cats.rb +12 -0
  228. data/test/rails3_root/db/seeds.rb +7 -0
  229. data/test/rails3_root/public/404.html +26 -0
  230. data/test/rails3_root/public/422.html +26 -0
  231. data/test/rails3_root/public/500.html +26 -0
  232. data/test/rails3_root/public/favicon.ico +0 -0
  233. data/test/rails3_root/public/images/rails.png +0 -0
  234. data/test/rails3_root/public/index.html +279 -0
  235. data/test/rails3_root/public/javascripts/application.js +2 -0
  236. data/test/rails3_root/public/javascripts/controls.js +965 -0
  237. data/test/rails3_root/public/javascripts/dragdrop.js +974 -0
  238. data/test/rails3_root/public/javascripts/effects.js +1123 -0
  239. data/test/rails3_root/public/javascripts/prototype.js +4874 -0
  240. data/test/rails3_root/public/javascripts/rails.js +118 -0
  241. data/test/rails3_root/public/robots.txt +5 -0
  242. data/test/rails3_root/script/rails +9 -0
  243. data/test/rails3_root/test/performance/browsing_test.rb +9 -0
  244. data/test/rails3_root/test/test_helper.rb +13 -0
  245. data/test/rails3_test_helper.rb +6 -0
  246. data/test/rspec_test.rb +207 -0
  247. data/test/test_helper.rb +36 -0
  248. data/test/unit/address_test.rb +10 -0
  249. data/test/unit/cat_test.rb +7 -0
  250. data/test/unit/dog_test.rb +9 -0
  251. data/test/unit/flea_test.rb +14 -0
  252. data/test/unit/friendship_test.rb +6 -0
  253. data/test/unit/post_test.rb +15 -0
  254. data/test/unit/product_test.rb +23 -0
  255. data/test/unit/tag_test.rb +11 -0
  256. data/test/unit/tagging_test.rb +6 -0
  257. data/test/unit/user_test.rb +46 -0
  258. metadata +330 -0
@@ -0,0 +1,74 @@
1
+ require 'test_helper'
2
+
3
+ class AllowMassAssignmentOfMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "an attribute that is blacklisted from mass-assignment" do
6
+ setup do
7
+ define_model :example, :attr => :string do
8
+ attr_protected :attr
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ should "reject being mass-assignable" do
14
+ assert_rejects allow_mass_assignment_of(:attr), @model
15
+ end
16
+ end
17
+
18
+ context "an attribute that is not whitelisted for mass-assignment" do
19
+ setup do
20
+ define_model :example, :attr => :string, :other => :string do
21
+ attr_accessible :other
22
+ end
23
+ @model = Example.new
24
+ end
25
+
26
+ should "reject being mass-assignable" do
27
+ assert_rejects allow_mass_assignment_of(:attr), @model
28
+ end
29
+ end
30
+
31
+ context "an attribute that is whitelisted for mass-assignment" do
32
+ setup do
33
+ define_model :example, :attr => :string do
34
+ attr_accessible :attr
35
+ end
36
+ @model = Example.new
37
+ end
38
+
39
+ should "accept being mass-assignable" do
40
+ assert_accepts allow_mass_assignment_of(:attr), @model
41
+ end
42
+ end
43
+
44
+ context "an attribute not included in the mass-assignment blacklist" do
45
+ setup do
46
+ define_model :example, :attr => :string, :other => :string do
47
+ attr_protected :other
48
+ end
49
+ @model = Example.new
50
+ end
51
+
52
+ should "accept being mass-assignable" do
53
+ assert_accepts allow_mass_assignment_of(:attr), @model
54
+ end
55
+ end
56
+
57
+ context "an attribute on a class with no protected attributes" do
58
+ setup do
59
+ define_model :example, :attr => :string
60
+ @model = Example.new
61
+ end
62
+
63
+ should "accept being mass-assignable" do
64
+ assert_accepts allow_mass_assignment_of(:attr), @model
65
+ end
66
+
67
+ should "assign a negative failure message" do
68
+ matcher = allow_mass_assignment_of(:attr)
69
+ matcher.matches?(@model)
70
+ assert_not_nil matcher.negative_failure_message
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class AllowValueMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "an attribute with a format validation" do
6
+ setup do
7
+ define_model :example, :attr => :string do
8
+ validates_format_of :attr, :with => /abc/
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ should "allow a good value" do
14
+ assert_accepts allow_value("abcde").for(:attr), @model
15
+ end
16
+
17
+ should "not allow a bad value" do
18
+ assert_rejects allow_value("xyz").for(:attr), @model
19
+ end
20
+ end
21
+
22
+ context "an attribute with a format validation and a custom message" do
23
+ setup do
24
+ define_model :example, :attr => :string do
25
+ validates_format_of :attr, :with => /abc/, :message => 'bad value'
26
+ end
27
+ @model = Example.new
28
+ end
29
+
30
+ should "allow a good value" do
31
+ assert_accepts allow_value('abcde').for(:attr).with_message(/bad/),
32
+ @model
33
+ end
34
+
35
+ should "not allow a bad value" do
36
+ assert_rejects allow_value('xyz').for(:attr).with_message(/bad/),
37
+ @model
38
+ end
39
+ end
40
+
41
+ context "an attribute with several validations" do
42
+ setup do
43
+ define_model :example, :attr => :string do
44
+ validates_presence_of :attr
45
+ validates_length_of :attr, :within => 1..5
46
+ validates_numericality_of :attr, :greater_than_or_equal_to => 1,
47
+ :less_than_or_equal_to => 50000
48
+ end
49
+ @model = Example.new
50
+ end
51
+
52
+ should "allow a good value" do
53
+ assert_accepts allow_value("12345").for(:attr), @model
54
+ end
55
+
56
+ bad_values = [nil, "", "abc", "0", "50001", "123456"]
57
+ bad_values.each do |value|
58
+ should "not allow a bad value (#{value.inspect})" do
59
+ assert_rejects allow_value(value).for(:attr), @model
60
+ end
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,462 @@
1
+ require 'test_helper'
2
+
3
+ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "belong_to" do
6
+ setup do
7
+ @matcher = belong_to(:parent)
8
+ end
9
+
10
+ should "accept a good association with the default foreign key" do
11
+ define_model :parent
12
+ define_model :child, :parent_id => :integer do
13
+ belongs_to :parent
14
+ end
15
+ assert_accepts @matcher, Child.new
16
+ end
17
+
18
+ should "reject a nonexistent association" do
19
+ define_model :child
20
+ assert_rejects @matcher, Child.new
21
+ end
22
+
23
+ should "reject an association of the wrong type" do
24
+ define_model :parent, :child_id => :integer
25
+ child_class = define_model :child do
26
+ has_one :parent
27
+ end
28
+ assert_rejects @matcher, Child.new
29
+ end
30
+
31
+ should "reject an association that has a nonexistent foreign key" do
32
+ define_model :parent
33
+ define_model :child do
34
+ belongs_to :parent
35
+ end
36
+ assert_rejects @matcher, Child.new
37
+ end
38
+
39
+ should "accept an association with an existing custom foreign key" do
40
+ define_model :parent
41
+ define_model :child, :guardian_id => :integer do
42
+ belongs_to :parent, :foreign_key => 'guardian_id'
43
+ end
44
+ assert_accepts @matcher, Child.new
45
+ end
46
+
47
+ should "accept a polymorphic association" do
48
+ define_model :child, :parent_type => :string,
49
+ :parent_id => :integer do
50
+ belongs_to :parent, :polymorphic => true
51
+ end
52
+ assert_accepts @matcher, Child.new
53
+ end
54
+
55
+ should "accept an association with a valid :dependent option" do
56
+ define_model :parent
57
+ define_model :child, :parent_id => :integer do
58
+ belongs_to :parent, :dependent => :destroy
59
+ end
60
+ assert_accepts @matcher.dependent(:destroy), Child.new
61
+ end
62
+
63
+ should "reject an association with a bad :dependent option" do
64
+ define_model :parent
65
+ define_model :child, :parent_id => :integer do
66
+ belongs_to :parent
67
+ end
68
+ assert_rejects @matcher.dependent(:destroy), Child.new
69
+ end
70
+
71
+ should "ignore other options" do
72
+ define_model :parent
73
+ define_model :child, :parent_id => :integer do
74
+ belongs_to :parent, :readonly => true
75
+ end
76
+ assert_accepts @matcher, Child.new
77
+ end
78
+
79
+ context "with options" do
80
+ setup do
81
+ @matcher = belong_to(:parent).with_options(:readonly => true, :touch => true)
82
+ end
83
+
84
+ should "accept a good association with the correct options" do
85
+ define_model :parent
86
+ define_model :child, :parent_id => :integer do
87
+ belongs_to :parent, :readonly => true, :touch => true
88
+ end
89
+ assert_accepts @matcher, Child.new
90
+ end
91
+
92
+ should "reject an association with missing options" do
93
+ define_model :parent
94
+ define_model :child, :parent_id => :integer do
95
+ belongs_to :parent, :readonly => true
96
+ end
97
+ assert_rejects @matcher, Child.new, :message => /missing :touch option/
98
+ end
99
+
100
+ should "reject an association with additional options" do
101
+ define_model :parent
102
+ define_model :child, :parent_id => :integer do
103
+ belongs_to :parent, :readonly => true, :touch => true, :validate => false
104
+ end
105
+ assert_rejects @matcher, Child.new, :message => /unexpected :validate option/
106
+ end
107
+
108
+ should "reject an association with incorrect option values" do
109
+ define_model :parent
110
+ define_model :child, :parent_id => :integer do
111
+ belongs_to :parent, :readonly => true, :touch => false
112
+ end
113
+ assert_rejects @matcher, Child.new,
114
+ :message => /expected :touch option to be true, but got false/
115
+ end
116
+ end
117
+ end
118
+
119
+ context "have_many" do
120
+ setup do
121
+ @matcher = have_many(:children)
122
+ end
123
+
124
+ should "accept a valid association without any options" do
125
+ define_model :child, :parent_id => :integer
126
+ define_model :parent do
127
+ has_many :children
128
+ end
129
+ assert_accepts @matcher, Parent.new
130
+ end
131
+
132
+ should "accept a valid association with a :through option" do
133
+ define_model :child
134
+ define_model :conception, :child_id => :integer,
135
+ :parent_id => :integer do
136
+ belongs_to :child
137
+ end
138
+ define_model :parent do
139
+ has_many :conceptions
140
+ has_many :children, :through => :conceptions
141
+ end
142
+ assert_accepts @matcher, Parent.new
143
+ end
144
+
145
+ should "accept a valid association with an :as option" do
146
+ define_model :child, :guardian_type => :string,
147
+ :guardian_id => :integer
148
+ define_model :parent do
149
+ has_many :children, :as => :guardian
150
+ end
151
+ assert_accepts @matcher, Parent.new
152
+ end
153
+
154
+ should "reject an association that has a nonexistent foreign key" do
155
+ define_model :child
156
+ define_model :parent do
157
+ has_many :children
158
+ end
159
+ assert_rejects @matcher, Parent.new
160
+ end
161
+
162
+ should "reject an association with a bad :as option" do
163
+ define_model :child, :caretaker_type => :string,
164
+ :caretaker_id => :integer
165
+ define_model :parent do
166
+ has_many :children, :as => :guardian
167
+ end
168
+ assert_rejects @matcher, Parent.new
169
+ end
170
+
171
+ should "reject an association that has a bad :through option" do
172
+ define_model :child, :parent_id => :integer
173
+ define_model :parent do
174
+ has_many :children
175
+ end
176
+ assert_rejects @matcher.through(:conceptions),
177
+ Parent.new,
178
+ :message => /does not have any relationship to conceptions/
179
+ end
180
+
181
+ should "reject an association that has the wrong :through option" do
182
+ define_model :child
183
+ define_model :conception, :child_id => :integer,
184
+ :parent_id => :integer do
185
+ belongs_to :child
186
+ end
187
+ define_model :parent do
188
+ has_many :conceptions
189
+ has_many :relationships
190
+ has_many :children, :through => :conceptions
191
+ end
192
+ assert_rejects @matcher.through(:relationships),
193
+ Parent.new,
194
+ :message => /through relationships, but got it through conceptions/
195
+ end
196
+
197
+ should "accept an association with a valid :dependent option" do
198
+ define_model :child, :parent_id => :integer
199
+ define_model :parent do
200
+ has_many :children, :dependent => :destroy
201
+ end
202
+ assert_accepts @matcher.dependent(:destroy), Parent.new
203
+ end
204
+
205
+ should "reject an association with a bad :dependent option" do
206
+ define_model :child, :parent_id => :integer
207
+ define_model :parent do
208
+ has_many :children
209
+ end
210
+ assert_rejects @matcher.dependent(:destroy), Parent.new
211
+ end
212
+
213
+ should "ignore other options" do
214
+ define_model :child, :parent_id => :integer
215
+ define_model :parent do
216
+ has_many :children, :order => "first_name"
217
+ end
218
+ assert_accepts @matcher, Parent.new
219
+ end
220
+
221
+ context "with options" do
222
+ setup do
223
+ @matcher = have_many(:children).with_options(:order => "first_name", :limit =>5)
224
+ end
225
+
226
+ should "accept a good association with the correct options" do
227
+ define_model :child, :parent_id => :integer
228
+ define_model :parent do
229
+ has_many :children, :order => "first_name", :limit =>5
230
+ end
231
+ assert_accepts @matcher, Parent.new
232
+ end
233
+
234
+ should "reject an association with missing options" do
235
+ define_model :child, :parent_id => :integer
236
+ define_model :parent do
237
+ has_many :children, :order => "first_name"
238
+ end
239
+ assert_rejects @matcher, Parent.new, :message => /missing :limit option/
240
+ end
241
+
242
+ should "reject an association with additional options" do
243
+ define_model :child, :parent_id => :integer
244
+ define_model :parent do
245
+ has_many :children, :order => "first_name", :limit =>5, :validate => false
246
+ end
247
+ assert_rejects @matcher, Parent.new, :message => /unexpected :validate option/
248
+ end
249
+
250
+ should "reject an association with incorrect option values" do
251
+ define_model :child, :parent_id => :integer
252
+ define_model :parent do
253
+ has_many :children, :order => "first_name", :limit => 2
254
+ end
255
+ assert_rejects @matcher, Parent.new,
256
+ :message => /expected :limit option to be 5, but got 2/
257
+ end
258
+ end
259
+ end
260
+
261
+ context "have_one" do
262
+ setup do
263
+ @matcher = have_one(:detail)
264
+ end
265
+
266
+ should "accept a valid association without any options" do
267
+ define_model :detail, :person_id => :integer
268
+ define_model :person do
269
+ has_one :detail
270
+ end
271
+ assert_accepts @matcher, Person.new
272
+ end
273
+
274
+ should "accept a valid association with an :as option" do
275
+ define_model :detail, :detailable_id => :integer,
276
+ :detailable_type => :string
277
+ define_model :person do
278
+ has_one :detail, :as => :detailable
279
+ end
280
+ assert_accepts @matcher, Person.new
281
+ end
282
+
283
+ should "reject an association that has a nonexistent foreign key" do
284
+ define_model :detail
285
+ define_model :person do
286
+ has_one :detail
287
+ end
288
+ assert_rejects @matcher, Person.new
289
+ end
290
+
291
+ should "reject an association with a bad :as option" do
292
+ define_model :detail, :detailable_id => :integer,
293
+ :detailable_type => :string
294
+ define_model :person do
295
+ has_one :detail, :as => :describable
296
+ end
297
+ assert_rejects @matcher, Person.new
298
+ end
299
+
300
+ should "accept an association with a valid :dependent option" do
301
+ define_model :detail, :person_id => :integer
302
+ define_model :person do
303
+ has_one :detail, :dependent => :destroy
304
+ end
305
+ assert_accepts @matcher.dependent(:destroy), Person.new
306
+ end
307
+
308
+ should "reject an association with a bad :dependent option" do
309
+ define_model :detail, :person_id => :integer
310
+ define_model :person do
311
+ has_one :detail
312
+ end
313
+ assert_rejects @matcher.dependent(:destroy), Person.new
314
+ end
315
+
316
+ should "ignore other options" do
317
+ define_model :detail, :person_id => :integer
318
+ define_model :person do
319
+ has_one :detail, :readonly => true
320
+ end
321
+ assert_accepts @matcher, Person.new
322
+ end
323
+
324
+ context "with options" do
325
+ setup do
326
+ @matcher = have_one(:detail).with_options(:readonly => true, :autosave => false)
327
+ end
328
+
329
+ should "accept a good association with the correct options" do
330
+ define_model :detail, :person_id => :integer
331
+ define_model :person do
332
+ has_one :detail, :readonly => true, :autosave => false
333
+ end
334
+ assert_accepts @matcher, Person.new
335
+ end
336
+
337
+ should "reject an association with missing options" do
338
+ define_model :detail, :person_id => :integer
339
+ define_model :person do
340
+ has_one :detail, :readonly => true
341
+ end
342
+ assert_rejects @matcher, Person.new, :message => /missing :autosave option/
343
+ end
344
+
345
+ should "reject an association with additional options" do
346
+ define_model :detail, :person_id => :integer
347
+ define_model :person do
348
+ has_one :detail, :readonly => true, :autosave => false, :validate => true
349
+ end
350
+ assert_rejects @matcher, Person.new, :message => /unexpected :validate option/
351
+ end
352
+
353
+ should "reject an association with incorrect option values" do
354
+ define_model :detail, :person_id => :integer
355
+ define_model :person do
356
+ has_one :detail, :readonly => true, :autosave => true
357
+ end
358
+ assert_rejects @matcher, Person.new,
359
+ :message => /expected :autosave option to be false, but got true/
360
+ end
361
+ end
362
+ end
363
+
364
+ context "have_and_belong_to_many" do
365
+ setup do
366
+ @matcher = have_and_belong_to_many(:relatives)
367
+ end
368
+
369
+ should "accept a valid association" do
370
+ define_model :relatives
371
+ define_model :person do
372
+ has_and_belongs_to_many :relatives
373
+ end
374
+ define_model :people_relative, :person_id => :integer,
375
+ :relative_id => :integer
376
+ assert_accepts @matcher, Person.new
377
+ end
378
+
379
+ should "reject a nonexistent association" do
380
+ define_model :relatives
381
+ define_model :person
382
+ define_model :people_relative, :person_id => :integer,
383
+ :relative_id => :integer
384
+ assert_rejects @matcher, Person.new
385
+ end
386
+
387
+ should "reject an association with a nonexistent join table" do
388
+ define_model :relatives
389
+ define_model :person do
390
+ has_and_belongs_to_many :relatives
391
+ end
392
+ assert_rejects @matcher, Person.new
393
+ end
394
+
395
+ should "reject an association of the wrong type" do
396
+ define_model :relatives, :person_id => :integer
397
+ define_model :person do
398
+ has_many :relatives
399
+ end
400
+ assert_rejects @matcher, Person.new
401
+ end
402
+
403
+ should "ignore other options" do
404
+ define_model :relatives
405
+ define_model :person do
406
+ has_and_belongs_to_many :relatives, :order => "surname"
407
+ end
408
+ define_model :people_relative, :person_id => :integer,
409
+ :relative_id => :integer
410
+ assert_accepts @matcher, Person.new
411
+ end
412
+
413
+ context "with options" do
414
+ setup do
415
+ @matcher = have_and_belong_to_many(:relatives).with_options(
416
+ :order => "surname", :limit =>5)
417
+ end
418
+
419
+ should "accept a good association with the correct options" do
420
+ define_model :relatives
421
+ define_model :person do
422
+ has_and_belongs_to_many :relatives, :order => "surname", :limit =>5
423
+ end
424
+ define_model :people_relative, :person_id => :integer,
425
+ :relative_id => :integer
426
+ assert_accepts @matcher, Person.new
427
+ end
428
+
429
+ should "reject an association with missing options" do
430
+ define_model :relatives
431
+ define_model :person do
432
+ has_and_belongs_to_many :relatives, :order => "surname"
433
+ end
434
+ define_model :people_relative, :person_id => :integer,
435
+ :relative_id => :integer
436
+ assert_rejects @matcher, Person.new, :message => /missing :limit option/
437
+ end
438
+
439
+ should "reject an association with additional options" do
440
+ define_model :relatives
441
+ define_model :person do
442
+ has_and_belongs_to_many :relatives, :order => "surname", :limit =>5,
443
+ :validate => true
444
+ end
445
+ define_model :people_relative, :person_id => :integer,
446
+ :relative_id => :integer
447
+ assert_rejects @matcher, Person.new, :message => /unexpected :validate option/
448
+ end
449
+
450
+ should "reject an association with incorrect option values" do
451
+ define_model :relatives
452
+ define_model :person do
453
+ has_and_belongs_to_many :relatives, :order => "surname", :limit =>2
454
+ end
455
+ define_model :people_relative, :person_id => :integer,
456
+ :relative_id => :integer
457
+ assert_rejects @matcher, Person.new,
458
+ :message => /expected :limit option to be 5, but got 2/
459
+ end
460
+ end
461
+ end
462
+ end
@@ -0,0 +1,81 @@
1
+ require 'test_helper'
2
+
3
+ class EnsureInclusionOfMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "an attribute which must be included in a range" do
6
+ setup do
7
+ @model = define_model(:example, :attr => :integer) do
8
+ validates_inclusion_of :attr, :in => 2..5
9
+ end.new
10
+ end
11
+
12
+ should "accept ensuring the correct range" do
13
+ assert_accepts ensure_inclusion_of(:attr).in_range(2..5), @model
14
+ end
15
+
16
+ should "reject ensuring a lower minimum value" do
17
+ assert_rejects ensure_inclusion_of(:attr).in_range(1..5), @model
18
+ end
19
+
20
+ should "reject ensuring a higher minimum value" do
21
+ assert_rejects ensure_inclusion_of(:attr).in_range(3..5), @model
22
+ end
23
+
24
+ should "reject ensuring a lower maximum value" do
25
+ assert_rejects ensure_inclusion_of(:attr).in_range(2..4), @model
26
+ end
27
+
28
+ should "reject ensuring a higher maximum value" do
29
+ assert_rejects ensure_inclusion_of(:attr).in_range(2..6), @model
30
+ end
31
+
32
+ should "not override the default message with a blank" do
33
+ assert_accepts ensure_inclusion_of(:attr).
34
+ in_range(2..5).
35
+ with_message(nil),
36
+ @model
37
+ end
38
+ end
39
+
40
+ context "an attribute with a custom ranged value validation" do
41
+ setup do
42
+ @model = define_model(:example, :attr => :string) do
43
+ validates_inclusion_of :attr, :in => 2..4, :message => 'not good'
44
+
45
+ end.new
46
+ end
47
+
48
+ should "accept ensuring the correct range" do
49
+ assert_accepts ensure_inclusion_of(:attr).
50
+ in_range(2..4).
51
+ with_message(/not good/),
52
+ @model
53
+ end
54
+ end
55
+
56
+ context "an attribute with custom range validations" do
57
+ setup do
58
+ define_model :example, :attr => :integer do
59
+ validate :custom_validation
60
+ def custom_validation
61
+ if attr < 2
62
+ errors.add(:attr, 'too low')
63
+ elsif attr > 5
64
+ errors.add(:attr, 'too high')
65
+ end
66
+ end
67
+ end
68
+ @model = Example.new
69
+ end
70
+
71
+ should "accept ensuring the correct range and messages" do
72
+ assert_accepts ensure_inclusion_of(:attr).
73
+ in_range(2..5).
74
+ with_low_message(/low/).
75
+ with_high_message(/high/),
76
+ @model
77
+ end
78
+
79
+ end
80
+
81
+ end