shoulda 2.11.3 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (262) hide show
  1. data/Gemfile +9 -0
  2. data/Gemfile.lock +108 -0
  3. data/README.md +95 -0
  4. data/Rakefile +6 -50
  5. data/lib/shoulda/version.rb +1 -1
  6. metadata +50 -269
  7. data/README.rdoc +0 -154
  8. data/bin/convert_to_should_syntax +0 -42
  9. data/lib/shoulda.rb +0 -9
  10. data/lib/shoulda/action_controller.rb +0 -34
  11. data/lib/shoulda/action_controller/macros.rb +0 -221
  12. data/lib/shoulda/action_controller/matchers.rb +0 -39
  13. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +0 -112
  14. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +0 -74
  15. data/lib/shoulda/action_controller/matchers/redirect_to_matcher.rb +0 -62
  16. data/lib/shoulda/action_controller/matchers/render_template_matcher.rb +0 -54
  17. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +0 -99
  18. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +0 -74
  19. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +0 -85
  20. data/lib/shoulda/action_controller/matchers/route_matcher.rb +0 -93
  21. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +0 -98
  22. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +0 -100
  23. data/lib/shoulda/action_mailer.rb +0 -13
  24. data/lib/shoulda/action_mailer/assertions.rb +0 -42
  25. data/lib/shoulda/action_mailer/matchers.rb +0 -22
  26. data/lib/shoulda/action_mailer/matchers/have_sent_email.rb +0 -110
  27. data/lib/shoulda/active_record.rb +0 -16
  28. data/lib/shoulda/active_record/assertions.rb +0 -69
  29. data/lib/shoulda/active_record/helpers.rb +0 -32
  30. data/lib/shoulda/active_record/macros.rb +0 -457
  31. data/lib/shoulda/active_record/matchers.rb +0 -42
  32. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +0 -83
  33. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +0 -110
  34. data/lib/shoulda/active_record/matchers/association_matcher.rb +0 -226
  35. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +0 -87
  36. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +0 -141
  37. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +0 -169
  38. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +0 -112
  39. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +0 -59
  40. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +0 -41
  41. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +0 -65
  42. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +0 -39
  43. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +0 -60
  44. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +0 -148
  45. data/lib/shoulda/active_record/matchers/validation_matcher.rb +0 -56
  46. data/lib/shoulda/assertions.rb +0 -79
  47. data/lib/shoulda/autoload_macros.rb +0 -46
  48. data/lib/shoulda/context.rb +0 -433
  49. data/lib/shoulda/helpers.rb +0 -8
  50. data/lib/shoulda/integrations/rspec.rb +0 -13
  51. data/lib/shoulda/integrations/rspec2.rb +0 -22
  52. data/lib/shoulda/integrations/test_unit.rb +0 -22
  53. data/lib/shoulda/macros.rb +0 -161
  54. data/lib/shoulda/private_helpers.rb +0 -13
  55. data/lib/shoulda/proc_extensions.rb +0 -14
  56. data/lib/shoulda/rails.rb +0 -8
  57. data/lib/shoulda/tasks.rb +0 -3
  58. data/lib/shoulda/tasks/list_tests.rake +0 -29
  59. data/lib/shoulda/tasks/yaml_to_shoulda.rake +0 -28
  60. data/rails/init.rb +0 -8
  61. data/test/README +0 -36
  62. data/test/fail_macros.rb +0 -55
  63. data/test/fixtures/addresses.yml +0 -3
  64. data/test/fixtures/friendships.yml +0 -0
  65. data/test/fixtures/posts.yml +0 -5
  66. data/test/fixtures/products.yml +0 -0
  67. data/test/fixtures/taggings.yml +0 -0
  68. data/test/fixtures/tags.yml +0 -9
  69. data/test/fixtures/users.yml +0 -6
  70. data/test/functional/posts_controller_test.rb +0 -121
  71. data/test/functional/users_controller_test.rb +0 -19
  72. data/test/matchers/action_mailer/have_sent_email_test.rb +0 -70
  73. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +0 -74
  74. data/test/matchers/active_record/allow_value_matcher_test.rb +0 -64
  75. data/test/matchers/active_record/association_matcher_test.rb +0 -263
  76. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +0 -81
  77. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +0 -158
  78. data/test/matchers/active_record/have_db_column_matcher_test.rb +0 -169
  79. data/test/matchers/active_record/have_db_index_matcher_test.rb +0 -91
  80. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +0 -29
  81. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +0 -44
  82. data/test/matchers/active_record/validate_format_of_matcher_test.rb +0 -39
  83. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +0 -52
  84. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +0 -86
  85. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +0 -147
  86. data/test/matchers/controller/assign_to_matcher_test.rb +0 -45
  87. data/test/matchers/controller/filter_param_matcher_test.rb +0 -40
  88. data/test/matchers/controller/redirect_to_matcher_test.rb +0 -37
  89. data/test/matchers/controller/render_template_matcher_test.rb +0 -37
  90. data/test/matchers/controller/render_with_layout_matcher_test.rb +0 -47
  91. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +0 -32
  92. data/test/matchers/controller/respond_with_matcher_test.rb +0 -96
  93. data/test/matchers/controller/route_matcher_test.rb +0 -75
  94. data/test/matchers/controller/set_session_matcher_test.rb +0 -48
  95. data/test/matchers/controller/set_the_flash_matcher.rb +0 -95
  96. data/test/other/autoload_macro_test.rb +0 -18
  97. data/test/other/context_test.rb +0 -372
  98. data/test/other/convert_to_should_syntax_test.rb +0 -63
  99. data/test/other/helpers_test.rb +0 -317
  100. data/test/other/private_helpers_test.rb +0 -32
  101. data/test/other/should_test.rb +0 -271
  102. data/test/rails2_model_builder.rb +0 -130
  103. data/test/rails2_root/app/controllers/application_controller.rb +0 -22
  104. data/test/rails2_root/app/controllers/posts_controller.rb +0 -87
  105. data/test/rails2_root/app/controllers/users_controller.rb +0 -84
  106. data/test/rails2_root/app/helpers/application_helper.rb +0 -3
  107. data/test/rails2_root/app/helpers/posts_helper.rb +0 -2
  108. data/test/rails2_root/app/helpers/users_helper.rb +0 -2
  109. data/test/rails2_root/app/models/address.rb +0 -7
  110. data/test/rails2_root/app/models/flea.rb +0 -11
  111. data/test/rails2_root/app/models/friendship.rb +0 -4
  112. data/test/rails2_root/app/models/notifier.rb +0 -8
  113. data/test/rails2_root/app/models/pets/cat.rb +0 -7
  114. data/test/rails2_root/app/models/pets/dog.rb +0 -10
  115. data/test/rails2_root/app/models/post.rb +0 -12
  116. data/test/rails2_root/app/models/product.rb +0 -12
  117. data/test/rails2_root/app/models/profile.rb +0 -2
  118. data/test/rails2_root/app/models/registration.rb +0 -2
  119. data/test/rails2_root/app/models/tag.rb +0 -8
  120. data/test/rails2_root/app/models/tagging.rb +0 -4
  121. data/test/rails2_root/app/models/treat.rb +0 -3
  122. data/test/rails2_root/app/models/user.rb +0 -32
  123. data/test/rails2_root/app/views/layouts/posts.rhtml +0 -19
  124. data/test/rails2_root/app/views/layouts/users.rhtml +0 -17
  125. data/test/rails2_root/app/views/layouts/wide.html.erb +0 -1
  126. data/test/rails2_root/app/views/notifier/the_email.html.erb +0 -1
  127. data/test/rails2_root/app/views/posts/edit.rhtml +0 -27
  128. data/test/rails2_root/app/views/posts/index.rhtml +0 -25
  129. data/test/rails2_root/app/views/posts/new.rhtml +0 -26
  130. data/test/rails2_root/app/views/posts/show.rhtml +0 -18
  131. data/test/rails2_root/app/views/users/edit.rhtml +0 -22
  132. data/test/rails2_root/app/views/users/index.rhtml +0 -22
  133. data/test/rails2_root/app/views/users/new.rhtml +0 -21
  134. data/test/rails2_root/app/views/users/show.rhtml +0 -13
  135. data/test/rails2_root/config/boot.rb +0 -110
  136. data/test/rails2_root/config/database.yml +0 -4
  137. data/test/rails2_root/config/environment.rb +0 -17
  138. data/test/rails2_root/config/environments/test.rb +0 -23
  139. data/test/rails2_root/config/initializers/new_rails_defaults.rb +0 -15
  140. data/test/rails2_root/config/initializers/shoulda.rb +0 -8
  141. data/test/rails2_root/config/routes.rb +0 -6
  142. data/test/rails2_root/db/migrate/001_create_users.rb +0 -19
  143. data/test/rails2_root/db/migrate/002_create_posts.rb +0 -13
  144. data/test/rails2_root/db/migrate/003_create_taggings.rb +0 -12
  145. data/test/rails2_root/db/migrate/004_create_tags.rb +0 -11
  146. data/test/rails2_root/db/migrate/005_create_dogs.rb +0 -12
  147. data/test/rails2_root/db/migrate/006_create_addresses.rb +0 -14
  148. data/test/rails2_root/db/migrate/007_create_fleas.rb +0 -11
  149. data/test/rails2_root/db/migrate/008_create_dogs_fleas.rb +0 -12
  150. data/test/rails2_root/db/migrate/009_create_products.rb +0 -17
  151. data/test/rails2_root/db/migrate/010_create_friendships.rb +0 -14
  152. data/test/rails2_root/db/migrate/011_create_treats.rb +0 -12
  153. data/test/rails2_root/db/migrate/20090506203502_create_profiles.rb +0 -12
  154. data/test/rails2_root/db/migrate/20090506203536_create_registrations.rb +0 -14
  155. data/test/rails2_root/db/migrate/20090513104502_create_cats.rb +0 -12
  156. data/test/rails2_root/db/schema.rb +0 -0
  157. data/test/rails2_root/log/test.log +0 -50381
  158. data/test/rails2_root/public/404.html +0 -30
  159. data/test/rails2_root/public/422.html +0 -30
  160. data/test/rails2_root/public/500.html +0 -30
  161. data/test/rails2_root/script/console +0 -3
  162. data/test/rails2_root/script/generate +0 -3
  163. data/test/rails2_root/test/shoulda_macros/custom_macro.rb +0 -6
  164. data/test/rails2_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +0 -6
  165. data/test/rails2_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +0 -6
  166. data/test/rails2_test_helper.rb +0 -6
  167. data/test/rails3_model_builder.rb +0 -118
  168. data/test/rails3_root/Gemfile +0 -28
  169. data/test/rails3_root/README +0 -244
  170. data/test/rails3_root/Rakefile +0 -10
  171. data/test/rails3_root/app/controllers/application_controller.rb +0 -22
  172. data/test/rails3_root/app/controllers/posts_controller.rb +0 -87
  173. data/test/rails3_root/app/controllers/users_controller.rb +0 -82
  174. data/test/rails3_root/app/helpers/application_helper.rb +0 -2
  175. data/test/rails3_root/app/models/address.rb +0 -7
  176. data/test/rails3_root/app/models/flea.rb +0 -11
  177. data/test/rails3_root/app/models/friendship.rb +0 -4
  178. data/test/rails3_root/app/models/notifier.rb +0 -8
  179. data/test/rails3_root/app/models/pets/cat.rb +0 -7
  180. data/test/rails3_root/app/models/pets/dog.rb +0 -10
  181. data/test/rails3_root/app/models/post.rb +0 -12
  182. data/test/rails3_root/app/models/product.rb +0 -12
  183. data/test/rails3_root/app/models/profile.rb +0 -2
  184. data/test/rails3_root/app/models/registration.rb +0 -2
  185. data/test/rails3_root/app/models/tag.rb +0 -8
  186. data/test/rails3_root/app/models/tagging.rb +0 -4
  187. data/test/rails3_root/app/models/treat.rb +0 -3
  188. data/test/rails3_root/app/models/user.rb +0 -32
  189. data/test/rails3_root/app/views/layouts/application.html.erb +0 -14
  190. data/test/rails3_root/app/views/layouts/posts.rhtml +0 -19
  191. data/test/rails3_root/app/views/layouts/users.rhtml +0 -17
  192. data/test/rails3_root/app/views/layouts/wide.html.erb +0 -1
  193. data/test/rails3_root/app/views/notifier/the_email.html.erb +0 -1
  194. data/test/rails3_root/app/views/posts/edit.rhtml +0 -27
  195. data/test/rails3_root/app/views/posts/index.rhtml +0 -25
  196. data/test/rails3_root/app/views/posts/new.rhtml +0 -24
  197. data/test/rails3_root/app/views/posts/show.rhtml +0 -18
  198. data/test/rails3_root/app/views/users/edit.rhtml +0 -22
  199. data/test/rails3_root/app/views/users/index.rhtml +0 -22
  200. data/test/rails3_root/app/views/users/new.rhtml +0 -21
  201. data/test/rails3_root/app/views/users/show.rhtml +0 -13
  202. data/test/rails3_root/config.ru +0 -4
  203. data/test/rails3_root/config/application.rb +0 -46
  204. data/test/rails3_root/config/boot.rb +0 -6
  205. data/test/rails3_root/config/database.yml +0 -22
  206. data/test/rails3_root/config/environment.rb +0 -5
  207. data/test/rails3_root/config/environments/development.rb +0 -19
  208. data/test/rails3_root/config/environments/production.rb +0 -42
  209. data/test/rails3_root/config/environments/test.rb +0 -32
  210. data/test/rails3_root/config/initializers/backtrace_silencers.rb +0 -7
  211. data/test/rails3_root/config/initializers/inflections.rb +0 -10
  212. data/test/rails3_root/config/initializers/mime_types.rb +0 -5
  213. data/test/rails3_root/config/initializers/secret_token.rb +0 -7
  214. data/test/rails3_root/config/initializers/session_store.rb +0 -8
  215. data/test/rails3_root/config/locales/en.yml +0 -5
  216. data/test/rails3_root/config/routes.rb +0 -4
  217. data/test/rails3_root/db/migrate/001_create_users.rb +0 -19
  218. data/test/rails3_root/db/migrate/002_create_posts.rb +0 -13
  219. data/test/rails3_root/db/migrate/003_create_taggings.rb +0 -12
  220. data/test/rails3_root/db/migrate/004_create_tags.rb +0 -11
  221. data/test/rails3_root/db/migrate/005_create_dogs.rb +0 -12
  222. data/test/rails3_root/db/migrate/006_create_addresses.rb +0 -14
  223. data/test/rails3_root/db/migrate/007_create_fleas.rb +0 -11
  224. data/test/rails3_root/db/migrate/008_create_dogs_fleas.rb +0 -12
  225. data/test/rails3_root/db/migrate/009_create_products.rb +0 -17
  226. data/test/rails3_root/db/migrate/010_create_friendships.rb +0 -14
  227. data/test/rails3_root/db/migrate/011_create_treats.rb +0 -12
  228. data/test/rails3_root/db/migrate/20090506203502_create_profiles.rb +0 -12
  229. data/test/rails3_root/db/migrate/20090506203536_create_registrations.rb +0 -14
  230. data/test/rails3_root/db/migrate/20090513104502_create_cats.rb +0 -12
  231. data/test/rails3_root/db/seeds.rb +0 -7
  232. data/test/rails3_root/db/test.sqlite3 +0 -0
  233. data/test/rails3_root/log/test.log +0 -10834
  234. data/test/rails3_root/public/404.html +0 -26
  235. data/test/rails3_root/public/422.html +0 -26
  236. data/test/rails3_root/public/500.html +0 -26
  237. data/test/rails3_root/public/favicon.ico +0 -0
  238. data/test/rails3_root/public/images/rails.png +0 -0
  239. data/test/rails3_root/public/index.html +0 -279
  240. data/test/rails3_root/public/javascripts/application.js +0 -2
  241. data/test/rails3_root/public/javascripts/controls.js +0 -965
  242. data/test/rails3_root/public/javascripts/dragdrop.js +0 -974
  243. data/test/rails3_root/public/javascripts/effects.js +0 -1123
  244. data/test/rails3_root/public/javascripts/prototype.js +0 -4874
  245. data/test/rails3_root/public/javascripts/rails.js +0 -118
  246. data/test/rails3_root/public/robots.txt +0 -5
  247. data/test/rails3_root/script/rails +0 -9
  248. data/test/rails3_root/test/performance/browsing_test.rb +0 -9
  249. data/test/rails3_root/test/test_helper.rb +0 -13
  250. data/test/rails3_test_helper.rb +0 -6
  251. data/test/rspec_test.rb +0 -207
  252. data/test/test_helper.rb +0 -36
  253. data/test/unit/address_test.rb +0 -10
  254. data/test/unit/cat_test.rb +0 -7
  255. data/test/unit/dog_test.rb +0 -9
  256. data/test/unit/flea_test.rb +0 -14
  257. data/test/unit/friendship_test.rb +0 -6
  258. data/test/unit/post_test.rb +0 -15
  259. data/test/unit/product_test.rb +0 -23
  260. data/test/unit/tag_test.rb +0 -11
  261. data/test/unit/tagging_test.rb +0 -6
  262. data/test/unit/user_test.rb +0 -46
