rails 4.1.4 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -10
  3. data/guides/CHANGELOG.md +15 -25
  4. data/guides/Rakefile +5 -3
  5. data/guides/assets/javascripts/guides.js +6 -0
  6. data/guides/assets/stylesheets/main.css +4 -1
  7. data/guides/bug_report_templates/action_controller_gem.rb +2 -2
  8. data/guides/bug_report_templates/action_controller_master.rb +3 -2
  9. data/guides/rails_guides/helpers.rb +1 -1
  10. data/guides/rails_guides/levenshtein.rb +29 -21
  11. data/guides/rails_guides/markdown/renderer.rb +1 -1
  12. data/guides/rails_guides/markdown.rb +11 -7
  13. data/guides/rails_guides.rb +2 -2
  14. data/guides/source/2_2_release_notes.md +1 -1
  15. data/guides/source/2_3_release_notes.md +4 -4
  16. data/guides/source/3_0_release_notes.md +8 -8
  17. data/guides/source/3_1_release_notes.md +5 -2
  18. data/guides/source/3_2_release_notes.md +6 -3
  19. data/guides/source/4_0_release_notes.md +6 -3
  20. data/guides/source/4_1_release_notes.md +10 -11
  21. data/guides/source/4_2_release_notes.md +850 -0
  22. data/guides/source/_license.html.erb +1 -1
  23. data/guides/source/_welcome.html.erb +2 -8
  24. data/guides/source/action_controller_overview.md +84 -10
  25. data/guides/source/action_mailer_basics.md +91 -28
  26. data/guides/source/action_view_overview.md +140 -130
  27. data/guides/source/active_job_basics.md +318 -0
  28. data/guides/source/active_model_basics.md +371 -17
  29. data/guides/source/active_record_basics.md +19 -18
  30. data/guides/source/active_record_callbacks.md +12 -9
  31. data/guides/source/{migrations.md → active_record_migrations.md} +135 -226
  32. data/guides/source/active_record_postgresql.md +433 -0
  33. data/guides/source/active_record_querying.md +269 -259
  34. data/guides/source/active_record_validations.md +21 -12
  35. data/guides/source/active_support_core_extensions.md +113 -73
  36. data/guides/source/active_support_instrumentation.md +10 -7
  37. data/guides/source/api_documentation_guidelines.md +62 -16
  38. data/guides/source/asset_pipeline.md +264 -67
  39. data/guides/source/association_basics.md +81 -74
  40. data/guides/source/caching_with_rails.md +32 -7
  41. data/guides/source/command_line.md +52 -30
  42. data/guides/source/configuring.md +132 -29
  43. data/guides/source/constant_autoloading_and_reloading.md +1297 -0
  44. data/guides/source/contributing_to_ruby_on_rails.md +192 -112
  45. data/guides/source/credits.html.erb +2 -2
  46. data/guides/source/debugging_rails_applications.md +448 -294
  47. data/guides/source/development_dependencies_install.md +47 -36
  48. data/guides/source/documents.yaml +19 -7
  49. data/guides/source/engines.md +210 -189
  50. data/guides/source/form_helpers.md +79 -56
  51. data/guides/source/generators.md +24 -11
  52. data/guides/source/getting_started.md +339 -201
  53. data/guides/source/i18n.md +111 -68
  54. data/guides/source/index.html.erb +1 -0
  55. data/guides/source/initialization.md +109 -62
  56. data/guides/source/layout.html.erb +1 -4
  57. data/guides/source/layouts_and_rendering.md +18 -17
  58. data/guides/source/maintenance_policy.md +26 -4
  59. data/guides/source/nested_model_forms.md +7 -4
  60. data/guides/source/plugins.md +27 -27
  61. data/guides/source/rails_application_templates.md +21 -3
  62. data/guides/source/rails_on_rack.md +12 -9
  63. data/guides/source/routing.md +100 -74
  64. data/guides/source/ruby_on_rails_guides_guidelines.md +11 -12
  65. data/guides/source/security.md +40 -34
  66. data/guides/source/testing.md +188 -117
  67. data/guides/source/upgrading_ruby_on_rails.md +284 -29
  68. data/guides/source/working_with_javascript_in_rails.md +18 -16
  69. data/guides/w3c_validator.rb +2 -0
  70. metadata +40 -94
  71. data/guides/code/getting_started/Gemfile +0 -40
  72. data/guides/code/getting_started/Gemfile.lock +0 -125
  73. data/guides/code/getting_started/README.rdoc +0 -28
  74. data/guides/code/getting_started/Rakefile +0 -6
  75. data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
  76. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
  77. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
  78. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
  79. data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
  80. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
  81. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
  82. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
  83. data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
  84. data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
  85. data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
  86. data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
  87. data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
  88. data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
  89. data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
  90. data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
  91. data/guides/code/getting_started/app/models/comment.rb +0 -3
  92. data/guides/code/getting_started/app/models/post.rb +0 -7
  93. data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
  94. data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
  95. data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
  96. data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
  97. data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
  98. data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
  99. data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
  100. data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
  101. data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
  102. data/guides/code/getting_started/bin/bundle +0 -4
  103. data/guides/code/getting_started/bin/rails +0 -4
  104. data/guides/code/getting_started/bin/rake +0 -4
  105. data/guides/code/getting_started/config/application.rb +0 -18
  106. data/guides/code/getting_started/config/boot.rb +0 -4
  107. data/guides/code/getting_started/config/database.yml +0 -25
  108. data/guides/code/getting_started/config/environment.rb +0 -5
  109. data/guides/code/getting_started/config/environments/development.rb +0 -30
  110. data/guides/code/getting_started/config/environments/production.rb +0 -80
  111. data/guides/code/getting_started/config/environments/test.rb +0 -36
  112. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
  113. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
  114. data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
  115. data/guides/code/getting_started/config/initializers/locale.rb +0 -9
  116. data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
  117. data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
  118. data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
  119. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
  120. data/guides/code/getting_started/config/locales/en.yml +0 -23
  121. data/guides/code/getting_started/config/routes.rb +0 -7
  122. data/guides/code/getting_started/config.ru +0 -4
  123. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
  124. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
  125. data/guides/code/getting_started/db/schema.rb +0 -33
  126. data/guides/code/getting_started/db/seeds.rb +0 -7
  127. data/guides/code/getting_started/public/404.html +0 -60
  128. data/guides/code/getting_started/public/422.html +0 -60
  129. data/guides/code/getting_started/public/500.html +0 -59
  130. data/guides/code/getting_started/public/favicon.ico +0 -0
  131. data/guides/code/getting_started/public/robots.txt +0 -5
  132. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
  133. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
  134. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
  135. data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
  136. data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
  137. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
  138. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
  139. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
  140. data/guides/code/getting_started/test/models/comment_test.rb +0 -7
  141. data/guides/code/getting_started/test/models/post_test.rb +0 -7
  142. data/guides/code/getting_started/test/test_helper.rb +0 -12
