Flamefork-shoulda 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +12 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +169 -0
  4. data/Rakefile +72 -0
  5. data/bin/convert_to_should_syntax +42 -0
  6. data/lib/shoulda/action_controller/helpers.rb +47 -0
  7. data/lib/shoulda/action_controller/macros.rb +277 -0
  8. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
  9. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
  10. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
  11. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +70 -0
  12. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
  13. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  14. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +87 -0
  15. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
  16. data/lib/shoulda/action_controller/matchers.rb +37 -0
  17. data/lib/shoulda/action_controller.rb +24 -0
  18. data/lib/shoulda/action_mailer/assertions.rb +38 -0
  19. data/lib/shoulda/action_mailer.rb +6 -0
  20. data/lib/shoulda/action_view/macros.rb +56 -0
  21. data/lib/shoulda/action_view.rb +6 -0
  22. data/lib/shoulda/active_record/assertions.rb +69 -0
  23. data/lib/shoulda/active_record/helpers.rb +40 -0
  24. data/lib/shoulda/active_record/macros.rb +588 -0
  25. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  26. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  27. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  28. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  29. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  30. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  31. data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
  32. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -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_numericality_of_matcher.rb +39 -0
  36. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  37. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  38. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  39. data/lib/shoulda/active_record/matchers.rb +42 -0
  40. data/lib/shoulda/active_record.rb +12 -0
  41. data/lib/shoulda/assertions.rb +59 -0
  42. data/lib/shoulda/autoload_macros.rb +46 -0
  43. data/lib/shoulda/context.rb +304 -0
  44. data/lib/shoulda/helpers.rb +8 -0
  45. data/lib/shoulda/macros.rb +73 -0
  46. data/lib/shoulda/private_helpers.rb +20 -0
  47. data/lib/shoulda/proc_extensions.rb +14 -0
  48. data/lib/shoulda/rails.rb +13 -0
  49. data/lib/shoulda/rspec.rb +11 -0
  50. data/lib/shoulda/tasks/list_tests.rake +29 -0
  51. data/lib/shoulda/tasks/yaml_to_shoulda.rake +28 -0
  52. data/lib/shoulda/tasks.rb +3 -0
  53. data/lib/shoulda/test_unit.rb +14 -0
  54. data/lib/shoulda.rb +9 -0
  55. data/rails/init.rb +7 -0
  56. data/test/README +36 -0
  57. data/test/fail_macros.rb +34 -0
  58. data/test/fixtures/addresses.yml +3 -0
  59. data/test/fixtures/friendships.yml +0 -0
  60. data/test/fixtures/posts.yml +5 -0
  61. data/test/fixtures/products.yml +0 -0
  62. data/test/fixtures/taggings.yml +0 -0
  63. data/test/fixtures/tags.yml +9 -0
  64. data/test/fixtures/users.yml +6 -0
  65. data/test/functional/posts_controller_test.rb +125 -0
  66. data/test/functional/users_controller_test.rb +19 -0
  67. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +68 -0
  68. data/test/matchers/active_record/allow_value_matcher_test.rb +41 -0
  69. data/test/matchers/active_record/association_matcher_test.rb +258 -0
  70. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +80 -0
  71. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  72. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  73. data/test/matchers/active_record/have_index_matcher_test.rb +74 -0
  74. data/test/matchers/active_record/have_named_scope_matcher_test.rb +65 -0
  75. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  76. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  77. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  78. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  79. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  80. data/test/matchers/controller/assign_to_matcher_test.rb +35 -0
  81. data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
  82. data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
  83. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +27 -0
  84. data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
  85. data/test/matchers/controller/route_matcher_test.rb +58 -0
  86. data/test/matchers/controller/set_session_matcher_test.rb +31 -0
  87. data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
  88. data/test/model_builder.rb +106 -0
  89. data/test/other/autoload_macro_test.rb +18 -0
  90. data/test/other/context_test.rb +145 -0
  91. data/test/other/convert_to_should_syntax_test.rb +63 -0
  92. data/test/other/helpers_test.rb +241 -0
  93. data/test/other/private_helpers_test.rb +34 -0
  94. data/test/other/should_test.rb +266 -0
  95. data/test/rails_root/app/controllers/application.rb +25 -0
  96. data/test/rails_root/app/controllers/posts_controller.rb +86 -0
  97. data/test/rails_root/app/controllers/users_controller.rb +84 -0
  98. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  99. data/test/rails_root/app/helpers/posts_helper.rb +2 -0
  100. data/test/rails_root/app/helpers/users_helper.rb +2 -0
  101. data/test/rails_root/app/models/address.rb +7 -0
  102. data/test/rails_root/app/models/flea.rb +3 -0
  103. data/test/rails_root/app/models/friendship.rb +4 -0
  104. data/test/rails_root/app/models/pets/dog.rb +10 -0
  105. data/test/rails_root/app/models/post.rb +12 -0
  106. data/test/rails_root/app/models/product.rb +12 -0
  107. data/test/rails_root/app/models/tag.rb +8 -0
  108. data/test/rails_root/app/models/tagging.rb +4 -0
  109. data/test/rails_root/app/models/treat.rb +3 -0
  110. data/test/rails_root/app/models/user.rb +29 -0
  111. data/test/rails_root/app/views/layouts/posts.rhtml +19 -0
  112. data/test/rails_root/app/views/layouts/users.rhtml +17 -0
  113. data/test/rails_root/app/views/layouts/wide.html.erb +1 -0
  114. data/test/rails_root/app/views/posts/edit.rhtml +27 -0
  115. data/test/rails_root/app/views/posts/index.rhtml +25 -0
  116. data/test/rails_root/app/views/posts/new.rhtml +26 -0
  117. data/test/rails_root/app/views/posts/show.rhtml +18 -0
  118. data/test/rails_root/app/views/users/edit.rhtml +22 -0
  119. data/test/rails_root/app/views/users/index.rhtml +22 -0
  120. data/test/rails_root/app/views/users/new.rhtml +21 -0
  121. data/test/rails_root/app/views/users/show.rhtml +13 -0
  122. data/test/rails_root/config/boot.rb +109 -0
  123. data/test/rails_root/config/database.yml +4 -0
  124. data/test/rails_root/config/environment.rb +14 -0
  125. data/test/rails_root/config/environments/test.rb +0 -0
  126. data/test/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  127. data/test/rails_root/config/initializers/shoulda.rb +8 -0
  128. data/test/rails_root/config/routes.rb +6 -0
  129. data/test/rails_root/db/migrate/001_create_users.rb +19 -0
  130. data/test/rails_root/db/migrate/002_create_posts.rb +13 -0
  131. data/test/rails_root/db/migrate/003_create_taggings.rb +12 -0
  132. data/test/rails_root/db/migrate/004_create_tags.rb +11 -0
  133. data/test/rails_root/db/migrate/005_create_dogs.rb +12 -0
  134. data/test/rails_root/db/migrate/006_create_addresses.rb +14 -0
  135. data/test/rails_root/db/migrate/007_create_fleas.rb +11 -0
  136. data/test/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  137. data/test/rails_root/db/migrate/009_create_products.rb +17 -0
  138. data/test/rails_root/db/migrate/010_create_friendships.rb +14 -0
  139. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  140. data/test/rails_root/db/schema.rb +0 -0
  141. data/test/rails_root/public/404.html +30 -0
  142. data/test/rails_root/public/422.html +30 -0
  143. data/test/rails_root/public/500.html +30 -0
  144. data/test/rails_root/script/console +3 -0
  145. data/test/rails_root/script/generate +3 -0
  146. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  147. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  148. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  149. data/test/rspec_test.rb +207 -0
  150. data/test/test_helper.rb +35 -0
  151. data/test/unit/address_test.rb +10 -0
  152. data/test/unit/dog_test.rb +10 -0
  153. data/test/unit/flea_test.rb +6 -0
  154. data/test/unit/friendship_test.rb +6 -0
  155. data/test/unit/post_test.rb +19 -0
  156. data/test/unit/product_test.rb +27 -0
  157. data/test/unit/tag_test.rb +15 -0
  158. data/test/unit/tagging_test.rb +6 -0
  159. data/test/unit/user_test.rb +70 -0
  160. metadata +258 -0