@@ -1,110 +0,0 @@
1
- module Shoulda # :nodoc:
2
- module ActionMailer # :nodoc:
3
- module Matchers
4
-
5
- # The right email is sent.
6
- #
7
- # it { should have_sent_email.with_subject(/is spam$/) }
8
- # it { should have_sent_email.from('do-not-reply@example.com') }
9
- # it { should have_sent_email.with_body(/is spam\./) }
10
- # it { should have_sent_email.to('myself@me.com') }
11
- # it { should have_sent_email.with_subject(/spam/).
12
- # from('do-not-reply@example.com').
13
- # with_body(/spam/).
14
- # to('myself@me.com') }
15
- def have_sent_email
16
- HaveSentEmailMatcher.new
17
- end
18
-
19
- class HaveSentEmailMatcher # :nodoc:
20
-
21
- def initialize
22
- end
23
-
24
- def with_subject(email_subject)
25
- @email_subject = email_subject
26
- self
27
- end
28
-
29
- def from(sender)
30
- @sender = sender
31
- self
32
- end
33
-
34
- def with_body(body)
35
- @body = body
36
- self
37
- end
38
-
39
- def to(recipient)
40
- @recipient = recipient
41
- self
42
- end
43
-
44
- def matches?(subject)
45
- ::ActionMailer::Base.deliveries.each do |mail|
46
- @subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject
47
- @body_failed = !regexp_or_string_match(mail.body, @body) if @body
48
- @sender_failed = !regexp_or_string_match_in_array(mail.from, @sender) if @sender
49
- @recipient_failed = !regexp_or_string_match_in_array(mail.to, @recipient) if @recipient
50
- return true unless anything_failed?
51
- end
52
-
53
- false
54
- end
55
-
56
- def failure_message
57
- "Expected #{expectation}"
58
- end
59
-
60
- def negative_failure_message
61
- "Did not expect #{expectation}"
62
- end
63
-
64
- def description
65
- "send an email"
66
- end
67
-
68
- private
69
-
70
- def expectation
71
- expectation = "sent email"
72
- expectation << " with subject #{@email_subject.inspect}" if @subject_failed
73
- expectation << " with body #{@body.inspect}" if @body_failed
74
- expectation << " from #{@sender.inspect}" if @sender_failed
75
- expectation << " to #{@recipient.inspect}" if @recipient_failed
76
- expectation << "\nDeliveries:\n#{inspect_deliveries}"
77
- end
78
-
79
- def inspect_deliveries
80
- ::ActionMailer::Base.deliveries.map do |delivery|
81
- "#{delivery.subject.inspect} to #{delivery.to.inspect}"
82
- end.join("\n")
83
- end
84
-
85
- def anything_failed?
86
- @subject_failed || @body_failed || @sender_failed || @recipient_failed
87
- end
88
-
89
- def regexp_or_string_match(a_string, a_regexp_or_string)
90
- case a_regexp_or_string
91
- when Regexp
92
- a_string =~ a_regexp_or_string
93
- when String
94
- a_string == a_regexp_or_string
95
- end
96
- end
97
-
98
- def regexp_or_string_match_in_array(an_array, a_regexp_or_string)
99
- case a_regexp_or_string
100
- when Regexp
101
- an_array.detect{|e| e =~ a_regexp_or_string}.any?
102
- when String
103
- an_array.include?(a_regexp_or_string)
104
- end
105
- end
106
- end
107
- end
108
- end
109
- end
110
-
@@ -1,16 +0,0 @@
1
- require 'shoulda'
2
- require 'shoulda/active_record/helpers'
3
- require 'shoulda/active_record/matchers'
4
- require 'shoulda/active_record/assertions'
5
- require 'shoulda/active_record/macros'
6
-
7
- module Test # :nodoc: all
8
- module Unit
9
- class TestCase
10
- include Shoulda::ActiveRecord::Helpers
11
- include Shoulda::ActiveRecord::Matchers
12
- include Shoulda::ActiveRecord::Assertions
13
- extend Shoulda::ActiveRecord::Macros
14
- end
15
- end
16
- end
@@ -1,69 +0,0 @@
1
- module Shoulda # :nodoc:
2
- module ActiveRecord # :nodoc:
3
- module Assertions
4
- # Asserts that the given object can be saved
5
- #
6
- # assert_save User.new(params)
7
- def assert_save(obj)
8
- assert obj.save, "Errors: #{pretty_error_messages obj}"
9
- obj.reload
10
- end
11
-
12
- # Asserts that the given object is valid
13
- #
14
- # assert_valid User.new(params)
15
- def assert_valid(obj)
16
- assert obj.valid?, "Errors: #{pretty_error_messages obj}"
17
- end
18
-
19
- # Asserts that an Active Record model validates with the passed
20
- # <tt>value</tt> by making sure the <tt>error_message_to_avoid</tt> is not
21
- # contained within the list of errors for that attribute.
22
- #
23
- # assert_good_value(User.new, :email, "user@example.com")
24
- # assert_good_value(User.new, :ssn, "123456789", /length/)
25
- #
26
- # If a class is passed as the first argument, a new object will be
27
- # instantiated before the assertion. If an instance variable exists with
28
- # the same name as the class (underscored), that object will be used
29
- # instead.
30
- #
31
- # assert_good_value(User, :email, "user@example.com")
32
- #
33
- # product = Product.new(:tangible => false)
34
- # assert_good_value(product, :price, "0")
35
- def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil)
36
- object = get_instance_of(object_or_klass)
37
- matcher = allow_value(value).
38
- for(attribute).
39
- with_message(error_message_to_avoid)
40
- assert_accepts(matcher, object)
41
- end
42
-
43
- # Asserts that an Active Record model invalidates the passed
44
- # <tt>value</tt> by making sure the <tt>error_message_to_expect</tt> is
45
- # contained within the list of errors for that attribute.
46
- #
47
- # assert_bad_value(User.new, :email, "invalid")
48
- # assert_bad_value(User.new, :ssn, "123", /length/)
49
- #
50
- # If a class is passed as the first argument, a new object will be
51
- # instantiated before the assertion. If an instance variable exists with
52
- # the same name as the class (underscored), that object will be used
53
- # instead.
54
- #
55
- # assert_bad_value(User, :email, "invalid")
56
- #
57
- # product = Product.new(:tangible => true)
58
- # assert_bad_value(product, :price, "0")
59
- def assert_bad_value(object_or_klass, attribute, value,
60
- error_message_to_expect = nil)
61
- object = get_instance_of(object_or_klass)
62
- matcher = allow_value(value).
63
- for(attribute).
64
- with_message(error_message_to_expect)
65
- assert_rejects(matcher, object)
66
- end
67
- end
68
- end
69
- end
@@ -1,32 +0,0 @@
1
- module Shoulda # :nodoc:
2
- module ActiveRecord # :nodoc:
3
- module Helpers
4
- def pretty_error_messages(obj) # :nodoc:
5
- obj.errors.map do |a, m|
6
- msg = "#{a} #{m}"
7
- msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
8
- end
9
- end
10
-
11
- # Helper method that determines the default error message used by Active
12
- # Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
13
- # introduced I18n module used for localization.
14
- #
15
- # default_error_message(:blank)
16
- # default_error_message(:too_short, :count => 5)
17
- # default_error_message(:too_long, :count => 60)
18
- def default_error_message(key, values = {})
19
- if Object.const_defined?(:I18n) # Rails >= 2.2
20
- result = I18n.translate("activerecord.errors.messages.#{key}", values)
21
- if result =~ /^translation missing/
22
- I18n.translate("errors.messages.#{key}", values)
23
- else
24
- result
25
- end
26
- else # Rails <= 2.1.x
27
- ::ActiveRecord::Errors.default_error_messages[key] % values[:count]
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,457 +0,0 @@
1
- module Shoulda # :nodoc:
2
- module ActiveRecord # :nodoc:
3
- # = Macro test helpers for your active record models
4
- #
5
- # These helpers will test most of the validations and associations for your ActiveRecord models.
6
- #
7
- # class UserTest < Test::Unit::TestCase
8
- # should_validate_presence_of :name, :phone_number
9
- # should_not_allow_values_for :phone_number, "abcd", "1234"
10
- # should_allow_values_for :phone_number, "(123) 456-7890"
11
- #
12
- # should_not_allow_mass_assignment_of :password
13
- #
14
- # should_have_one :profile
15
- # should_have_many :dogs
16
- # should_have_many :messes, :through => :dogs
17
- # should_belong_to :lover
18
- # end
19
- #
20
- # For all of these helpers, the last parameter may be a hash of options.
21
- #
22
- module Macros
23
- include Helpers
24
- include Matchers
25
-
26
- # Deprecated: use ActiveRecord::Matchers#validate_presence_of instead.
27
- #
28
- # Ensures that the model cannot be saved if one of the attributes listed is not present.
29
- #
30
- # Options:
31
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
32
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.blank')</tt>
33
- #
34
- # Example:
35
- # should_validate_presence_of :name, :phone_number
36
- #
37
- def should_validate_presence_of(*attributes)
38
- ::ActiveSupport::Deprecation.warn("use: should validate_presence_of")
39
- message = get_options!(attributes, :message)
40
-
41
- attributes.each do |attribute|
42
- should validate_presence_of(attribute).with_message(message)
43
- end
44
- end
45
-
46
- # Deprecated: use ActiveRecord::Matchers#validate_uniqueness_of instead.
47
- #
48
- # Ensures that the model cannot be saved if one of the attributes listed is not unique.
49
- # Requires an existing record
50
- #
51
- # Options:
52
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
53
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.taken')</tt>
54
- # * <tt>:scoped_to</tt> - field(s) to scope the uniqueness to.
55
- # * <tt>:case_sensitive</tt> - whether or not uniqueness is defined by an
56
- # exact match. Ignored by non-text attributes. Default = <tt>true</tt>
57
- #
58
- # Examples:
59
- # should_validate_uniqueness_of :keyword, :username
60
- # should_validate_uniqueness_of :name, :message => "O NOES! SOMEONE STOELED YER NAME!"
61
- # should_validate_uniqueness_of :email, :scoped_to => :name
62
- # should_validate_uniqueness_of :address, :scoped_to => [:first_name, :last_name]
63
- # should_validate_uniqueness_of :email, :case_sensitive => false
64
- #
65
- def should_validate_uniqueness_of(*attributes)
66
- ::ActiveSupport::Deprecation.warn("use: should validate_uniqueness_of")
67
- message, scope, case_sensitive = get_options!(attributes, :message, :scoped_to, :case_sensitive)
68
- scope = [*scope].compact
69
- case_sensitive = true if case_sensitive.nil?
70
-
71
- attributes.each do |attribute|
72
- matcher = validate_uniqueness_of(attribute).
73
- with_message(message).scoped_to(scope)
74
- matcher = matcher.case_insensitive unless case_sensitive
75
- should matcher
76
- end
77
- end
78
-
79
- # Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
80
- #
81
- # Ensures that the attribute can be set on mass update.
82
- #
83
- # should_allow_mass_assignment_of :first_name, :last_name
84
- #
85
- def should_allow_mass_assignment_of(*attributes)
86
- ::ActiveSupport::Deprecation.warn("use: should allow_mass_assignment_of")
87
- get_options!(attributes)
88
-
89
- attributes.each do |attribute|
90
- should allow_mass_assignment_of(attribute)
91
- end
92
- end
93
-
94
- # Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
95
- #
96
- # Ensures that the attribute cannot be set on mass update.
97
- #
98
- # should_not_allow_mass_assignment_of :password, :admin_flag
99
- #
100
- def should_not_allow_mass_assignment_of(*attributes)
101
- ::ActiveSupport::Deprecation.warn("use: should_not allow_mass_assignment_of")
102
- get_options!(attributes)
103
-
104
- attributes.each do |attribute|
105
- should_not allow_mass_assignment_of(attribute)
106
- end
107
- end
108
-
109
- # Deprecated: use ActiveRecord::Matchers#have_readonly_attribute instead.
110
- #
111
- # Ensures that the attribute cannot be changed once the record has been created.
112
- #
113
- # should_have_readonly_attributes :password, :admin_flag
114
- #
115
- def should_have_readonly_attributes(*attributes)
116
- ::ActiveSupport::Deprecation.warn("use: should have_readonly_attribute")
117
- get_options!(attributes)
118
-
119
- attributes.each do |attribute|
120
- should have_readonly_attribute(attribute)
121
- end
122
- end
123
-
124
- # Deprecated: use ActiveRecord::Matchers#allow_value instead.
125
- #
126
- # Ensures that the attribute cannot be set to the given values
127
- #
128
- # Options:
129
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
130
- # Regexp or string. If omitted, the test will pass if there is ANY error in
131
- # <tt>errors.on(:attribute)</tt>.
132
- #
133
- # Example:
134
- # should_not_allow_values_for :isbn, "bad 1", "bad 2"
135
- #
136
- def should_not_allow_values_for(attribute, *bad_values)
137
- ::ActiveSupport::Deprecation.warn("use: should_not allow_value")
138
- message = get_options!(bad_values, :message)
139
- bad_values.each do |value|
140
- should_not allow_value(value).for(attribute).with_message(message)
141
- end
142
- end
143
-
144
- # Deprecated: use ActiveRecord::Matchers#allow_value instead.
145
- #
146
- # Ensures that the attribute can be set to the given values.
147
- #
148
- # Example:
149
- # should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
150
- #
151
- def should_allow_values_for(attribute, *good_values)
152
- ::ActiveSupport::Deprecation.warn("use: should allow_value")
153
- get_options!(good_values)
154
- good_values.each do |value|
155
- should allow_value(value).for(attribute)
156
- end
157
- end
158
-
159
- # Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
160
- #
161
- # Ensures that the length of the attribute is in the given range
162
- #
163
- # Options:
164
- # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
165
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % range.first</tt>
166
- # * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
167
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_long') % range.last</tt>
168
- #
169
- # Example:
170
- # should_ensure_length_in_range :password, (6..20)
171
- #
172
- def should_ensure_length_in_range(attribute, range, opts = {})
173
- ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least.is_at_most")
174
- short_message, long_message = get_options!([opts],
175
- :short_message,
176
- :long_message)
177
- should ensure_length_of(attribute).
178
- is_at_least(range.first).
179
- with_short_message(short_message).
180
- is_at_most(range.last).
181
- with_long_message(long_message)
182
- end
183
-
184
- # Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
185
- #
186
- # Ensures that the length of the attribute is at least a certain length
187
- #
188
- # Options:
189
- # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
190
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % min_length</tt>
191
- #
192
- # Example:
193
- # should_ensure_length_at_least :name, 3
194
- #
195
- def should_ensure_length_at_least(attribute, min_length, opts = {})
196
- ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least")
197
- short_message = get_options!([opts], :short_message)
198
-
199
- should ensure_length_of(attribute).
200
- is_at_least(min_length).
201
- with_short_message(short_message)
202
- end
203
-
204
- # Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
205
- #
206
- # Ensures that the length of the attribute is exactly a certain length
207
- #
208
- # Options:
209
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
210
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % length</tt>
211
- #
212
- # Example:
213
- # should_ensure_length_is :ssn, 9
214
- #
215
- def should_ensure_length_is(attribute, length, opts = {})
216
- ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_equal_to")
217
- message = get_options!([opts], :message)
218
- should ensure_length_of(attribute).
219
- is_equal_to(length).
220
- with_message(message)
221
- end
222
-
223
- # Deprecated: use ActiveRecord::Matchers#ensure_inclusion_of instead.
224
- #
225
- # Ensure that the attribute is in the range specified
226
- #
227
- # Options:
228
- # * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
229
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
230
- # * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
231
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
232
- #
233
- # Example:
234
- # should_ensure_value_in_range :age, (0..100)
235
- #
236
- def should_ensure_value_in_range(attribute, range, opts = {})
237
- ::ActiveSupport::Deprecation.warn("use: should ensure_inclusion_of.in_range")
238
- message, low_message, high_message = get_options!([opts],
239
- :message,
240
- :low_message,
241
- :high_message)
242
- should ensure_inclusion_of(attribute).
243
- in_range(range).
244
- with_message(message).
245
- with_low_message(low_message).
246
- with_high_message(high_message)
247
- end
248
-
249
- # Deprecated: use ActiveRecord::Matchers#validate_numericality_of instead.
250
- #
251
- # Ensure that the attribute is numeric
252
- #
253
- # Options:
254
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
255
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.not_a_number')</tt>
256
- #
257
- # Example:
258
- # should_validate_numericality_of :age
259
- #
260
- def should_validate_numericality_of(*attributes)
261
- ::ActiveSupport::Deprecation.warn("use: should validate_numericality_of")
262
- message = get_options!(attributes, :message)
263
- attributes.each do |attribute|
264
- should validate_numericality_of(attribute).
265
- with_message(message)
266
- end
267
- end
268
-
269
- # Deprecated: use ActiveRecord::Matchers#have_many instead.
270
- #
271
- # Ensures that the has_many relationship exists. Will also test that the
272
- # associated table has the required columns. Works with polymorphic
273
- # associations.
274
- #
275
- # Options:
276
- # * <tt>:through</tt> - association name for <tt>has_many :through</tt>
277
- # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
278
- #
279
- # Example:
280
- # should_have_many :friends
281
- # should_have_many :enemies, :through => :friends
282
- # should_have_many :enemies, :dependent => :destroy
283
- #
284
- def should_have_many(*associations)
285
- ::ActiveSupport::Deprecation.warn("use: should have_many")
286
- through, dependent = get_options!(associations, :through, :dependent)
287
- associations.each do |association|
288
- should have_many(association).through(through).dependent(dependent)
289
- end
290
- end
291
-
292
- # Deprecated: use ActiveRecord::Matchers#have_one instead.
293
- #
294
- # Ensure that the has_one relationship exists. Will also test that the
295
- # associated table has the required columns. Works with polymorphic
296
- # associations.
297
- #
298
- # Options:
299
- # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
300
- #
301
- # Example:
302
- # should_have_one :god # unless hindu
303
- #
304
- def should_have_one(*associations)
305
- ::ActiveSupport::Deprecation.warn("use: should have_one")
306
- dependent, through = get_options!(associations, :dependent, :through)
307
- associations.each do |association|
308
- should have_one(association).dependent(dependent).through(through)
309
- end
310
- end
311
-
312
- # Deprecated: use ActiveRecord::Matchers#have_and_belong_to_many instead.
313
- #
314
- # Ensures that the has_and_belongs_to_many relationship exists, and that the join
315
- # table is in place.
316
- #
317
- # should_have_and_belong_to_many :posts, :cars
318
- #
319
- def should_have_and_belong_to_many(*associations)
320
- ::ActiveSupport::Deprecation.warn("use: should have_and_belong_to_many")
321
- get_options!(associations)
322
-
323
- associations.each do |association|
324
- should have_and_belong_to_many(association)
325
- end
326
- end
327
-
328
- # Deprecated: use ActiveRecord::Matchers#belong_to instead.
329
- #
330
- # Ensure that the belongs_to relationship exists.
331
- #
332
- # should_belong_to :parent
333
- #
334
- def should_belong_to(*associations)
335
- ::ActiveSupport::Deprecation.warn("use: should belong_to")
336
- dependent = get_options!(associations, :dependent)
337
- associations.each do |association|
338
- should belong_to(association).dependent(dependent)
339
- end
340
- end
341
-
342
- # Deprecated.
343
- #
344
- # Ensure that the given class methods are defined on the model.
345
- #
346
- # should_have_class_methods :find, :destroy
347
- #
348
- def should_have_class_methods(*methods)
349
- ::ActiveSupport::Deprecation.warn
350
- get_options!(methods)
351
- klass = described_type
352
- methods.each do |method|
353
- should "respond to class method ##{method}" do
354
- assert_respond_to klass, method, "#{klass.name} does not have class method #{method}"
355
- end
356
- end
357
- end
358
-
359
- # Deprecated.
360
- #
361
- # Ensure that the given instance methods are defined on the model.
362
- #
363
- # should_have_instance_methods :email, :name, :name=
364
- #
365
- def should_have_instance_methods(*methods)
366
- ::ActiveSupport::Deprecation.warn
367
- get_options!(methods)
368
- klass = described_type
369
- methods.each do |method|
370
- should "respond to instance method ##{method}" do
371
- assert_respond_to klass.new, method, "#{klass.name} does not have instance method #{method}"
372
- end
373
- end
374
- end
375
-
376
- # Deprecated: use ActiveRecord::Matchers#have_db_column instead.
377
- #
378
- # Ensure that the given columns are defined on the models backing SQL table.
379
- # Also aliased to should_have_db_column for readability.
380
- # Takes the same options available in migrations:
381
- # :type, :precision, :limit, :default, :null, and :scale
382
- #
383
- # Examples:
384
- #
385
- # should_have_db_columns :id, :email, :name, :created_at
386
- #
387
- # should_have_db_column :email, :type => "string", :limit => 255
388
- # should_have_db_column :salary, :decimal, :precision => 15, :scale => 2
389
- # should_have_db_column :admin, :default => false, :null => false
390
- #
391
- def should_have_db_columns(*columns)
392
- ::ActiveSupport::Deprecation.warn("use: should have_db_column")
393
- column_type, precision, limit, default, null, scale, sql_type =
394
- get_options!(columns, :type, :precision, :limit,
395
- :default, :null, :scale, :sql_type)
396
- columns.each do |name|
397
- should have_db_column(name).
398
- of_type(column_type).
399
- with_options(:precision => precision, :limit => limit,
400
- :default => default, :null => null,
401
- :scale => scale, :sql_type => sql_type)
402
- end
403
- end
404
-
405
- alias_method :should_have_db_column, :should_have_db_columns
406
-
407
- # Deprecated: use ActiveRecord::Matchers#have_db_index instead.
408
- #
409
- # Ensures that there are DB indices on the given columns or tuples of columns.
410
- # Also aliased to should_have_db_index for readability
411
- #
412
- # Options:
413
- # * <tt>:unique</tt> - whether or not the index has a unique
414
- # constraint. Use <tt>true</tt> to explicitly test for a unique
415
- # constraint. Use <tt>false</tt> to explicitly test for a non-unique
416
- # constraint. Use <tt>nil</tt> if you don't care whether the index is
417
- # unique or not. Default = <tt>nil</tt>
418
- #
419
- # Examples:
420
- #
421
- # should_have_db_indices :email, :name, [:commentable_type, :commentable_id]
422
- # should_have_db_index :age
423
- # should_have_db_index :ssn, :unique => true
424
- #
425
- def should_have_db_indices(*columns)
426
- ::ActiveSupport::Deprecation.warn("use: should have_db_index")
427
- unique = get_options!(columns, :unique)
428
-
429
- columns.each do |column|
430
- should have_db_index(column).unique(unique)
431
- end
432
- end
433
-
434
- alias_method :should_have_db_index, :should_have_db_indices
435
-
436
- # Deprecated: use ActiveRecord::Matchers#validate_acceptance_of instead.
437
- #
438
- # Ensures that the model cannot be saved if one of the attributes listed is not accepted.
439
- #
440
- # Options:
441
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
442
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.accepted')</tt>
443
- #
444
- # Example:
445
- # should_validate_acceptance_of :eula
446
- #
447
- def should_validate_acceptance_of(*attributes)
448
- ::ActiveSupport::Deprecation.warn("use: should validate_acceptance_of")
449
- message = get_options!(attributes, :message)
450
-
451
- attributes.each do |attribute|
452
- should validate_acceptance_of(attribute).with_message(message)
453
- end
454
- end
455
- end
456
- end
457
- end