@@ -49,7 +49,9 @@ The `test_helper.rb` file holds the default configuration for your tests.
49
49
 
50
50
  ### The Low-Down on Fixtures
51
51
 
52
- For good tests, you'll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.
52
+ For good tests, you'll need to give some thought to setting up test data.
53
+ In Rails, you can handle this by defining and customizing fixtures.
54
+ You can find comprehensive documentation in the [fixture api documentation](http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html).
53
55
 
54
56
  #### What Are Fixtures?
55
57
 
@@ -80,7 +82,7 @@ Each fixture is given a name followed by an indented list of colon-separated key
80
82
 
81
83
  If you are working with [associations](/association_basics.html), you can simply
82
84
  define a reference node between two different fixtures. Here's an example with
83
- a belongs_to/has_many association:
85
+ a `belongs_to`/`has_many` association:
84
86
 
85
87
  ```yaml
86
88
  # In fixtures/categories.yml
@@ -94,6 +96,12 @@ one:
94
96
  category: about
95
97
  ```
96
98
 
99
+ Note: For associations to reference one another by name, you cannot specify the `id:`
100
+ attribute on the fixtures. Rails will auto assign a primary key to be consistent between
101
+ runs. If you manually specify an `id:` attribute, this behavior will not work. For more
102
+ information on this association behavior please read the
103
+ [fixture api documentation](http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html).
104
+
97
105
  #### ERB'in It Up
98
106
 
99
107
  ERB allows you to embed Ruby code within templates. The YAML fixture format is pre-processed with ERB when Rails loads fixtures. This allows you to use Ruby to help you generate some sample data. For example, the following code generates a thousand users:
@@ -134,27 +142,27 @@ Unit Testing your Models
134
142
 
135
143
  In Rails, models tests are what you write to test your models.
136
144
 
137
- For this guide we will be using Rails _scaffolding_. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practices. I will be using examples from this generated code and will be supplementing it with additional examples where necessary.
145
+ For this guide we will be using Rails _scaffolding_. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practices. We will be using examples from this generated code and will be supplementing it with additional examples where necessary.
138
146
 
139
- NOTE: For more information on Rails <i>scaffolding</i>, refer to [Getting Started with Rails](getting_started.html)
147
+ NOTE: For more information on Rails _scaffolding_, refer to [Getting Started with Rails](getting_started.html)
140
148
 
141
149
  When you use `rails generate scaffold`, for a resource among other things it creates a test stub in the `test/models` folder:
142
150
 
143
151
  ```bash
144
- $ bin/rails generate scaffold post title:string body:text
152
+ $ bin/rails generate scaffold article title:string body:text
145
153
  ...
146
- create app/models/post.rb
147
- create test/models/post_test.rb
148
- create test/fixtures/posts.yml
154
+ create app/models/article.rb
155
+ create test/models/article_test.rb
156
+ create test/fixtures/articles.yml
149
157
  ...
150
158
  ```
151
159
 
152
- The default test stub in `test/models/post_test.rb` looks like this:
160
+ The default test stub in `test/models/article_test.rb` looks like this:
153
161
 
154
162
  ```ruby
155
163
  require 'test_helper'
156
164
 
157
- class PostTest < ActiveSupport::TestCase
165
+ class ArticleTest < ActiveSupport::TestCase
158
166
  # test "the truth" do
159
167
  # assert true
160
168
  # end
@@ -170,15 +178,15 @@ require 'test_helper'
170
178
  As you know by now, `test_helper.rb` specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.
171
179
 
172
180
  ```ruby
