mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
0d16c9d1a5
commit
09d2623783
1 changed files with 21 additions and 28 deletions
|
@ -141,24 +141,18 @@ users(:david).id
|
|||
email(david.girlfriend.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
|
||||
table below lists the commands included in the default Rakefile when a Rails
|
||||
project is created.
|
||||
Rails comes with a CLI command to run tests.
|
||||
Here are some examples how to use it:
|
||||
|
||||
| Tasks | Description |
|
||||
| ----------------------- | ----------- |
|
||||
| `rake test` | Runs all tests in the `test` directory. You can also run `rake` and Rails will run all tests by default |
|
||||
| `rake test:controllers` | Runs all the controller tests from `test/controllers` |
|
||||
| `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 |
|
||||
```bash
|
||||
$ bin/rails test # run all tests in the `test` directory
|
||||
$ bin/rails test controllers # run all controller tests in the `test/controllers` directory
|
||||
$ bin/rails test test/controllers # run all tests from specific directory
|
||||
$ bin/rails test test/models/post_test.rb # run specific test
|
||||
$ bin/rails test test/models/post_test.rb:44 # run specific test and line
|
||||
```
|
||||
|
||||
We will cover each of types Rails tests listed above in this guide.
|
||||
|
||||
|
@ -259,10 +253,10 @@ be rebuilt. This can be done by executing `bin/rake db:test:prepare`.
|
|||
|
||||
### 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
|
||||
$ 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.
|
||||
|
@ -275,7 +269,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`.
|
||||
|
||||
```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.
|
||||
|
@ -299,7 +293,7 @@ end
|
|||
Let us run this newly added test.
|
||||
|
||||
```bash
|
||||
$ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title
|
||||
$ bin/rails test test/models/article_test.rb test_should_not_save_article_without_title
|
||||
F
|
||||
|
||||
Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s.
|
||||
|
@ -339,7 +333,7 @@ end
|
|||
Now the test should pass. Let us verify by running the test again:
|
||||
|
||||
```bash
|
||||
$ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title
|
||||
$ bin/rails test test/models/article_test.rb test_should_not_save_article_without_title
|
||||
.
|
||||
|
||||
Finished tests in 0.047721s, 20.9551 tests/s, 20.9551 assertions/s.
|
||||
|
@ -368,7 +362,7 @@ end
|
|||
Now you can see even more output in the console from running the tests:
|
||||
|
||||
```bash
|
||||
$ bin/rake test test/models/article_test.rb test_should_report_error
|
||||
$ bin/rails test test/models/article_test.rb test_should_report_error
|
||||
E
|
||||
|
||||
Finished tests in 0.030974s, 32.2851 tests/s, 0.0000 assertions/s.
|
||||
|
@ -393,11 +387,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
|
||||
application. This eliminates the framework noise and helps to focus on your
|
||||
code. However there are situations when you want to see the full
|
||||
backtrace. simply set the `BACKTRACE` environment variable to enable this
|
||||
behavior:
|
||||
backtrace. Simply set the `-b` (or `--backtrace`) argument to enable this behavior:
|
||||
|
||||
```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:
|
||||
|
@ -666,7 +659,7 @@ end
|
|||
If we run our test now, we should see a failure:
|
||||
|
||||
```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
|
||||
|
||||
# Running:
|
||||
|
@ -704,7 +697,7 @@ end
|
|||
Now if we run our tests, we should see it pass:
|
||||
|
||||
```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
|
||||
|
||||
# Running:
|
||||
|
@ -852,7 +845,7 @@ end
|
|||
I've added this file here `test/controllers/articles_routes_test.rb` and if we run the test we should see:
|
||||
|
||||
```bash
|
||||
$ bin/rake test test/controllers/articles_routes_test.rb
|
||||
$ bin/rails test test/controllers/articles_routes_test.rb
|
||||
|
||||
# Running:
|
||||
|
||||
|
|
Loading…
Reference in a new issue