Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
09d26237
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
09d26237
编写于
3月 30, 2015
作者:
K
Kir Shatrov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Switch to `bin/rails test` in testing guide
rails/rails#18305
上级
0d16c9d1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
23 addition
and
30 deletion
+23
-30
guides/source/testing.md
guides/source/testing.md
+23
-30
未找到文件。
guides/source/testing.md
浏览文件 @
09d26237
...
...
@@ -141,24 +141,18 @@ users(:david).id
email
(
david
.
girlfriend
.
email
,
david
.
location_tonight
)
```
### Rake 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.
| 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 |
### Console Tasks for Running your Tests
Rails comes with a CLI command to run tests.
Here are some examples how to use it:
```
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
`ra
ke
test`
command.
Running a test is as simple as invoking the file containing the test cases through
`ra
ils
test`
command.
```
bash
$
bin/ra
ke
test test
/models/article_test.rb
$
bin/ra
ils
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/ra
ke
test test
/models/article_test.rb test_the_truth
$
bin/ra
ils
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/ra
ke
test test
/models/article_test.rb test_should_not_save_article_without_title
$
bin/ra
ils
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/ra
ke
test test
/models/article_test.rb test_should_not_save_article_without_title
$
bin/ra
ils
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/ra
ke
test test
/models/article_test.rb test_should_report_error
$
bin/ra
ils
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/ra
ke
test test
/controllers/articles_controller_test.rb test_should_create_article
$
bin/ra
ils
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/ra
ke
test test
/controllers/articles_controller_test.rb test_should_create_article
$
bin/ra
ils
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/ra
ke
test test
/controllers/articles_routes_test.rb
$
bin/ra
ils
test test
/controllers/articles_routes_test.rb
# Running:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录