173
- class PostTest < ActiveSupport::TestCase
181
+ class ArticleTest < ActiveSupport::TestCase
174
182
  ```
175
183
 
176
- The `PostTest` class defines a _test case_ because it inherits from `ActiveSupport::TestCase`. `PostTest` thus has all the methods available from `ActiveSupport::TestCase`. You'll see those methods a little later in this guide.
184
+ The `ArticleTest` class defines a _test case_ because it inherits from `ActiveSupport::TestCase`. `ArticleTest` thus has all the methods available from `ActiveSupport::TestCase`. You'll see those methods a little later in this guide.
177
185
 
178
- Any method defined within a class inherited from `MiniTest::Unit::TestCase`
179
- (which is the superclass of `ActiveSupport::TestCase`) that begins with `test` (case sensitive) is simply called a test. So, `test_password`, `test_valid_password` and `testValidPassword` all are legal test names and are run automatically when the test case is run.
186
+ Any method defined within a class inherited from `Minitest::Test`
187
+ (which is the superclass of `ActiveSupport::TestCase`) that begins with `test_` (case sensitive) is simply called a test. So, `test_password` and `test_valid_password` are legal test names and are run automatically when the test case is run.
180
188
 
181
- Rails adds a `test` method that takes a test name and a block. It generates a normal `MiniTest::Unit` test with method names prefixed with `test_`. So,
189
+ Rails adds a `test` method that takes a test name and a block. It generates a normal `Minitest::Unit` test with method names prefixed with `test_`. So,
182
190
 
183
191
  ```ruby
184
192
  test "the truth" do
@@ -220,7 +228,7 @@ In order to run your tests, your test database will need to have the current str
220
228
  Running a test is as simple as invoking the file containing the test cases through `rake test` command.
221
229
 
222
230
  ```bash
223
- $ bin/rake test test/models/post_test.rb
231
+ $ bin/rake test test/models/article_test.rb
224
232
  .
225
233
 
226
234
  Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s.
@@ -231,7 +239,7 @@ Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s.
231
239
  You can also run a particular test method from the test case by running the test and providing the `test method name`.
232
240
 
233
241
  ```bash
234
- $ bin/rake test test/models/post_test.rb test_the_truth
242
+ $ bin/rake test test/models/article_test.rb test_the_truth
235
243
  .
236
244
 
237
245
  Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s.
@@ -243,25 +251,25 @@ This will run all test methods from the test case. Note that `test_helper.rb` is
243
251
 
244
252
  The `.` (dot) above indicates a passing test. When a test fails you see an `F`; when a test throws an error you see an `E` in its place. The last line of the output is the summary.
245
253
 
246
- To see how a test failure is reported, you can add a failing test to the `post_test.rb` test case.
254
+ To see how a test failure is reported, you can add a failing test to the `article_test.rb` test case.
247
255
 
248
256
  ```ruby
249
- test "should not save post without title" do
250
- post = Post.new
251
- assert_not post.save
257
+ test "should not save article without title" do
258
+ article = Article.new
259
+ assert_not article.save
252
260
  end
253
261
  ```
254
262
 
255
263
  Let us run this newly added test.
256
264
 
257
265
  ```bash
258
- $ bin/rake test test/models/post_test.rb test_should_not_save_post_without_title
266
+ $ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title
259
267
  F
260
268
 
261
269
  Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s.
262
270
 
263
271
  1) Failure:
264
- test_should_not_save_post_without_title(PostTest) [test/models/post_test.rb:6]:
272
+ test_should_not_save_article_without_title(ArticleTest) [test/models/article_test.rb:6]:
265
273
  Failed assertion, no message given.
266
274
 
267
275
  1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
@@ -270,9 +278,9 @@ Failed assertion, no message given.
270
278
  In the output, `F` denotes a failure. You can see the corresponding trace shown under `1)` along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable, every assertion provides an optional message parameter, as shown here:
271
279
 
272
280
  ```ruby
273
- test "should not save post without title" do
274
- post = Post.new
275
- assert_not post.save, "Saved the post without a title"
281
+ test "should not save article without title" do
282
+ article = Article.new
283
+ assert_not article.save, "Saved the article without a title"
276
284
  end
277
285
  ```
278
286
 
@@ -280,14 +288,14 @@ Running this test shows the friendlier assertion message:
280
288
 
281
289
  ```bash
282
290
  1) Failure:
283
- test_should_not_save_post_without_title(PostTest) [test/models/post_test.rb:6]:
284
- Saved the post without a title
291
+ test_should_not_save_article_without_title(ArticleTest) [test/models/article_test.rb:6]:
292
+ Saved the article without a title
285
293
  ```
286
294
 
287
295
  Now to get this test to pass we can add a model level validation for the _title_ field.
288
296
 
289
297
  ```ruby
290
- class Post < ActiveRecord::Base
298
+ class Article < ActiveRecord::Base
291
299
  validates :title, presence: true
292
300
  end
293
301
  ```
@@ -295,7 +303,7 @@ end
295
303
  Now the test should pass. Let us verify by running the test again:
296
304
 
297
305
  ```bash
298
- $ bin/rake test test/models/post_test.rb test_should_not_save_post_without_title
306
+ $ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title
299
307
  .
300
308
 