@@ -0,0 +1,12 @@
1
+ We're using GitHub[http://github.com/thoughtbot/shoulda/tree/master] and Lighthouse[http://thoughtbot.lighthouseapp.com/projects/5807], and we've been getting any combination of github pull requests, Lighthouse tickets, patches, emails, etc. We need to normalize this workflow to make sure we don't miss any fixes.
2
+
3
+ * Make sure you're accessing the source from the {official repository}[http://github.com/thoughtbot/shoulda/tree/master].
4
+ * We prefer git branches over patches, but we can take either.
5
+ * If you're using git, please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier.
6
+ * If you're submitting patches, please cut each fix or feature into a separate patch.
7
+ * There should be a Lighthouse[http://thoughtbot.lighthouseapp.com/projects/5807] ticket for any submission. If you've found a bug and want to fix it, open a new ticket at the same time.
8
+ * We've got github/lighthouse integration going, so you can update tickets when you commit. {This blog post}[http://hoth.entp.com/2008/4/11/github-and-lighthouse-sitting-in-a-tree] explains the commit options pretty well.
9
+ * Please <b>don't send pull requests</b> Just update the lighthouse ticket with the url for your fix (or attach the patch) when it's ready. The github pull requests pretty much get dropped on the floor until someone with commit rights notices them in the mailbox.
10
+ * Contributions without tests won't be accepted. The file <tt>/test/README</tt> explains the testing system pretty thoroughly.
11
+
12
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2007, Tammer Saleh, Thoughtbot, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,169 @@
1
+ = Shoulda - Making tests easy on the fingers and eyes
2
+
3
+ Shoulda makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of test macros, assertions, and helpers added on to the Test::Unit framework. It's fully compatible with your existing tests, and requires no retooling to use.
4
+
5
+ Helpers:: #context and #should give you RSpec like test blocks.
6
+ In addition, you get nested contexts and a much more readable syntax.
7
+ Macros:: Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros.
8
+ They get you started quickly, and can help you ensure that your application is conforming to best practices.
9
+ Assertions:: Many common rails testing idioms have been distilled into a set of useful assertions.
10
+ Matchers:: Rspec-compatible matchers providing the same tests as Shoulda macros.
11
+
12
+ = Usage
13
+
14
+ === Context Helpers (Shoulda::Context)
15
+
16
+ Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
17
+
18
+ class UserTest < Test::Unit::TestCase
19
+ context "A User instance" do
20
+ setup do
21
+ @user = User.find(:first)
22
+ end
23
+
24
+ should "return its full name" do
25
+ assert_equal 'John Doe', @user.full_name
26
+ end
27
+
28
+ context "with a profile" do
29
+ setup do
30
+ @user.profile = Profile.find(:first)
31
+ end
32
+
33
+ should "return true when sent #has_profile?" do
34
+ assert @user.has_profile?
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ Produces the following test methods:
41
+
42
+ "test: A User instance should return its full name."
43
+ "test: A User instance with a profile should return true when sent #has_profile?."
44
+
45
+ So readable!
46
+
47
+ === ActiveRecord Tests (Shoulda::ActiveRecord::Macros)
48
+
49
+ Quick macro tests for your ActiveRecord associations and validations:
50
+
51
+ class PostTest < Test::Unit::TestCase
52
+ fixtures :all
53
+
54
+ should_belong_to :user
55
+ should_have_many :tags, :through => :taggings
56
+
57
+ should_validate_uniqueness_of :title
58
+ should_validate_presence_of :body, :message => /wtf/
59
+ should_validate_presence_of :title
60
+ should_validate_numericality_of :user_id
61
+ end
62
+
63
+ class UserTest < Test::Unit::TestCase
64
+ should_have_many :posts
65
+
66
+ should_not_allow_values_for :email, "blah", "b lah"
67
+ should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
68
+ should_ensure_length_in_range :email, 1..100
69
+ should_ensure_value_in_range :age, 1..100
70
+ should_not_allow_mass_assignment_of :password
71
+ end
72
+
73
+ Makes TDD so much easier.
74
+
75
+ === Controller Tests (Shoulda::Controller::Macros)
76
+
77
+ Macros to test the most common controller patterns...
78
+
79
+ context "on GET to :show for first record" do
80
+ setup do
81
+ get :show, :id => 1
82
+ end
83
+
84
+ should_assign_to :user
85
+ should_respond_with :success
86
+ should_render_template :show
87
+ should_not_set_the_flash
88
+
89
+ should "do something else really cool" do
90
+ assert_equal 1, assigns(:user).id
91
+ end
92
+ end
93
+
94
+ === Helpful Assertions (Shoulda::Assertions)
95
+
96
+ More to come here, but have fun with what's there.
97
+
98
+ assert_same_elements([:a, :b, :c], [:c, :a, :b])
99
+ assert_contains(['a', '1'], /\d/)
100
+ assert_contains(['a', '1'], 'a')
101
+
102
+ === 3rd Party and Application Specific Macros
103
+
104
+ Any *.rb file under RAILS_ROOT/test/shoulda_macros/ or vendor/(plugins|gems)/gem_name/shoulda_macros/ will be automatically required when you run your tests. This allows you to distribute macros with your plugins, or to organize the macros inside your application. Remember to add your macro to Test::Unit::TestCase in the macro file:
105
+
106
+ # test/shoulda_macros/security.rb
107
+ class Test::Unit::TestCase
108
+ def self.should_be_denied(opts = {})
109
+ should_set_the_flash_to(opts[:flash] || /Please log in/i)
110
+ should_redirect_to(opts[:redirect] || 'login_url')
111
+ end
112
+ end
113
+
114
+ = Rails Installation (Test::Unit)
115
+
116
+ === As a Gem
117
+
118
+ Use this if you prefer to use versioned releases of shoulda. Specify the gem dependency in your config/environment.rb file:
119
+
120
+ Rails::Initializer.run do |config|
121
+ config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com"
122
+ end
123
+
124
+ Then:
125
+
126
+ $ rake gems:install
127
+ $ rake gems:unpack
128
+
129
+ === As a Plugin
130
+
131
+ Use this if you prefer to use the edge version of shoulda:
132
+
133
+ $ script/plugin install git://github.com/thoughtbot/shoulda.git
134
+
135
+ === As a Plugin (using git submodules)
136
+
137
+ Use this if you prefer the idea of being able to easily switch between using edge or a tagged version of shoulda:
138
+
139
+ $ git submodule add git://github.com/thoughtbot/shoulda.git vendor/plugins/shoulda
140
+
141
+ = Rails Installation (RSpec)
142
+
143
+ If you're using Shoulda with RSpec, we recommend that you add config.gem lines
144
+ for RSpec and Shoulda in your config/environment/test.rb file, but do not ask
145
+ Rails to load the RSpec and Shoulda libraries:
146
+
147
+ config.gem 'rspec', :lib => false
148
+ config.gem 'rspec-rails', :lib => false
149
+ config.gem 'thoughtbot-shoulda',
150
+ :lib => false,
151
+ :source => 'http://gems.github.com'
152
+
153
+ Then require shoulda from your spec/spec_helper.rb file, before Spec::Runner is
154
+ configured:
155
+
156
+ # requires for RSpec
157
+ require 'shoulda'
158
+ Spec::Runner.configure do |config|
159
+ # ...
160
+
161
+ You should not need to require anything besides the top-level shoulda library.
162
+
163
+ = Credits
164
+
165
+ Shoulda is maintained by {Tammer Saleh}[mailto:tsaleh@thoughtbot.com], and is funded by Thoughtbot[http://www.thoughtbot.com], inc.
166
+
167
+ = License
168
+
169
+ Shoulda is Copyright © 2006-2008 Tammer Saleh, Thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,72 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ $LOAD_PATH.unshift("lib")
7
+ require 'shoulda'
8
+ load 'tasks/shoulda.rake'
9
+
10
+ # Test::Unit::UI::VERBOSE
11
+ test_files_pattern = 'test/{unit,functional,other,matchers}/**/*_test.rb'
12
+ Rake::TestTask.new do |t|
13
+ t.libs << 'lib'
14
+ t.pattern = test_files_pattern
15
+ t.verbose = false
16
+ end
17
+
18
+ Rake::RDocTask.new { |rdoc|
19
+ rdoc.rdoc_dir = 'doc'
20
+ rdoc.title = "Shoulda -- Making tests easy on the fingers and eyes"
21
+ rdoc.options << '--line-numbers'
22
+ rdoc.template = "#{ENV['template']}.rb" if ENV['template']
23
+ rdoc.rdoc_files.include('README.rdoc', 'CONTRIBUTION_GUIDELINES.rdoc', 'lib/**/*.rb')
24
+ }
25
+
26
+ desc "Run code-coverage analysis using rcov"
27
+ task :coverage do
28
+ rm_rf "coverage"
29
+ files = Dir[test_files_pattern]
30
+ system "rcov --rails --sort coverage -Ilib #{files.join(' ')}"
31
+ end
32
+
33
+ desc 'Update documentation on website'
34
+ task :sync_docs => 'rdoc' do
35
+ `rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/shoulda`
36
+ end
37
+
38
+ desc 'Default: run tests.'
39
+ task :default => ['test']
40
+
41
+ spec = Gem::Specification.new do |s|
42
+ s.name = "shoulda"
43
+ s.version = Shoulda::VERSION
44
+ s.summary = "Making tests easy on the fingers and eyes"
45
+ s.homepage = "http://thoughtbot.com/projects/shoulda"
46
+ s.rubyforge_project = "shoulda"
47
+
48
+ s.files = FileList["[A-Z]*", "{bin,lib,rails,test}/**/*"]
49
+ s.executables = s.files.grep(/^bin/) { |f| File.basename(f) }
50
+
51
+ s.has_rdoc = true
52
+ s.extra_rdoc_files = ["README.rdoc", "CONTRIBUTION_GUIDELINES.rdoc"]
53
+ s.rdoc_options = ["--line-numbers", "--main", "README.rdoc"]
54
+
55
+ s.authors = ["Tammer Saleh"]
56
+ s.email = "tsaleh@thoughtbot.com"
57
+ end
58
+
59
+ Rake::GemPackageTask.new spec do |pkg|
60
+ pkg.need_tar = true
61
+ pkg.need_zip = true
62
+ end
63
+
64
+ desc "Clean files generated by rake tasks"
65
+ task :clobber => [:clobber_rdoc, :clobber_package]
66
+
67
+ desc "Generate a gemspec file for GitHub"
68
+ task :gemspec do
69
+ File.open("#{spec.name}.gemspec", 'w') do |f|
70
+ f.write spec.to_ruby
71
+ end
72
+ end
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+
5
+ TMP = Dir::tmpdir
6
+
7
+ def usage(msg = nil)
8
+ puts "Error: #{msg}" if msg
9
+ puts if msg
10
+ puts "Usage: #{File.basename(__FILE__)} normal_test_file.rb"
11
+ puts
12
+ puts "Will convert an existing test file with names like "
13
+ puts
14
+ puts " def test_should_do_stuff"
15
+ puts " ..."
16
+ puts " end"
17
+ puts
18
+ puts "to one using the new syntax: "
19
+ puts
20
+ puts " should \"be super cool\" do"
21
+ puts " ..."
22
+ puts " end"
23
+ puts
24
+ puts "A copy of the old file will be left under #{TMP} in case\nthis script just seriously screws up"
25
+ puts
26
+ exit (msg ? 2 : 0)
27
+ end
28
+
29
+ usage("Wrong number of arguments.") unless ARGV.size == 1
30
+ usage("Temp directory '#{TMP}' is not valid. Set TMPDIR environment variable to a writeable directory.") unless File.directory?(TMP) && File.writable?(TMP)
31
+
32
+ file = ARGV.shift
33
+ tmpfile = File.join(TMP, File.basename(file))
34
+ usage("File '#{file}' doesn't exist") unless File.exists?(file)
35
+
36
+ FileUtils.cp(file, tmpfile)
37
+ contents = File.read(tmpfile)
38
+ contents.gsub!(/def test_should_(\S+)/) {|line| "should \"#{$1.tr('_', ' ')}\" do"}
39
+ contents.gsub!(/def test_(\S+)/) {|line| "should \"RENAME ME: test #{$1.tr('_', ' ')}\" do"}
40
+ File.open(file, 'w') { |f| f.write(contents) }
41
+
42
+ puts "File '#{file}' has been converted to 'should' syntax. Old version has been stored in '#{tmpfile}'"
@@ -0,0 +1,47 @@
1
+ module Shoulda # :nodoc:
2
+ module ActionController # :nodoc:
3
+ module Helpers # :nodoc:
4
+ private # :enddoc:
5
+
6
+ SPECIAL_INSTANCE_VARIABLES = %w{
7
+ _cookies
8
+ _flash
9
+ _headers
10
+ _params
11
+ _request
12
+ _response
13
+ _session
14
+ action_name
15
+ before_filter_chain_aborted
16
+ cookies
17
+ flash
18
+ headers
19
+ ignore_missing_templates
20
+ logger
21
+ params
22
+ request
23
+ request_origin
24
+ response
25
+ session
26
+ template
27
+ template_class
28
+ template_root
29
+ url
30
+ variables_added
31
+ }.map(&:to_s)
32
+
33
+ def instantiate_variables_from_assigns(*names, &blk)
34
+ old = {}
35
+ names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty?
36
+ names.each do |name|
37
+ old[name] = instance_variable_get("@#{name}")
38
+ instance_variable_set("@#{name}", assigns(name.to_sym))
39
+ end
40
+ blk.call
41
+ names.each do |name|
42
+ instance_variable_set("@#{name}", old[name])
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,277 @@
1
+ module Shoulda # :nodoc:
2
+ module ActionController # :nodoc:
3
+ # = Macro test helpers for your controllers
4
+ #
5
+ # By using the macro helpers you can quickly and easily create concise and easy to read test suites.
6
+ #
7
+ # This code segment:
8
+ # context "on GET to :show for first record" do
9
+ # setup do
10
+ # get :show, :id => 1
11
+ # end
12
+ #
13
+ # should_assign_to :user
14
+ # should_respond_with :success
15
+ # should_render_template :show
16
+ # should_not_set_the_flash
17
+ #
18
+ # should "do something else really cool" do
19
+ # assert_equal 1, assigns(:user).id
20
+ # end
21
+ # end
22
+ #
23
+ # Would produce 5 tests for the +show+ action
24
+ module Macros
25
+ include Matchers
26
+
27
+ # Macro that creates a test asserting that the flash contains the given value.
28
+ # val can be a String, a Regex, or nil (indicating that the flash should not be set)
29
+ #
30
+ # Example:
31
+ #
32
+ # should_set_the_flash_to "Thank you for placing this order."
33
+ # should_set_the_flash_to /created/i
34
+ # should_set_the_flash_to nil
35
+ def should_set_the_flash_to(val)
36
+ matcher = set_the_flash.to(val)
37
+ if val
38
+ should matcher.description do
39
+ assert_accepts matcher, @controller
40
+ end
41
+ else
42
+ should "not #{matcher.description}" do
43
+ assert_rejects matcher, @controller
44
+ end
45
+ end
46
+ end
47
+
48
+ # Macro that creates a test asserting that the flash is empty. Same as
49
+ # @should_set_the_flash_to nil@
50
+ def should_not_set_the_flash
51
+ should_set_the_flash_to nil
52
+ end
53
+
54
+ # Macro that creates a test asserting that filter_parameter_logging
55
+ # is set for the specified keys
56
+ #
57
+ # Example:
58
+ #
59
+ # should_filter_params :password, :ssn
60
+ def should_filter_params(*keys)
61
+ keys.each do |key|
62
+ matcher = filter_param(key)
63
+ should matcher.description do
64
+ assert_accepts matcher, @controller
65
+ end
66
+ end
67
+ end
68
+
69
+ # Macro that creates a test asserting that the controller assigned to
70
+ # each of the named instance variable(s).
71
+ #
72
+ # Options:
73
+ # * <tt>:class</tt> - The expected class of the instance variable being checked.
74
+ # * <tt>:equals</tt> - A string which is evaluated and compared for equality with
75
+ # the instance variable being checked.
76
+ #
77
+ # Example:
78
+ #
79
+ # should_assign_to :user, :posts
80
+ # should_assign_to :user, :class => User
81
+ # should_assign_to(:user) { @user }
82
+ def should_assign_to(*names, &block)
83
+ opts = names.extract_options!
84
+ if opts[:equals]
85
+ warn "[DEPRECATION] should_assign_to :var, :equals => 'val' " <<
86
+ "is deprecated. Use should_assign_to(:var) { 'val' } instead."
87
+ end
88
+ names.each do |name|
89
+ matcher = assign_to(name).with_kind_of(opts[:class])
90
+ test_name = matcher.description
91
+ test_name << " which is equal to #{opts[:equals]}" if opts[:equals]
92
+ should test_name do
93
+ if opts[:equals]
94
+ instantiate_variables_from_assigns do
95
+ expected_value = eval(opts[:equals],
96
+ self.send(:binding),
97
+ __FILE__,
98
+ __LINE__)
99
+ matcher = matcher.with(expected_value)
100
+ end
101
+ elsif block
102
+ expected_value = instance_eval(&block)
103
+ matcher = matcher.with(expected_value)
104
+ end
105
+
106
+ assert_accepts matcher, @controller
107
+ end
108
+ end
109
+ end
110
+
111
+ # Macro that creates a test asserting that the controller did not assign to
112
+ # any of the named instance variable(s).
113
+ #
114
+ # Example:
115
+ #
116
+ # should_not_assign_to :user, :posts
117
+ def should_not_assign_to(*names)
118
+ names.each do |name|
119
+ matcher = assign_to(name)
120
+ should "not #{matcher.description}" do
121
+ assert_rejects matcher, @controller
122
+ end
123
+ end
124
+ end
125
+
126
+ # Macro that creates a test asserting that the controller responded with a 'response' status code.
127
+ # Example:
128
+ #
129
+ # should_respond_with :success
130
+ def should_respond_with(response)
131
+ should "respond with #{response}" do
132
+ matcher = respond_with(response)
133
+ assert_accepts matcher, @controller
134
+ end
135
+ end
136
+
137
+ # Macro that creates a test asserting that the response content type was 'content_type'.
138
+ # Example:
139
+ #
140
+ # should_respond_with_content_type 'application/rss+xml'
141
+ # should_respond_with_content_type :rss
142
+ # should_respond_with_content_type /rss/
143
+ def should_respond_with_content_type(content_type)
144
+ should "respond with content type of #{content_type}" do
145
+ matcher = respond_with_content_type(content_type)
146
+ assert_accepts matcher, @controller
147
+ end
148
+ end
149
+
150
+ # Macro that creates a test asserting that a value returned from the session is correct.
151
+ # The given string is evaled to produce the resulting redirect path. All of the instance variables
152
+ # set by the controller are available to the evaled string.
153
+ # Example:
154
+ #
155
+ # should_set_session(:user_id) { '@user.id' }
156
+ # should_set_session(:message) { "Free stuff" }
157
+ def should_set_session(key, expected = nil, &block)
158
+ matcher = set_session(key)
159
+ if expected
160
+ warn "[DEPRECATION] should_set_session :key, 'val' is deprecated. " <<
161
+ "Use should_set_session(:key) { 'val' } instead."
162
+ end
163
+ should matcher.description do
164
+ if expected
165
+ instantiate_variables_from_assigns do
166
+ expected_value = eval(expected,
167
+ self.send(:binding),
168
+ __FILE__,
169
+ __LINE__)
170
+ matcher = matcher.to(expected_value)
171
+ end
172
+ else
173
+ expected_value = instance_eval(&block)
174
+ matcher = matcher.to(expected_value)
175
+ end
176
+ assert_accepts matcher, @controller
177
+ end
178
+ end
179
+
180
+ # Deprecated. See should_set_session
181
+ def should_return_from_session(key, expected)
182
+ warn "[DEPRECATION] should_return_from_session is deprecated. " <<
183
+ "Use should_set_session instead."
184
+ should_set_session(key, expected)
185
+ end
186
+
187
+ # Macro that creates a test asserting that the controller rendered the given template.
188
+ # Example:
189
+ #
190
+ # should_render_template :new
191
+ def should_render_template(template)
192
+ should "render template #{template.inspect}" do
193
+ assert_template template.to_s
194
+ end
195
+ end
196
+
197
+ # Macro that creates a test asserting that the controller rendered with the given layout.
198
+ # Example:
199
+ #
200
+ # should_render_with_layout 'special'
201
+ def should_render_with_layout(expected_layout = 'application')
202
+ matcher = render_with_layout(expected_layout)
203
+ if expected_layout
204
+ should matcher.description do
205
+ assert_accepts matcher, @controller
206
+ end
207
+ else
208
+ should "render without layout" do
209
+ assert_rejects matcher, @controller
210
+ end
211
+ end
212
+ end
213
+
214
+ # Macro that creates a test asserting that the controller rendered without a layout.
215
+ # Same as @should_render_with_layout false@
216
+ def should_render_without_layout
217
+ should_render_with_layout nil
218
+ end
219
+
220
+ # Macro that creates a test asserting that the controller returned a redirect to the given path.
221
+ # The given string is evaled to produce the resulting redirect path. All of the instance variables
222
+ # set by the controller are available to the evaled string.
223
+ # Example:
224
+ #
225
+ # should_redirect_to("the user's profile") { user_url(@user) }
226
+ def should_redirect_to(description, &block)
227
+ unless block
228
+ warn "[DEPRECATION] should_redirect_to without a block is " <<
229
+ "deprecated. Use should_redirect_to('somewhere') { } instead."
230
+ end
231
+ should "redirect to #{description}" do
232
+ if block
233
+ url = instance_eval(&block)
234
+ else
235
+ instantiate_variables_from_assigns do
236
+ url = eval(description, self.send(:binding), __FILE__, __LINE__)
237
+ end
238
+ end
239
+ assert_redirected_to url
240
+ end
241
+ end
242
+
243
+ # Macro that creates a routing test. It tries to use the given HTTP
244
+ # +method+ on the given +path+, and asserts that it routes to the
245
+ # given +options+.
246
+ #
247
+ # If you don't specify a :controller, it will try to guess the controller
248
+ # based on the current test.
249
+ #
250
+ # +to_param+ is called on the +options+ given.
251
+ #
252
+ # Examples:
253
+ #
254
+ # should_route :get, "/posts", :controller => :posts, :action => :index
255
+ # should_route :get, "/posts/new", :action => :new
256
+ # should_route :post, "/posts", :action => :create
257
+ # should_route :get, "/posts/1", :action => :show, :id => 1
258
+ # should_route :edit, "/posts/1", :action => :show, :id => 1
259
+ # should_route :put, "/posts/1", :action => :update, :id => 1
260
+ # should_route :delete, "/posts/1", :action => :destroy, :id => 1
261
+ # should_route :get, "/users/1/posts/1",
262
+ # :action => :show, :id => 1, :user_id => 1
263
+ #
264
+ def should_route(method, path, options)
265
+ unless options[:controller]
266
+ options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
267
+ end
268
+
269
+ matcher = route(method, path).to(options)
270
+
271
+ should matcher.description do
272
+ assert_accepts matcher.in_context(self), self
273
+ end
274
+ end
275
+ end
276
+ end
277
+ end