提交 8ac458ad 编写于 作者: V Vijay Dev

Merge branch 'master' of github.com:rails/docrails

...@@ -80,7 +80,7 @@ def form_tag(url_for_options = {}, options = {}, &block) ...@@ -80,7 +80,7 @@ def form_tag(url_for_options = {}, options = {}, &block)
# associated records. <tt>option_tags</tt> is a string containing the option tags for the select box. # associated records. <tt>option_tags</tt> is a string containing the option tags for the select box.
# #
# ==== Options # ==== Options
# * <tt>:multiple</tt> - If set to true the selection will allow multiple choices. # * <tt>:multiple</tt> - If set to true, the selection will allow multiple choices.
# * <tt>:disabled</tt> - If set to true, the user will not be able to use this input. # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
# * <tt>:include_blank</tt> - If set to true, an empty option will be created. If set to a string, the string will be used as the option's content and the value will be empty. # * <tt>:include_blank</tt> - If set to true, an empty option will be created. If set to a string, the string will be used as the option's content and the value will be empty.
# * <tt>:prompt</tt> - Create a prompt option with blank value and the text asking user to select something. # * <tt>:prompt</tt> - Create a prompt option with blank value and the text asking user to select something.
......
...@@ -7,7 +7,7 @@ class Object ...@@ -7,7 +7,7 @@ class Object
# provided. Each method called on the block variable must take an options # provided. Each method called on the block variable must take an options
# hash as its final argument. # hash as its final argument.
# #
# Without <tt>with_options></tt>, this code contains duplication: # Without <tt>with_options</tt>, this code contains duplication:
# #
# class Account < ActiveRecord::Base # class Account < ActiveRecord::Base
# has_many :customers, dependent: :destroy # has_many :customers, dependent: :destroy
......
...@@ -667,7 +667,7 @@ You may notice in the above code that we're using `render xml: @users`, not `ren ...@@ -667,7 +667,7 @@ You may notice in the above code that we're using `render xml: @users`, not `ren
Filters Filters
------- -------
Filters are methods that are run before, after or "around" a controller action. Filters are methods that are run "before", "after" or "around" a controller action.
Filters are inherited, so if you set a filter on `ApplicationController`, it will be run on every controller in your application. Filters are inherited, so if you set a filter on `ApplicationController`, it will be run on every controller in your application.
......
...@@ -529,7 +529,7 @@ Client.order("orders_count ASC, created_at DESC") ...@@ -529,7 +529,7 @@ Client.order("orders_count ASC, created_at DESC")
Client.order("orders_count ASC", "created_at DESC") Client.order("orders_count ASC", "created_at DESC")
``` ```
If you want to call `order` multiple times e.g. in different context, new order will append previous one If you want to call `order` multiple times e.g. in different context, new order will append previous one:
```ruby ```ruby
Client.order("orders_count ASC").order("created_at DESC") Client.order("orders_count ASC").order("created_at DESC")
......
...@@ -368,7 +368,7 @@ called `Blorgh::ArticlesController` (at ...@@ -368,7 +368,7 @@ called `Blorgh::ArticlesController` (at
`app/controllers/blorgh/articles_controller.rb`) and its related views at `app/controllers/blorgh/articles_controller.rb`) and its related views at
`app/views/blorgh/articles`. This generator also generates a test for the `app/views/blorgh/articles`. This generator also generates a test for the
controller (`test/controllers/blorgh/articles_controller_test.rb`) and a helper controller (`test/controllers/blorgh/articles_controller_test.rb`) and a helper
(`app/helpers/blorgh/articles_controller.rb`). (`app/helpers/blorgh/articles_helper.rb`).
Everything this generator has created is neatly namespaced. The controller's Everything this generator has created is neatly namespaced. The controller's
class is defined within the `Blorgh` module: class is defined within the `Blorgh` module:
...@@ -822,11 +822,9 @@ Notice that only _one_ migration was copied over here. This is because the first ...@@ -822,11 +822,9 @@ Notice that only _one_ migration was copied over here. This is because the first
two migrations were copied over the first time this command was run. two migrations were copied over the first time this command was run.
``` ```
NOTE Migration [timestamp]_create_blorgh_articles.rb from blorgh has been NOTE Migration [timestamp]_create_blorgh_articles.rb from blorgh has been skipped. Migration with the same name already exists.
skipped. Migration with the same name already exists. NOTE Migration NOTE Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists.
[timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration Copied migration [timestamp]_add_author_id_to_blorgh_articles.rb from blorgh
with the same name already exists. Copied migration
[timestamp]_add_author_id_to_blorgh_articles.rb from blorgh
``` ```
Run the migration using: Run the migration using:
...@@ -1192,7 +1190,7 @@ end ...@@ -1192,7 +1190,7 @@ end
``` ```
```ruby ```ruby
# Blorgh/lib/concerns/models/article # Blorgh/lib/concerns/models/article.rb
module Blorgh::Concerns::Models::Article module Blorgh::Concerns::Models::Article
extend ActiveSupport::Concern extend ActiveSupport::Concern
......
...@@ -123,8 +123,6 @@ Content-Type: */*; charset=utf-8 ...@@ -123,8 +123,6 @@ Content-Type: */*; charset=utf-8
X-Runtime: 0.014297 X-Runtime: 0.014297
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
Cache-Control: no-cache Cache-Control: no-cache
$
``` ```
We see there is an empty response (no data after the `Cache-Control` line), but the request was successful because Rails has set the response to 200 OK. You can set the `:status` option on render to change this response. Rendering nothing can be useful for Ajax requests where all you want to send back to the browser is an acknowledgment that the request was completed. We see there is an empty response (no data after the `Cache-Control` line), but the request was successful because Rails has set the response to 200 OK. You can set the `:status` option on render to change this response. Rendering nothing can be useful for Ajax requests where all you want to send back to the browser is an acknowledgment that the request was completed.
......
...@@ -141,32 +141,23 @@ users(:david).id ...@@ -141,32 +141,23 @@ users(:david).id
email(david.partner.email, david.location_tonight) email(david.partner.email, david.location_tonight)
``` ```
### Rake Tasks for Running your Tests ### Console Tasks for Running your Tests
Rails comes with a number of built-in rake tasks to help with testing. The Rails comes with a CLI command to run tests.
table below lists the commands included in the default Rakefile when a Rails Here are some examples of how to use it:
project is created.
```bash
| Tasks | Description | $ bin/rails test # run all tests in the `test` directory
| ----------------------- | ----------- | $ bin/rails test test/controllers # run all tests from specific directory
| `rake test` | Runs all tests in the `test` directory. You can also run `rake` and Rails will run all tests by default | $ bin/rails test test/models/post_test.rb # run specific test
| `rake test:controllers` | Runs all the controller tests from `test/controllers` | $ bin/rails test test/models/post_test.rb:44 # run specific test and line
| `rake test:functionals` | Runs all the functional tests from `test/controllers`, `test/mailers`, and `test/functional` | ```
| `rake test:helpers` | Runs all the helper tests from `test/helpers` |
| `rake test:integration` | Runs all the integration tests from `test/integration` |
| `rake test:jobs` | Runs all the job tests from `test/jobs` |
| `rake test:mailers` | Runs all the mailer tests from `test/mailers` |
| `rake test:models` | Runs all the model tests from `test/models` |
| `rake test:units` | Runs all the unit tests from `test/models`, `test/helpers`, and `test/unit` |
| `rake test:db` | Runs all tests in the `test` directory and resets the db |
We will cover each of types Rails tests listed above in this guide. We will cover each of types Rails tests listed above in this guide.
Model Testing Model Testing
------------------------ ------------------------
In Rails, unit tests are what you write to test your models.
For this guide we will be using the application we built in the [Getting Started with Rails](getting_started.html) guide. For this guide we will be using the application we built in the [Getting Started with Rails](getting_started.html) guide.
If you remember when you used the `rails generate scaffold` command from earlier. We created our first resource among other things it created a test stub in the `test/models` directory: If you remember when you used the `rails generate scaffold` command from earlier. We created our first resource among other things it created a test stub in the `test/models` directory:
...@@ -259,10 +250,10 @@ be rebuilt. This can be done by executing `bin/rake db:test:prepare`. ...@@ -259,10 +250,10 @@ be rebuilt. This can be done by executing `bin/rake db:test:prepare`.
### Running Tests ### Running Tests
Running a test is as simple as invoking the file containing the test cases through `rake test` command. Running a test is as simple as invoking the file containing the test cases through `rails test` command.
```bash ```bash
$ bin/rake test test/models/article_test.rb $ bin/rails test test/models/article_test.rb
. .
Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s. Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s.
...@@ -275,7 +266,7 @@ This will run all test methods from the test case. ...@@ -275,7 +266,7 @@ This will run all test methods from the test case.
You can also run a particular test method from the test case by running the test and providing the `test method name`. You can also run a particular test method from the test case by running the test and providing the `test method name`.
```bash ```bash
$ bin/rake test test/models/article_test.rb test_the_truth $ bin/rails test test/models/article_test.rb test_the_truth
. .
Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s. Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s.
...@@ -296,10 +287,10 @@ test "should not save article without title" do ...@@ -296,10 +287,10 @@ test "should not save article without title" do
end end
``` ```
Let us run this newly added test. Let us run this newly added test (where `6` is the number of line where the test is defined).
```bash ```bash
$ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title $ bin/rails test test/models/article_test.rb:6
F F
Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s. Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s.
...@@ -339,7 +330,7 @@ end ...@@ -339,7 +330,7 @@ end
Now the test should pass. Let us verify by running the test again: Now the test should pass. Let us verify by running the test again:
```bash ```bash
$ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title $ bin/rails test test/models/article_test.rb:6
. .
Finished tests in 0.047721s, 20.9551 tests/s, 20.9551 assertions/s. Finished tests in 0.047721s, 20.9551 tests/s, 20.9551 assertions/s.
...@@ -368,7 +359,7 @@ end ...@@ -368,7 +359,7 @@ end
Now you can see even more output in the console from running the tests: Now you can see even more output in the console from running the tests:
```bash ```bash
$ bin/rake test test/models/article_test.rb test_should_report_error $ bin/rails test test/models/article_test.rb
E E
Finished tests in 0.030974s, 32.2851 tests/s, 0.0000 assertions/s. Finished tests in 0.030974s, 32.2851 tests/s, 0.0000 assertions/s.
...@@ -393,11 +384,10 @@ When a test fails you are presented with the corresponding backtrace. By default ...@@ -393,11 +384,10 @@ When a test fails you are presented with the corresponding backtrace. By default
Rails filters that backtrace and will only print lines relevant to your Rails filters that backtrace and will only print lines relevant to your
application. This eliminates the framework noise and helps to focus on your application. This eliminates the framework noise and helps to focus on your
code. However there are situations when you want to see the full code. However there are situations when you want to see the full
backtrace. simply set the `BACKTRACE` environment variable to enable this backtrace. Simply set the `-b` (or `--backtrace`) argument to enable this behavior:
behavior:
```bash ```bash
$ BACKTRACE=1 bin/rake test test/models/article_test.rb $ bin/rails test -b test/models/article_test.rb
``` ```
If we want this test to pass we can modify it to use `assert_raises` like so: If we want this test to pass we can modify it to use `assert_raises` like so:
...@@ -504,13 +494,13 @@ All the keyword arguments are optional. ...@@ -504,13 +494,13 @@ All the keyword arguments are optional.
Example: Calling the `:show` action, passing an `id` of 12 as the `params` and setting a `user_id` of 5 in the session: Example: Calling the `:show` action, passing an `id` of 12 as the `params` and setting a `user_id` of 5 in the session:
```ruby ```ruby
get(:show, params: { 'id' => "12" }, session: { 'user_id' => 5 }) get(:show, params: { id: 12 }, session: { user_id: 5 })
``` ```
Another example: Calling the `:view` action, passing an `id` of 12 as the `params`, this time with no session, but with a flash message. Another example: Calling the `:view` action, passing an `id` of 12 as the `params`, this time with no session, but with a flash message.
```ruby ```ruby
get(:view, params: { 'id' => '12' }, flash: { 'message' => 'booya!' }) get(:view, params: { id: 12 }, flash: { message: 'booya!' })
``` ```
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. 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.
...@@ -666,7 +656,7 @@ end ...@@ -666,7 +656,7 @@ end
If we run our test now, we should see a failure: If we run our test now, we should see a failure:
```bash ```bash
$ bin/rake test test/controllers/articles_controller_test.rb test_should_create_article $ bin/rails test test/controllers/articles_controller_test.rb test_should_create_article
Run options: -n test_should_create_article --seed 32266 Run options: -n test_should_create_article --seed 32266
# Running: # Running:
...@@ -704,7 +694,7 @@ end ...@@ -704,7 +694,7 @@ end
Now if we run our tests, we should see it pass: Now if we run our tests, we should see it pass:
```bash ```bash
$ bin/rake test test/controllers/articles_controller_test.rb test_should_create_article $ bin/rails test test/controllers/articles_controller_test.rb test_should_create_article
Run options: -n test_should_create_article --seed 18981 Run options: -n test_should_create_article --seed 18981
# Running: # Running:
...@@ -852,7 +842,7 @@ end ...@@ -852,7 +842,7 @@ end
I've added this file here `test/controllers/articles_routes_test.rb` and if we run the test we should see: I've added this file here `test/controllers/articles_routes_test.rb` and if we run the test we should see:
```bash ```bash
$ bin/rake test test/controllers/articles_routes_test.rb $ bin/rails test test/controllers/articles_routes_test.rb
# Running: # Running:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册