301
309
  Finished tests in 0.047721s, 20.9551 tests/s, 20.9551 assertions/s.
@@ -320,15 +328,15 @@ end
320
328
  Now you can see even more output in the console from running the tests:
321
329
 
322
330
  ```bash
323
- $ bin/rake test test/models/post_test.rb test_should_report_error
331
+ $ bin/rake test test/models/article_test.rb test_should_report_error
324
332
  E
325
333
 
326
334
  Finished tests in 0.030974s, 32.2851 tests/s, 0.0000 assertions/s.
327
335
 
328
336
  1) Error:
329
- test_should_report_error(PostTest):
330
- NameError: undefined local variable or method `some_undefined_variable' for #<PostTest:0x007fe32e24afe0>
331
- test/models/post_test.rb:10:in `block in <class:PostTest>'
337
+ test_should_report_error(ArticleTest):
338
+ NameError: undefined local variable or method `some_undefined_variable' for #<ArticleTest:0x007fe32e24afe0>
339
+ test/models/article_test.rb:10:in `block in <class:ArticleTest>'
332
340
 
333
341
  1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
334
342
  ```
@@ -345,7 +353,7 @@ backtrace. simply set the `BACKTRACE` environment variable to enable this
345
353
  behavior:
346
354
 
347
355
  ```bash
348
- $ BACKTRACE=1 bin/rake test test/models/post_test.rb
356
+ $ BACKTRACE=1 bin/rake test test/models/article_test.rb
349
357
  ```
350
358
 
351
359
  ### What to Include in Your Unit Tests
@@ -357,7 +365,7 @@ Ideally, you would like to include a test for everything which could possibly br
357
365
  By now you've caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.
358
366
 
359
367
  There are a bunch of different types of assertions you can use.
360
- Here's an extract of the assertions you can use with `minitest`, the default testing library used by Rails. The `[msg]` parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.
368
+ Here's an extract of the assertions you can use with [`Minitest`](https://github.com/seattlerb/minitest), the default testing library used by Rails. The `[msg]` parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.
361
369
 
362
370
  | Assertion | Purpose |
363
371
  | ---------------------------------------------------------------- | ------- |
@@ -369,8 +377,12 @@ Here's an extract of the assertions you can use with `minitest`, the default tes
369
377
  | `assert_not_same( expected, actual, [msg] )` | Ensures that `expected.equal?(actual)` is false.|
370
378
  | `assert_nil( obj, [msg] )` | Ensures that `obj.nil?` is true.|
371
379
  | `assert_not_nil( obj, [msg] )` | Ensures that `obj.nil?` is false.|
380
+ | `assert_empty( obj, [msg] )` | Ensures that `obj` is `empty?`.|
381
+ | `assert_not_empty( obj, [msg] )` | Ensures that `obj` is not `empty?`.|
372
382
  | `assert_match( regexp, string, [msg] )` | Ensures that a string matches the regular expression.|
373
383
  | `assert_no_match( regexp, string, [msg] )` | Ensures that a string doesn't match the regular expression.|
384
+ | `assert_includes( collection, obj, [msg] )` | Ensures that `obj` is in `collection`.|
385
+ | `assert_not_includes( collection, obj, [msg] )` | Ensures that `obj` is not in `collection`.|
374
386
  | `assert_in_delta( expecting, actual, [delta], [msg] )` | Ensures that the numbers `expected` and `actual` are within `delta` of each other.|
375
387
  | `assert_not_in_delta( expecting, actual, [delta], [msg] )` | Ensures that the numbers `expected` and `actual` are not within `delta` of each other.|
376
388
  | `assert_throws( symbol, [msg] ) { block }` | Ensures that the given block throws the symbol.|
@@ -384,16 +396,20 @@ Here's an extract of the assertions you can use with `minitest`, the default tes
384
396
  | `assert_not_respond_to( obj, symbol, [msg] )` | Ensures that `obj` does not respond to `symbol`.|
385
397
  | `assert_operator( obj1, operator, [obj2], [msg] )` | Ensures that `obj1.operator(obj2)` is true.|
386
398
  | `assert_not_operator( obj1, operator, [obj2], [msg] )` | Ensures that `obj1.operator(obj2)` is false.|
399
+ | `assert_predicate ( obj, predicate, [msg] )` | Ensures that `obj.predicate` is true, e.g. `assert_predicate str, :empty?`|
400
+ | `assert_not_predicate ( obj, predicate, [msg] )` | Ensures that `obj.predicate` is false, e.g. `assert_not_predicate str, :empty?`|
387
401
  | `assert_send( array, [msg] )` | Ensures that executing the method listed in `array[1]` on the object in `array[0]` with the parameters of `array[2 and up]` is true. This one is weird eh?|
388
402
  | `flunk( [msg] )` | Ensures failure. This is useful to explicitly mark a test that isn't finished yet.|
389
403
 
