faster_shoulda 2.11.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) 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 +226 -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 +537 -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/hello_should.rb +37 -0
  71. data/test/matchers/action_mailer/have_sent_email_test.rb +76 -0
  72. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +74 -0
  73. data/test/matchers/active_record/allow_value_matcher_test.rb +64 -0
  74. data/test/matchers/active_record/association_matcher_test.rb +263 -0
  75. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +81 -0
  76. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  77. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  78. data/test/matchers/active_record/have_db_index_matcher_test.rb +91 -0
  79. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  80. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  81. data/test/matchers/active_record/validate_format_of_matcher_test.rb +39 -0
  82. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  83. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  84. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  85. data/test/matchers/controller/assign_to_matcher_test.rb +55 -0
  86. data/test/matchers/controller/filter_param_matcher_test.rb +40 -0
  87. data/test/matchers/controller/redirect_to_matcher_test.rb +37 -0
  88. data/test/matchers/controller/render_template_matcher_test.rb +37 -0
  89. data/test/matchers/controller/render_with_layout_matcher_test.rb +47 -0
  90. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +32 -0
  91. data/test/matchers/controller/respond_with_matcher_test.rb +96 -0
  92. data/test/matchers/controller/route_matcher_test.rb +75 -0
  93. data/test/matchers/controller/set_session_matcher_test.rb +48 -0
  94. data/test/matchers/controller/set_the_flash_matcher.rb +95 -0
  95. data/test/other/autoload_macro_test.rb +18 -0
  96. data/test/other/context_test.rb +372 -0
  97. data/test/other/convert_to_should_syntax_test.rb +63 -0
  98. data/test/other/helpers_test.rb +317 -0
  99. data/test/other/private_helpers_test.rb +32 -0
  100. data/test/other/should_test.rb +271 -0
  101. data/test/rails2_model_builder.rb +130 -0
  102. data/test/rails2_root/app/controllers/application_controller.rb +22 -0
  103. data/test/rails2_root/app/controllers/posts_controller.rb +87 -0
  104. data/test/rails2_root/app/controllers/users_controller.rb +84 -0
  105. data/test/rails2_root/app/helpers/application_helper.rb +3 -0
  106. data/test/rails2_root/app/helpers/posts_helper.rb +2 -0
  107. data/test/rails2_root/app/helpers/users_helper.rb +2 -0
  108. data/test/rails2_root/app/models/address.rb +7 -0
  109. data/test/rails2_root/app/models/flea.rb +11 -0
  110. data/test/rails2_root/app/models/friendship.rb +4 -0
  111. data/test/rails2_root/app/models/notifier.rb +8 -0
  112. data/test/rails2_root/app/models/pets/cat.rb +7 -0
  113. data/test/rails2_root/app/models/pets/dog.rb +10 -0
  114. data/test/rails2_root/app/models/post.rb +12 -0
  115. data/test/rails2_root/app/models/product.rb +12 -0
  116. data/test/rails2_root/app/models/profile.rb +2 -0
  117. data/test/rails2_root/app/models/registration.rb +2 -0
  118. data/test/rails2_root/app/models/tag.rb +8 -0
  119. data/test/rails2_root/app/models/tagging.rb +4 -0
  120. data/test/rails2_root/app/models/treat.rb +3 -0
  121. data/test/rails2_root/app/models/user.rb +32 -0
  122. data/test/rails2_root/app/views/layouts/posts.rhtml +19 -0
  123. data/test/rails2_root/app/views/layouts/users.rhtml +17 -0
  124. data/test/rails2_root/app/views/layouts/wide.html.erb +1 -0
  125. data/test/rails2_root/app/views/notifier/the_email.html.erb +1 -0
  126. data/test/rails2_root/app/views/posts/edit.rhtml +27 -0
  127. data/test/rails2_root/app/views/posts/index.rhtml +25 -0
  128. data/test/rails2_root/app/views/posts/new.rhtml +26 -0
  129. data/test/rails2_root/app/views/posts/show.rhtml +18 -0
  130. data/test/rails2_root/app/views/users/edit.rhtml +22 -0
  131. data/test/rails2_root/app/views/users/index.rhtml +22 -0
  132. data/test/rails2_root/app/views/users/new.rhtml +21 -0
  133. data/test/rails2_root/app/views/users/show.rhtml +13 -0
  134. data/test/rails2_root/config/boot.rb +110 -0
  135. data/test/rails2_root/config/database.yml +4 -0
  136. data/test/rails2_root/config/environment.rb +17 -0
  137. data/test/rails2_root/config/environments/test.rb +23 -0
  138. data/test/rails2_root/config/initializers/new_rails_defaults.rb +15 -0
  139. data/test/rails2_root/config/initializers/shoulda.rb +8 -0
  140. data/test/rails2_root/config/routes.rb +6 -0
  141. data/test/rails2_root/db/migrate/001_create_users.rb +19 -0
  142. data/test/rails2_root/db/migrate/002_create_posts.rb +13 -0
  143. data/test/rails2_root/db/migrate/003_create_taggings.rb +12 -0
  144. data/test/rails2_root/db/migrate/004_create_tags.rb +11 -0
  145. data/test/rails2_root/db/migrate/005_create_dogs.rb +12 -0
  146. data/test/rails2_root/db/migrate/006_create_addresses.rb +14 -0
  147. data/test/rails2_root/db/migrate/007_create_fleas.rb +11 -0
  148. data/test/rails2_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  149. data/test/rails2_root/db/migrate/009_create_products.rb +17 -0
  150. data/test/rails2_root/db/migrate/010_create_friendships.rb +14 -0
  151. data/test/rails2_root/db/migrate/011_create_treats.rb +12 -0
  152. data/test/rails2_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  153. data/test/rails2_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  154. data/test/rails2_root/db/migrate/20090513104502_create_cats.rb +12 -0
  155. data/test/rails2_root/db/schema.rb +0 -0
  156. data/test/rails2_root/public/404.html +30 -0
  157. data/test/rails2_root/public/422.html +30 -0
  158. data/test/rails2_root/public/500.html +30 -0
  159. data/test/rails2_root/script/console +3 -0
  160. data/test/rails2_root/script/generate +3 -0
  161. data/test/rails2_root/test/shoulda_macros/custom_macro.rb +6 -0
  162. data/test/rails2_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  163. data/test/rails2_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  164. data/test/rails2_test_helper.rb +6 -0
  165. data/test/rails3_model_builder.rb +118 -0
  166. data/test/rails3_root/Gemfile +28 -0
  167. data/test/rails3_root/README +244 -0
  168. data/test/rails3_root/Rakefile +10 -0
  169. data/test/rails3_root/app/controllers/application_controller.rb +22 -0
  170. data/test/rails3_root/app/controllers/posts_controller.rb +87 -0
  171. data/test/rails3_root/app/controllers/users_controller.rb +82 -0
  172. data/test/rails3_root/app/helpers/application_helper.rb +2 -0
  173. data/test/rails3_root/app/models/address.rb +7 -0
  174. data/test/rails3_root/app/models/flea.rb +11 -0
  175. data/test/rails3_root/app/models/friendship.rb +4 -0
  176. data/test/rails3_root/app/models/notifier.rb +8 -0
  177. data/test/rails3_root/app/models/pets/cat.rb +7 -0
  178. data/test/rails3_root/app/models/pets/dog.rb +10 -0
  179. data/test/rails3_root/app/models/post.rb +12 -0
  180. data/test/rails3_root/app/models/product.rb +12 -0
  181. data/test/rails3_root/app/models/profile.rb +2 -0
  182. data/test/rails3_root/app/models/registration.rb +2 -0
  183. data/test/rails3_root/app/models/tag.rb +8 -0
  184. data/test/rails3_root/app/models/tagging.rb +4 -0
  185. data/test/rails3_root/app/models/treat.rb +3 -0
  186. data/test/rails3_root/app/models/user.rb +32 -0
  187. data/test/rails3_root/app/views/layouts/application.html.erb +14 -0
  188. data/test/rails3_root/app/views/layouts/posts.rhtml +19 -0
  189. data/test/rails3_root/app/views/layouts/users.rhtml +17 -0
  190. data/test/rails3_root/app/views/layouts/wide.html.erb +1 -0
  191. data/test/rails3_root/app/views/notifier/the_email.html.erb +1 -0
  192. data/test/rails3_root/app/views/posts/edit.rhtml +27 -0
  193. data/test/rails3_root/app/views/posts/index.rhtml +25 -0
  194. data/test/rails3_root/app/views/posts/new.rhtml +24 -0
  195. data/test/rails3_root/app/views/posts/show.rhtml +18 -0
  196. data/test/rails3_root/app/views/users/edit.rhtml +22 -0
  197. data/test/rails3_root/app/views/users/index.rhtml +22 -0
  198. data/test/rails3_root/app/views/users/new.rhtml +21 -0
  199. data/test/rails3_root/app/views/users/show.rhtml +13 -0
  200. data/test/rails3_root/config/application.rb +46 -0
  201. data/test/rails3_root/config/boot.rb +6 -0
  202. data/test/rails3_root/config/database.yml +22 -0
  203. data/test/rails3_root/config/environment.rb +5 -0
  204. data/test/rails3_root/config/environments/development.rb +19 -0
  205. data/test/rails3_root/config/environments/production.rb +42 -0
  206. data/test/rails3_root/config/environments/test.rb +32 -0
  207. data/test/rails3_root/config/initializers/backtrace_silencers.rb +7 -0
  208. data/test/rails3_root/config/initializers/inflections.rb +10 -0
  209. data/test/rails3_root/config/initializers/mime_types.rb +5 -0
  210. data/test/rails3_root/config/initializers/secret_token.rb +7 -0
  211. data/test/rails3_root/config/initializers/session_store.rb +8 -0
  212. data/test/rails3_root/config/locales/en.yml +5 -0
  213. data/test/rails3_root/config/routes.rb +4 -0
  214. data/test/rails3_root/config.ru +4 -0
  215. data/test/rails3_root/db/migrate/001_create_users.rb +19 -0
  216. data/test/rails3_root/db/migrate/002_create_posts.rb +13 -0
  217. data/test/rails3_root/db/migrate/003_create_taggings.rb +12 -0
  218. data/test/rails3_root/db/migrate/004_create_tags.rb +11 -0
  219. data/test/rails3_root/db/migrate/005_create_dogs.rb +12 -0
  220. data/test/rails3_root/db/migrate/006_create_addresses.rb +14 -0
  221. data/test/rails3_root/db/migrate/007_create_fleas.rb +11 -0
  222. data/test/rails3_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  223. data/test/rails3_root/db/migrate/009_create_products.rb +17 -0
  224. data/test/rails3_root/db/migrate/010_create_friendships.rb +14 -0
  225. data/test/rails3_root/db/migrate/011_create_treats.rb +12 -0
  226. data/test/rails3_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  227. data/test/rails3_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  228. data/test/rails3_root/db/migrate/20090513104502_create_cats.rb +12 -0
  229. data/test/rails3_root/db/seeds.rb +7 -0
  230. data/test/rails3_root/public/404.html +26 -0
  231. data/test/rails3_root/public/422.html +26 -0
  232. data/test/rails3_root/public/500.html +26 -0
  233. data/test/rails3_root/public/favicon.ico +0 -0
  234. data/test/rails3_root/public/images/rails.png +0 -0
  235. data/test/rails3_root/public/index.html +279 -0
  236. data/test/rails3_root/public/javascripts/application.js +2 -0
  237. data/test/rails3_root/public/javascripts/controls.js +965 -0
  238. data/test/rails3_root/public/javascripts/dragdrop.js +974 -0
  239. data/test/rails3_root/public/javascripts/effects.js +1123 -0
  240. data/test/rails3_root/public/javascripts/prototype.js +4874 -0
  241. data/test/rails3_root/public/javascripts/rails.js +118 -0
  242. data/test/rails3_root/public/robots.txt +5 -0
  243. data/test/rails3_root/script/rails +9 -0
  244. data/test/rails3_root/test/performance/browsing_test.rb +9 -0
  245. data/test/rails3_root/test/test_helper.rb +13 -0
  246. data/test/rails3_test_helper.rb +6 -0
  247. data/test/rspec_test.rb +207 -0
  248. data/test/test_helper.rb +36 -0
  249. data/test/unit/address_test.rb +10 -0
  250. data/test/unit/cat_test.rb +7 -0
  251. data/test/unit/dog_test.rb +9 -0
  252. data/test/unit/flea_test.rb +14 -0
  253. data/test/unit/friendship_test.rb +6 -0
  254. data/test/unit/post_test.rb +15 -0
  255. data/test/unit/product_test.rb +23 -0
  256. data/test/unit/tag_test.rb +11 -0
  257. data/test/unit/tagging_test.rb +6 -0
  258. data/test/unit/user_test.rb +46 -0
  259. metadata +325 -0
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+
3
+ require 'shoulda/context'
4
+ require 'shoulda/proc_extensions'
5
+ require 'shoulda/assertions'
6
+ require 'shoulda/macros'
7
+ require 'shoulda/helpers'
8
+ require 'shoulda/autoload_macros'
9
+ require 'shoulda/rails' if defined? RAILS_ROOT
10
+
11
+ module Test # :nodoc: all
12
+ module Unit
13
+ class TestCase
14
+ include Shoulda::InstanceMethods
15
+ extend Shoulda::ClassMethods
16
+ include Shoulda::Assertions
17
+ extend Shoulda::Macros
18
+ include Shoulda::Helpers
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,161 @@
1
+ require 'shoulda/private_helpers'
2
+
3
+ module Shoulda # :nodoc:
4
+ module Macros
5
+ # Deprecated.
6
+ #
7
+ # Macro that creates a test asserting a change between the return value
8
+ # of a block that is run before and after the current setup block
9
+ # is run. This is similar to Active Support's <tt>assert_difference</tt>
10
+ # assertion, but supports more than just numeric values. See also
11
+ # should_not_change.
12
+ #
13
+ # The passed description will be used when generating the test name and failure messages.
14
+ #
15
+ # Example:
16
+ #
17
+ # context "Creating a post" do
18
+ # setup { Post.create }
19
+ # should_change("the number of posts", :by => 1) { Post.count }
20
+ # end
21
+ #
22
+ # As shown in this example, the <tt>:by</tt> option expects a numeric
23
+ # difference between the before and after values of the expression. You
24
+ # may also specify <tt>:from</tt> and <tt>:to</tt> options:
25
+ #
26
+ # should_change("the number of posts", :from => 0, :to => 1) { Post.count }
27
+ # should_change("the post title", :from => "old", :to => "new") { @post.title }
28
+ #
29
+ # Combinations of <tt>:by</tt>, <tt>:from</tt>, and <tt>:to</tt> are allowed:
30
+ #
31
+ # # Assert the value changed in some way:
32
+ # should_change("the post title") { @post.title }
33
+ #
34
+ # # Assert the value changed to anything other than "old:"
35
+ # should_change("the post title", :from => "old") { @post.title }
36
+ #
37
+ # # Assert the value changed to "new:"
38
+ # should_change("the post title", :to => "new") { @post.title }
39
+ #
40
+ # This macro was deprecated because these tests aren't as valuable as
41
+ # alternative tests that explicitly test the final state.
42
+ #
43
+ # Consider an alternative:
44
+ #
45
+ # context "updating a post" do
46
+ # setup do
47
+ # @post = Post.create(:title => "old")
48
+ # put :update, :post => {:title => "new"}, :id => @post.to_param
49
+ # end
50
+ # should "update the title" do
51
+ # assert_equal "new", @post.reload.title
52
+ # end
53
+ # end
54
+ def should_change(description, options = {}, &block)
55
+ ::ActiveSupport::Deprecation.warn("Not considered a useful test. Instead, test the end state explicitly.")
56
+ by, from, to = get_options!([options], :by, :from, :to)
57
+ stmt = "change #{description}"
58
+ stmt << " from #{from.inspect}" if from
59
+ stmt << " to #{to.inspect}" if to
60
+ stmt << " by #{by.inspect}" if by
61
+
62
+ before = lambda { @_before_should_change = block.bind(self).call }
63
+ should stmt, :before => before do
64
+ old_value = @_before_should_change
65
+ new_value = block.bind(self).call
66
+ assert_operator from, :===, old_value, "#{description} did not originally match #{from.inspect}" if from
67
+ assert_not_equal old_value, new_value, "#{description} did not change" unless by == 0
68
+ assert_operator to, :===, new_value, "#{description} was not changed to match #{to.inspect}" if to
69
+ assert_equal old_value + by, new_value if by
70
+ end
71
+ end
72
+
73
+ # Deprecated.
74
+ #
75
+ # Macro that creates a test asserting no change between the return value
76
+ # of a block that is run before and after the current setup block
77
+ # is run. This is the logical opposite of should_change.
78
+ #
79
+ # The passed description will be used when generating the test name and failure message.
80
+ #
81
+ # Example:
82
+ #
83
+ # context "Updating a post" do
84
+ # setup { @post.update_attributes(:title => "new") }
85
+ # should_not_change("the number of posts") { Post.count }
86
+ # end
87
+ #
88
+ # This macro was deprecated because these tests aren't as valuable as
89
+ # alternative tests that explicitly test the final state.
90
+ #
91
+ # Consider an alternative:
92
+ #
93
+ # context "updating a post" do
94
+ # setup do
95
+ # @post = Post.create(:title => "old")
96
+ # put :update, :post => {:title => ""}, :id => @post.to_param
97
+ # end
98
+ # should "not update the title" do
99
+ # assert_equal "old", @post.reload.title
100
+ # end
101
+ # end
102
+ def should_not_change(description, &block)
103
+ ::ActiveSupport::Deprecation.warn("Not considered a useful test. Instead, test the end state explicitly.")
104
+ before = lambda { @_before_should_not_change = block.bind(self).call }
105
+ should "not change #{description}", :before => before do
106
+ new_value = block.bind(self).call
107
+ assert_equal @_before_should_not_change, new_value, "#{description} changed"
108
+ end
109
+ end
110
+
111
+ # Deprecated.
112
+ #
113
+ # Macro that creates a test asserting that a record of the given class was
114
+ # created.
115
+ #
116
+ # Example:
117
+ #
118
+ # context "creating a post" do
119
+ # setup { Post.create(post_attributes) }
120
+ # should_create :post
121
+ # end
122
+ def should_create(class_name)
123
+ ::ActiveSupport::Deprecation.warn
124
+ should_change_record_count_of(class_name, 1, 'create')
125
+ end
126
+
127
+ # Deprecated.
128
+ #
129
+ # Macro that creates a test asserting that a record of the given class was
130
+ # destroyed.
131
+ #
132
+ # Example:
133
+ #
134
+ # context "destroying a post" do
135
+ # setup { Post.first.destroy }
136
+ # should_destroy :post
137
+ # end
138
+ def should_destroy(class_name)
139
+ ::ActiveSupport::Deprecation.warn
140
+ should_change_record_count_of(class_name, -1, 'destroy')
141
+ end
142
+
143
+ private
144
+
145
+ def should_change_record_count_of(class_name, amount, action) # :nodoc:
146
+ klass = class_name.to_s.camelize.constantize
147
+ before = lambda do
148
+ @_before_change_record_count = klass.count
149
+ end
150
+ human_name = class_name.to_s.humanize.downcase
151
+ should "#{action} a #{human_name}", :before => before do
152
+ assert_equal @_before_change_record_count + amount,
153
+ klass.count,
154
+ "Expected to #{action} a #{human_name}"
155
+ end
156
+ end
157
+
158
+ include Shoulda::Private
159
+ end
160
+ end
161
+
@@ -0,0 +1,13 @@
1
+ module Shoulda # :nodoc:
2
+ module Private # :nodoc:
3
+ # Returns the values for the entries in the args hash who's keys are listed in the wanted array.
4
+ # Will raise if there are keys in the args hash that aren't listed.
5
+ def get_options!(args, *wanted)
6
+ ret = []
7
+ opts = (args.last.is_a?(Hash) ? args.pop : {})
8
+ wanted.each {|w| ret << opts.delete(w)}
9
+ raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
10
+ return wanted.size == 1 ? ret.first : ret
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # Stolen straight from ActiveSupport
2
+
3
+ class Proc #:nodoc:
4
+ def bind(object)
5
+ block, time = self, Time.now
6
+ (class << object; self end).class_eval do
7
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
8
+ define_method(method_name, &block)
9
+ method = instance_method(method_name)
10
+ remove_method(method_name)
11
+ method
12
+ end.bind(object)
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'shoulda'
4
+
5
+ require 'shoulda/active_record' if defined? ActiveRecord::Base
6
+ require 'shoulda/action_controller' if defined? ActionController::Base
7
+ require 'shoulda/action_mailer' if defined? ActionMailer::Base
8
+
@@ -0,0 +1,29 @@
1
+ namespace :shoulda do
2
+ desc "List the names of the test methods in a specification like format"
3
+ task :list do
4
+ $LOAD_PATH.unshift("test")
5
+
6
+ require 'test/unit'
7
+ require 'rubygems'
8
+ require 'active_support'
9
+
10
+ # bug in test unit. Set to true to stop from running.
11
+ Test::Unit.run = true
12
+
13
+ test_files = Dir.glob(File.join('test', '**', '*_test.rb'))
14
+ test_files.each do |file|
15
+ load file
16
+ klass = File.basename(file, '.rb').classify
17
+ unless Object.const_defined?(klass.to_s)
18
+ puts "Skipping #{klass} because it doesn't map to a Class"
19
+ next
20
+ end
21
+ klass = klass.constantize
22
+
23
+ puts klass.name.gsub('Test', '')
24
+
25
+ test_methods = klass.instance_methods.grep(/^test/).map {|s| s.gsub(/^test: /, '')}.sort
26
+ test_methods.each {|m| puts " " + m }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ namespace :shoulda do
2
+ # From http://blog.internautdesign.com/2007/11/2/a-yaml_to_shoulda-rake-task
3
+ # David.Lowenfels@gmail.com
4
+ desc "Converts a YAML file (FILE=./path/to/yaml) into a Shoulda skeleton"
5
+ task :from_yaml do
6
+ require 'yaml'
7
+
8
+ def yaml_to_context(hash, indent = 0)
9
+ indent1 = ' ' * indent
10
+ indent2 = ' ' * (indent + 1)
11
+ hash.each_pair do |context, shoulds|
12
+ puts indent1 + "context \"#{context}\" do"
13
+ puts
14
+ shoulds.each do |should|
15
+ yaml_to_context( should, indent + 1 ) and next if should.is_a?( Hash )
16
+ puts indent2 + "should_eventually \"" + should.gsub(/^should +/,'') + "\" do"
17
+ puts indent2 + "end"
18
+ puts
19
+ end
20
+ puts indent1 + "end"
21
+ end
22
+ end
23
+
24
+ puts("Please pass in a FILE argument.") and exit unless ENV['FILE']
25
+
26
+ yaml_to_context( YAML.load_file( ENV['FILE'] ) )
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ Dir[File.join(File.dirname(__FILE__), 'tasks', '*.rake')].each do |f|
2
+ load f
3
+ end
@@ -0,0 +1,4 @@
1
+ module Shoulda
2
+ VERSION = "2.11.3"
3
+ end
4
+
data/lib/shoulda.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'shoulda/version'
2
+
3
+ if defined?(RSpec)
4
+ require 'shoulda/integrations/rspec2'
5
+ elsif defined?(Spec)
6
+ require 'shoulda/integrations/rspec'
7
+ else
8
+ require 'shoulda/integrations/test_unit'
9
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,8 @@
1
+ if RAILS_ENV == 'test'
2
+ if defined? Spec
3
+ require 'shoulda/integrations/rspec'
4
+ else
5
+ require 'shoulda/integrations/test_unit'
6
+ Shoulda.autoload_macros RAILS_ROOT, File.join("vendor", "{plugins,gems}", "*")
7
+ end
8
+ end
data/test/README ADDED
@@ -0,0 +1,36 @@
1
+ The Shoulda test suite (in particular - the tests that test shoulda)
2
+
3
+ Quick overview:
4
+
5
+ The test directory contains the following files and subdirectories:
6
+
7
+ * rails_root - contains the stripped down rails application that the tests run against. The rails root contains:
8
+ ** the models, controllers, and views defined under app/
9
+ ** the test.rb environment file
10
+ ** a migration file for each model
11
+ ** a shoulda initializer that simulates loading the plugin but without relying on vendor/plugins
12
+ * fixtures - contain the sample DB data for each model
13
+ * functional - controller tests for each of the controllers under rails_root/app
14
+ * unit - model tests for each of the models under rails_root/app
15
+ * other - tests for the shoulda contexts, should statements, and assertions
16
+ * test_helper.rb - responsible for initializing the test environment
17
+ ** sets the rails_env to test
18
+ ** sets the rails_root
19
+ ** runs all the migrations against the in-memory sqlite3 db
20
+ ** adds some magic to load the right fixture files
21
+
22
+ In order to add a new model (or controller) to the test suite:
23
+
24
+ * add that model to rails_root/app/models
25
+ * add a migration for that model
26
+ * add a fixture file
27
+ * add a test for that file under test/units
28
+
29
+ Dependencies:
30
+
31
+ * Rails gem installed in the host system
32
+ * A working sqlite3 installation.
33
+
34
+ If you have problems running these tests, please notify the mailing list: shoulda@googlegroups.com
35
+
36
+ - Tammer Saleh <tsaleh@thoughtbot.com>
@@ -0,0 +1,55 @@
1
+ module Shoulda
2
+ class << self
3
+ attr_accessor :expected_exceptions
4
+ end
5
+
6
+ module ClassMethods
7
+ # Enables the core shoulda test suite to test for failure scenarios. For
8
+ # example, to ensure that a set of test macros should fail, do this:
9
+ #
10
+ # should_fail do
11
+ # should_validate_presence_of :comments
12
+ # should_not_allow_mass_assignment_of :name
13
+ # end
14
+ def should_fail(&block)
15
+ context "should fail when trying to run:" do
16
+ if defined?(Test::Unit::AssertionFailedError)
17
+ failures = [Test::Unit::AssertionFailedError]
18
+ elsif defined?(MiniTest::Assertion)
19
+ failures = [MiniTest::Assertion]
20
+ end
21
+ Shoulda.expected_exceptions = failures
22
+ yield block
23
+ Shoulda.expected_exceptions = nil
24
+ end
25
+ end
26
+ end
27
+
28
+ class Context
29
+ # alias_method_chain hack to allow the should_fail macro to work
30
+ def should_with_failure_scenario(*args, &block)
31
+ should_without_failure_scenario(*args, &block)
32
+ wrap_last_should_with_failure_expectation
33
+ end
34
+ alias_method_chain :should, :failure_scenario
35
+
36
+ # alias_method_chain hack to allow the should_fail macro to work
37
+ def should_not_with_failure_scenario(*args, &block)
38
+ should_not_without_failure_scenario(*args, &block)
39
+ wrap_last_should_with_failure_expectation
40
+ end
41
+ alias_method_chain :should_not, :failure_scenario
42
+
43
+ def wrap_last_should_with_failure_expectation
44
+ if Shoulda.expected_exceptions
45
+ expected_exceptions = Shoulda.expected_exceptions
46
+ should = self.shoulds.last
47
+ assertion_block = should[:block]
48
+ failure_block = lambda do
49
+ assert_raise(*expected_exceptions, &assertion_block.bind(self))
50
+ end
51
+ should[:block] = failure_block
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ first:
2
+ title: Home
3
+ addressable: first (User)
File without changes
@@ -0,0 +1,5 @@
1
+ first:
2
+ id: 1
3
+ title: My Cute Kitten!
4
+ body: This is totally a cute kitten
5
+ user_id: 1
File without changes
File without changes
@@ -0,0 +1,9 @@
1
+ first:
2
+ id: 1
3
+ name: Stuff
4
+ second:
5
+ id: 2
6
+ name: Rails
7
+ third:
8
+ id: 3
9
+ name: Nothing
@@ -0,0 +1,6 @@
1
+ first:
2
+ id: 1
3
+ name: Some dude
4
+ age: 2
5
+ email: none@none.com
6
+ ssn: 123456789
@@ -0,0 +1,121 @@
1
+ require 'test_helper'
2
+ require 'posts_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class PostsController; def rescue_action(e) raise e end; end
6
+
7
+ class PostsControllerTest < ActionController::TestCase
8
+ fixtures :all
9
+
10
+ def setup
11
+ @controller = PostsController.new
12
+ @request = ActionController::TestRequest.new
13
+ @response = ActionController::TestResponse.new
14
+ @post = Post.find(:first)
15
+ end
16
+
17
+ # autodetects the :controller
18
+ should_route :get, '/posts', :action => :index
19
+ # explicitly specify :controller
20
+ should_route :post, '/posts', :controller => :posts, :action => :create
21
+ # non-string parameter
22
+ should_route :get, '/posts/1', :action => :show, :id => 1
23
+ # string-parameter
24
+ should_route :put, '/posts/1', :action => :update, :id => "1"
25
+ should_route :delete, '/posts/1', :action => :destroy, :id => 1
26
+ should_route :get, '/posts/new', :action => :new
27
+
28
+ # Test the nested routes
29
+ should_route :get, '/users/5/posts', :action => :index, :user_id => 5
30
+ should_route :post, '/users/5/posts', :action => :create, :user_id => 5
31
+ should_route :get, '/users/5/posts/1', :action => :show, :id => 1, :user_id => 5
32
+ should_route :delete, '/users/5/posts/1', :action => :destroy, :id => 1, :user_id => 5
33
+ should_route :get, '/users/5/posts/new', :action => :new, :user_id => 5
34
+ should_route :put, '/users/5/posts/1', :action => :update, :id => 1, :user_id => 5
35
+
36
+ context "Logged in" do
37
+ setup do
38
+ @request.session[:logged_in] = true
39
+ end
40
+
41
+ context "viewing posts for a user" do
42
+ setup do
43
+ get :index, :user_id => users(:first)
44
+ end
45
+ should_respond_with :success
46
+ should_assign_to :user, :class => User
47
+ should_render_template :index
48
+ should_assign_to(:user) { users(:first) }
49
+ should_fail do
50
+ should_assign_to :user, :class => Post
51
+ end
52
+ should_fail do
53
+ should_assign_to(:user) { posts(:first) }
54
+ end
55
+ should_assign_to :posts
56
+ should_not_assign_to :foo, :bar
57
+ end
58
+
59
+ context "viewing posts for a user with rss format" do
60
+ setup do
61
+ get :index, :user_id => users(:first), :format => 'rss'
62
+ @user = users(:first)
63
+ end
64
+ should_respond_with :success
65
+ should_respond_with_content_type 'application/rss+xml'
66
+ context "with a symbol" do
67
+ should_respond_with_content_type :rss
68
+ end
69
+ context "with a regexp" do
70
+ should_respond_with_content_type /rss/
71
+ end
72
+ should_set_session(:mischief) { nil }
73
+ should_set_session(:special) { '$2 off your next purchase' }
74
+ should_set_session(:special_user_id) { @user.id }
75
+ should_set_session(:false_var) { false }
76
+ should_fail do
77
+ should_set_session(:special_user_id) { 'value' }
78
+ end
79
+ should_assign_to :user, :posts
80
+ should_not_assign_to :foo, :bar
81
+ end
82
+
83
+ context "viewing a post on GET to #show" do
84
+ setup { get :show, :user_id => users(:first), :id => posts(:first) }
85
+ should_render_with_layout 'wide'
86
+ context "with a symbol" do # to avoid redefining a test
87
+ should_render_with_layout :wide
88
+ end
89
+ should_assign_to :false_flag
90
+ should_fail do
91
+ should_set_the_flash_to /.*/
92
+ end
93
+ end
94
+
95
+ context "on GET to #new" do
96
+ setup { get :new, :user_id => users(:first) }
97
+ should_render_without_layout
98
+ should_not_set_the_flash
99
+ end
100
+
101
+ context "on POST to #create" do
102
+ setup do
103
+ post :create, :user_id => users(:first),
104
+ :post => { :title => "first post",
105
+ :body => 'blah blah blah' }
106
+ end
107
+
108
+ should_redirect_to('the created post') { user_post_url(users(:first),
109
+ assigns(:post)) }
110
+ should_fail do
111
+ should_redirect_to('elsewhere') { user_posts_url(users(:first)) }
112
+ end
113
+
114
+ should_set_the_flash_to /success/
115
+ should_fail do
116
+ should_not_set_the_flash
117
+ end
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+ require 'users_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class UsersController; def rescue_action(e) raise e end; end
6
+
7
+ class UsersControllerTest < ActionController::TestCase
8
+ fixtures :all
9
+
10
+ def setup
11
+ @controller = UsersController.new
12
+ @request = ActionController::TestRequest.new
13
+ @response = ActionController::TestResponse.new
14
+ @user = User.find(:first)
15
+ end
16
+
17
+ should_filter_params :ssn
18
+
19
+ end
@@ -0,0 +1,37 @@
1
+ $: << File.join(File.dirname(__FILE__),"lib")
2
+
3
+ require File.join(File.dirname(__FILE__),"lib","shoulda")
4
+
5
+ class UserTest < Test::Unit::TestCase
6
+ context "For a new user" do
7
+ @@setup_run_count = 0
8
+
9
+ setup(:once) do
10
+ @x = "Hemant Kumar"
11
+ @@setup_run_count += 1
12
+ end
13
+
14
+ @@foo = 0
15
+ setup do
16
+ @@foo += 1
17
+ puts "Runing this thing #{@@foo}"
18
+ end
19
+
20
+ should "be set" do
21
+ assert_equal "Hemant Kumar", @x
22
+ assert_equal 1, @@setup_run_count
23
+ end
24
+
25
+ should "set hre as well"do
26
+ assert_equal "Hemant Kumar", @x
27
+ assert_equal 1, @@setup_run_count
28
+ end
29
+
30
+ context "For subcontexts" do
31
+ should "run in subcontexts" do
32
+ assert_equal "Hemant Kumar", @x
33
+ assert_equal 1, @@setup_run_count
34
+ end
35
+ end
36
+ end
37
+ end