404
+ The above are subset of assertions that minitest supports. For an exhaustive & more up-to-date list, please check [Minitest API documentation](http://docs.seattlerb.org/minitest/), specifically [`Minitest::Assertions`](http://docs.seattlerb.org/minitest/Minitest/Assertions.html)
405
+
390
406
  Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that's exactly what Rails does. It includes some specialized assertions to make your life easier.
391
407
 
392
408
  NOTE: Creating your own assertions is an advanced topic that we won't cover in this tutorial.
393
409
 
394
410
  ### Rails Specific Assertions
395
411
 
396
- Rails adds some custom assertions of its own to the `test/unit` framework:
412
+ Rails adds some custom assertions of its own to the `minitest` framework:
397
413
 
398
414
  | Assertion | Purpose |
399
415
  | --------------------------------------------------------------------------------- | ------- |
@@ -422,26 +438,26 @@ You should test for things such as:
422
438
  * was the correct object stored in the response template?
423
439
  * was the appropriate message displayed to the user in the view?
424
440
 
425
- Now that we have used Rails scaffold generator for our `Post` resource, it has already created the controller code and tests. You can take look at the file `posts_controller_test.rb` in the `test/controllers` directory.
441
+ Now that we have used Rails scaffold generator for our `Article` resource, it has already created the controller code and tests. You can take look at the file `articles_controller_test.rb` in the `test/controllers` directory.
426
442
 
427
- Let me take you through one such test, `test_should_get_index` from the file `posts_controller_test.rb`.
443
+ Let me take you through one such test, `test_should_get_index` from the file `articles_controller_test.rb`.
428
444
 
429
445
  ```ruby
430
- class PostsControllerTest < ActionController::TestCase
446
+ class ArticlesControllerTest < ActionController::TestCase
431
447
  test "should get index" do
432
448
  get :index
433
449
  assert_response :success
434
- assert_not_nil assigns(:posts)
450
+ assert_not_nil assigns(:articles)
435
451
  end
436
452
  end
437
453
  ```
438
454
 
439
- In the `test_should_get_index` test, Rails simulates a request on the action called `index`, making sure the request was successful and also ensuring that it assigns a valid `posts` instance variable.
455
+ In the `test_should_get_index` test, Rails simulates a request on the action called `index`, making sure the request was successful and also ensuring that it assigns a valid `articles` instance variable.
440
456
 
441
457
  The `get` method kicks off the web request and populates the results into the response. It accepts 4 arguments:
442
458
 
443
459
  * The action of the controller you are requesting. This can be in the form of a string or a symbol.
444
- * An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
460
+ * An optional hash of request parameters to pass into the action (eg. query string parameters or article variables).
445
461
  * An optional hash of session variables to pass along with the request.
446
462
  * An optional hash of flash values.
447
463
 
@@ -457,17 +473,17 @@ Another example: Calling the `:view` action, passing an `id` of 12 as the `param
457
473
  get(:view, {'id' => '12'}, nil, {'message' => 'booya!'})
458
474
  ```
459
475
 
460
- NOTE: If you try running `test_should_create_post` test from `posts_controller_test.rb` it will fail on account of the newly added model level validation and rightly so.
476
+ NOTE: If you try running `test_should_create_article` test from `articles_controller_test.rb` it will fail on account of the newly added model level validation and rightly so.
461
477
 
462
- Let us modify `test_should_create_post` test in `posts_controller_test.rb` so that all our test pass:
478
+ Let us modify `test_should_create_article` test in `articles_controller_test.rb` so that all our test pass:
463
479
 
464
480
  ```ruby
465
- test "should create post" do
466
- assert_difference('Post.count') do
467
- post :create, post: {title: 'Some title'}
481
+ test "should create article" do
482
+ assert_difference('Article.count') do
483
+ post :create, article: {title: 'Some title'}
468
484
  end
469
485
 
470
- assert_redirected_to post_path(assigns(:post))
486
+ assert_redirected_to article_path(assigns(:article))
471
487
  end
472
488
  ```
473
489
 
@@ -576,12 +592,12 @@ is the correct way to assert for the layout when the view renders a partial with
576
592
  Here's another example that uses `flash`, `assert_redirected_to`, and `assert_difference`:
577
593
 
578
594
  ```ruby
579
- test "should create post" do
580
- assert_difference('Post.count') do
581
- post :create, post: {title: 'Hi', body: 'This is my first post.'}
595
+ test "should create article" do
596
+ assert_difference('Article.count') do
597
+ post :create, article: {title: 'Hi', body: 'This is my first article.'}
582
598
  end
583
- assert_redirected_to post_path(assigns(:post))
584
- assert_equal 'Post was successfully created.', flash[:notice]
599
+ assert_redirected_to article_path(assigns(:article))
600
+ assert_equal 'Article was successfully created.', flash[:notice]
585
601
  end
586
602
  ```
587
603
 
@@ -589,13 +605,13 @@ end
589
605
 
590
606
  Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The `assert_select` assertion allows you to do this by using a simple yet powerful syntax.
591
607
 
592
- NOTE: You may find references to `assert_tag` in other documentation, but this is now deprecated in favor of `assert_select`.
608
+ NOTE: You may find references to `assert_tag` in other documentation. This has been removed in 4.2. Use `assert_select` instead.
593
609
 
594
610
  There are two forms of `assert_select`:
595
611
 
596
- `assert_select(selector, [equality], [message])` ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an `HTML::Selector` object.
612
+ `assert_select(selector, [equality], [message])` ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String) or an expression with substitution values.
597
613
 
598
- `assert_select(element, selector, [equality], [message])` ensures that the equality condition is met on all the selected elements through the selector starting from the _element_ (instance of `HTML::Node`) and its descendants.
614
+ `assert_select(element, selector, [equality], [message])` ensures that the equality condition is met on all the selected elements through the selector starting from the _element_ (instance of `Nokogiri::XML::Node` or `Nokogiri::XML::NodeSet`) and its descendants.
599
615
 
600
616
  For example, you could verify the contents on the title element in your response with:
601
617
 
@@ -625,7 +641,7 @@ assert_select "ol" do
625
641
  end
626
642
  ```
627
643
 
628
- The `assert_select` assertion is quite powerful. For more advanced usage, refer to its [documentation](http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html).
644
+ The `assert_select` assertion is quite powerful. For more advanced usage, refer to its [documentation](https://github.com/rails/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb).
629
645
 
630
646
  #### Additional View-Based Assertions
631
647
 
@@ -710,9 +726,9 @@ class UserFlowsTest < ActionDispatch::IntegrationTest
710
726
  assert_equal 'Welcome david!', flash[:notice]
711
727
 
712
728
  https!(false)
713
- get "/posts/all"
729
+ get "/articles/all"
714
730
  assert_response :success
715
- assert assigns(:products)
731
+ assert assigns(:articles)
716
732
  end
717
733
  end
718
734
  ```
@@ -769,76 +785,71 @@ end
769
785
  Rake Tasks for Running your Tests
770
786
  ---------------------------------
771
787
 
772
- You don't need to set up and run your tests by hand on a test-by-test basis.
773
- Rails comes with a number of commands to help in testing.
774
- The table below lists all commands that come along in the default Rakefile
775
- when you initiate a Rails project.
788
+ Rails comes with a number of built-in rake tasks to help with testing. The
789
+ table below lists the commands included in the default Rakefile when a Rails
790
+ project is created.
776
791
 
777
792
  | Tasks | Description |
778
793
  | ----------------------- | ----------- |
779
- | `rake test` | Runs all unit, functional and integration tests. You can also simply run `rake` as Rails will run all the tests by default |
794
+ | `rake test` | Runs all tests in the `test` folder. You can also simply run `rake` as Rails will run all the tests by default |
780
795
  | `rake test:controllers` | Runs all the controller tests from `test/controllers` |
781
796
  | `rake test:functionals` | Runs all the functional tests from `test/controllers`, `test/mailers`, and `test/functional` |
782
797
  | `rake test:helpers` | Runs all the helper tests from `test/helpers` |
783
798
  | `rake test:integration` | Runs all the integration tests from `test/integration` |
799
+ | `rake test:jobs` | Runs all the job tests from `test/jobs` |
784
800
  | `rake test:mailers` | Runs all the mailer tests from `test/mailers` |
785
801
  | `rake test:models` | Runs all the model tests from `test/models` |
786
802
  | `rake test:units` | Runs all the unit tests from `test/models`, `test/helpers`, and `test/unit` |
787
- | `rake test:all` | Runs all tests quickly by merging all types and not resetting db |
788
- | `rake test:all:db` | Runs all tests quickly by merging all types and resetting db |
803
+ | `rake test:db` | Runs all tests in the `test` folder and resets the db |
789
804
 
790
805
 
791
- Brief Note About `MiniTest`
806
+ A Brief Note About Minitest
792
807
  -----------------------------
793
808
 
794
- Ruby ships with a boat load of libraries. Ruby 1.8 provides `Test::Unit`, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in `Test::Unit::Assertions`. The class `ActiveSupport::TestCase` which we have been using in our unit and functional tests extends `Test::Unit::TestCase`, allowing
795
- us to use all of the basic assertions in our tests.
796
-
797
- Ruby 1.9 introduced `MiniTest`, an updated version of `Test::Unit` which provides a backwards compatible API for `Test::Unit`. You could also use `MiniTest` in Ruby 1.8 by installing the `minitest` gem.
809
+ Ruby ships with a vast Standard Library for all common use-cases including testing. Since version 1.9, Ruby provides `Minitest`, a framework for testing. All the basic assertions such as `assert_equal` discussed above are actually defined in `Minitest::Assertions`. The classes `ActiveSupport::TestCase`, `ActionController::TestCase`, `ActionMailer::TestCase`, `ActionView::TestCase` and `ActionDispatch::IntegrationTest` - which we have been inheriting in our test classes - include `Minitest::Assertions`, allowing us to use all of the basic assertions in our tests.
798
810
 
799
- NOTE: For more information on `Test::Unit`, refer to [test/unit Documentation](http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/)
800
- For more information on `MiniTest`, refer to [Minitest](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/minitest/unit/rdoc/)
811
+ NOTE: For more information on `Minitest`, refer to [Minitest](http://ruby-doc.org/stdlib-2.1.0/libdoc/minitest/rdoc/MiniTest.html)
801
812
 
802
813
  Setup and Teardown
803
814
  ------------------
804
815
 
805
- If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let's take note of this by looking at an example for our functional test in `Posts` controller:
816
+ If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let's take note of this by looking at an example for our functional test in `Articles` controller:
806
817
 
807
818
  ```ruby
808
819
  require 'test_helper'
809
820
 
810
- class PostsControllerTest < ActionController::TestCase
821
+ class ArticlesControllerTest < ActionController::TestCase
811
822
 
812
823
  # called before every single test
813
824
  def setup
814
- @post = posts(:one)
825
+ @article = articles(:one)
815
826
  end
816
827
 
817
828
  # called after every single test
818
829
  def teardown
819
- # as we are re-initializing @post before every test
830
+ # as we are re-initializing @article before every test
820
831
  # setting it to nil here is not essential but I hope
821
832
  # you understand how you can use the teardown method
822
- @post = nil
833
+ @article = nil
823
834
  end
824
835
 
825
- test "should show post" do
826
- get :show, id: @post.id
836
+ test "should show article" do
837
+ get :show, id: @article.id
827
838
  assert_response :success
828
839
  end
829
840
 
830
- test "should destroy post" do
831
- assert_difference('Post.count', -1) do
832
- delete :destroy, id: @post.id
841
+ test "should destroy article" do
842
+ assert_difference('Article.count', -1) do
843
+ delete :destroy, id: @article.id
833
844
  end
834
845
 
835
- assert_redirected_to posts_path
846
+ assert_redirected_to articles_path
836
847
  end
837
848
 
838
849
  end
839
850
  ```
840
851
 
841
- Above, the `setup` method is called before each test and so `@post` is available for each of the tests. Rails implements `setup` and `teardown` as `ActiveSupport::Callbacks`. Which essentially means you need not only use `setup` and `teardown` as methods in your tests. You could specify them by using:
852
+ Above, the `setup` method is called before each test and so `@article` is available for each of the tests. Rails implements `setup` and `teardown` as `ActiveSupport::Callbacks`. Which essentially means you need not only use `setup` and `teardown` as methods in your tests. You could specify them by using:
842
853
 
843
854
  * a block
844
855
  * a method (like in the earlier example)
@@ -850,38 +861,38 @@ Let's see the earlier example by specifying `setup` callback by specifying a met
850
861
  ```ruby
851
862
  require 'test_helper'
852
863
 
853
- class PostsControllerTest < ActionController::TestCase
864
+ class ArticlesControllerTest < ActionController::TestCase
854
865
 
855
866
  # called before every single test
856
- setup :initialize_post
867
+ setup :initialize_article
857
868
 
858
869
  # called after every single test
859
870
  def teardown
860
- @post = nil
871
+ @article = nil
861
872
  end
862
873
 
863
- test "should show post" do
864
- get :show, id: @post.id
874
+ test "should show article" do
875
+ get :show, id: @article.id
865
876
  assert_response :success
866
877
  end
867
878
 
868
- test "should update post" do
869
- patch :update, id: @post.id, post: {}
870
- assert_redirected_to post_path(assigns(:post))
879
+ test "should update article" do
880
+ patch :update, id: @article.id, article: {}
881
+ assert_redirected_to article_path(assigns(:article))
871
882
  end
872
883
 
873
- test "should destroy post" do
874
- assert_difference('Post.count', -1) do
875
- delete :destroy, id: @post.id
884
+ test "should destroy article" do
885
+ assert_difference('Article.count', -1) do
886
+ delete :destroy, id: @article.id
876
887
  end
877
888
 
878
- assert_redirected_to posts_path
889
+ assert_redirected_to articles_path
879
890
  end
880
891
 
881
892
  private
882
893
 
883
- def initialize_post
884
- @post = posts(:one)
894
+ def initialize_article
895
+ @article = articles(:one)
885
896
  end
886
897
  end
887
898
  ```
@@ -889,11 +900,17 @@ end
889
900
  Testing Routes
890
901
  --------------
891
902
 
892
- Like everything else in your Rails application, it is recommended that you test your routes. An example test for a route in the default `show` action of `Posts` controller above should look like:
903
+ Like everything else in your Rails application, it is recommended that you test your routes. Below are example tests for the routes of default `show` and `create` action of `Articles` controller above and it should look like:
893
904
 
894
905
  ```ruby
895
- test "should route to post" do
896
- assert_routing '/posts/1', {controller: "posts", action: "show", id: "1"}
906
+ class ArticleRoutesTest < ActionController::TestCase
907
+ test "should route to article" do
908
+ assert_routing '/articles/1', { controller: "articles", action: "show", id: "1" }
909
+ end
910
+
911
+ test "should route to create article" do
912
+ assert_routing({ method: 'post', path: '/articles' }, { controller: "articles", action: "create" })
913
+ end
897
914
  end
898
915
  ```
899
916
 
@@ -937,7 +954,7 @@ class UserMailerTest < ActionMailer::TestCase
937
954
  test "invite" do
938
955
  # Send the email, then test that it got queued
939
956
  email = UserMailer.create_invite('me@example.com',
940
- 'friend@example.com', Time.now).deliver
957
+ 'friend@example.com', Time.now).deliver_now
941
958
  assert_not ActionMailer::Base.deliveries.empty?
942
959
 
943
960
  # Test the body of the sent email contains what we expect it to
@@ -992,7 +1009,7 @@ class UserControllerTest < ActionController::TestCase
992
1009
 
993
1010
  assert_equal "You have been invited by me@example.com", invite_email.subject
994
1011
  assert_equal 'friend@example.com', invite_email.to[0]
995
- assert_match(/Hi friend@example.com/, invite_email.body)
1012
+ assert_match(/Hi friend@example.com/, invite_email.body.to_s)
996
1013
  end
997
1014
  end
998
1015
  ```
@@ -1002,17 +1019,9 @@ Testing helpers
1002
1019
 
1003
1020
  In order to test helpers, all you need to do is check that the output of the
1004
1021
  helper method matches what you'd expect. Tests related to the helpers are
1005
- located under the `test/helpers` directory. Rails provides a generator which
1006
- generates both the helper and the test file:
1007
-
1008
- ```bash
1009
- $ bin/rails generate helper User
1010
- create app/helpers/user_helper.rb
1011
- invoke test_unit
1012
- create test/helpers/user_helper_test.rb
1013
- ```
1022
+ located under the `test/helpers` directory.
1014
1023
 
1015
- The generated test file contains the following code:
1024
+ A helper test looks like so:
1016
1025
 
1017
1026
  ```ruby
1018
1027
  require 'test_helper'
@@ -1038,15 +1047,77 @@ end
1038
1047
  Moreover, since the test class extends from `ActionView::TestCase`, you have
1039
1048
  access to Rails' helper methods such as `link_to` or `pluralize`.
1040
1049
 
1050
+ Testing Jobs
1051
+ ------------
1052
+
1053
+ Since your custom jobs can be queued at different levels inside your application,
1054
+ you'll need to test both jobs themselves (their behavior when they get enqueued)
1055
+ and that other entities correctly enqueue them.
1056
+
1057
+ ### A Basic Test Case
1058
+
1059
+ By default, when you generate a job, an associated test will be generated as well
1060
+ under the `test/jobs` directory. Here's an example test with a billing job:
1061
+
1062
+ ```ruby
1063
+ require 'test_helper'
1064
+
1065
+ class BillingJobTest < ActiveJob::TestCase
1066
+ test 'that account is charged' do
1067
+ BillingJob.perform_now(account, product)
1068
+ assert account.reload.charged_for?(product)
1069
+ end
1070
+ end
1071
+ ```
1072
+
1073
+ This test is pretty simple and only asserts that the job get the work done
1074
+ as expected.
1075
+
1076
+ By default, `ActiveJob::TestCase` will set the queue adapter to `:test` so that
1077
+ your jobs are performed inline. It will also ensure that all previously performed
1078
+ and enqueued jobs are cleared before any test run so you can safely assume that
1079
+ no jobs have already been executed in the scope of each test.
1080
+
1081
+ ### Custom Assertions And Testing Jobs Inside Other Components
1082
+
1083
+ Active Job ships with a bunch of custom assertions that can be used to lessen
1084
+ the verbosity of tests:
1085
+
1086
+ | Assertion | Purpose |
1087
+ | -------------------------------------- | ------- |
1088
+ | `assert_enqueued_jobs(number)` | Asserts that the number of enqueued jobs matches the given number. |
1089
+ | `assert_performed_jobs(number)` | Asserts that the number of performed jobs matches the given number. |
1090
+ | `assert_no_enqueued_jobs { ... }` | Asserts that no jobs have been enqueued. |
1091
+ | `assert_no_performed_jobs { ... }` | Asserts that no jobs have been performed. |
1092
+ | `assert_enqueued_with([args]) { ... }` | Asserts that the job passed in the block has been enqueued with the given arguments. |
1093
+ | `assert_performed_with([args]) { ... }`| Asserts that the job passed in the block has been performed with the given arguments. |
1094
+
1095
+ It's a good practice to ensure that your jobs correctly get enqueued or performed
1096
+ wherever you invoke them (e.g. inside your controllers). This is precisely where
1097
+ the custom assertions provided by Active Job are pretty useful. For instance,
1098
+ within a model:
1099
+
1100
+ ```ruby
1101
+ require 'test_helper'
1102
+
1103
+ class ProductTest < ActiveSupport::TestCase
1104
+ test 'billing job scheduling' do
1105
+ assert_enqueued_with(job: BillingJob) do
1106
+ product.charge(account)
1107
+ end
1108
+ end
1109
+ end
1110
+ ```
1111
+
1041
1112
  Other Testing Approaches
1042
1113
  ------------------------
1043
1114
 
1044
- The built-in `test/unit` based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:
1115
+ The built-in `minitest` based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:
1045
1116
 
1046
1117
  * [NullDB](http://avdi.org/projects/nulldb/), a way to speed up testing by avoiding database use.
1047
1118
  * [Factory Girl](https://github.com/thoughtbot/factory_girl/tree/master), a replacement for fixtures.
1048
- * [Machinist](https://github.com/notahat/machinist/tree/master), another replacement for fixtures.
1049
1119
  * [Fixture Builder](https://github.com/rdy/fixture_builder), a tool that compiles Ruby factories into fixtures before a test run.
1050
1120
  * [MiniTest::Spec Rails](https://github.com/metaskills/minitest-spec-rails), use the MiniTest::Spec DSL within your rails tests.
1051
1121
  * [Shoulda](http://www.thoughtbot.com/projects/shoulda), an extension to `test/unit` with additional helpers, macros, and assertions.
1052
1122
  * [RSpec](http://relishapp.com/rspec), a behavior-driven development framework
1123
+ * [Capybara](http://jnicklas.github.com/capybara/), Acceptance test framework for web applications