You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `unit` directory is meant to hold tests for your models, the `functional` directory is meant to hold tests for your controllers and the `integration` directory is meant to hold tests that involve any number of controllers interacting.
44
+
The `models` directory is meant to hold tests for your models, the `controllers` directory is meant to hold tests for your controllers and the `integration` directory is meant to hold tests that involve any number of controllers interacting.
46
45
47
46
Fixtures are a way of organizing test data; they reside in the `fixtures` folder.
48
47
@@ -140,10 +139,9 @@ The default test stub in `test/models/post_test.rb` looks like this:
140
139
require'test_helper'
141
140
142
141
classPostTest < ActiveSupport::TestCase
143
-
# Replace this with your real tests.
144
-
test"the truth"do
145
-
assert true
146
-
end
142
+
# test "the truth" do
143
+
# assert true
144
+
# end
147
145
end
148
146
```
149
147
@@ -224,34 +222,30 @@ TIP: You can see all these rake tasks and their descriptions by running `rake --
224
222
225
223
### Running Tests
226
224
227
-
Running a test is as simple as invoking the file containing the test cases through Ruby:
225
+
Running a test is as simple as invoking the file containing the test cases through `rails test` command.
228
226
229
227
```bash
230
-
$ ruby -Itest test/models/post_test.rb
231
-
232
-
Loaded suite models/post_test
233
-
Started
228
+
$ rails test test/models/post_test.rb
234
229
.
235
-
Finished in 0.023513 seconds.
236
230
237
-
1 tests, 1 assertions, 0 failures, 0 errors
238
-
```
231
+
Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s.
239
232
240
-
This will run all the test methods from the test case. Note that `test_helper.rb` is in the `test` directory, hence this directory needs to be added to the load path using the `-I` switch.
This will run all test methods from the test case. Note that `test_helper.rb` is in the `test` directory, hence this directory needs to be added to the load path using the `-I` switch.
248
+
255
249
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.
256
250
257
251
To see how a test failure is reported, you can add a failing test to the `post_test.rb` test case.
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:
@@ -292,9 +285,8 @@ Running this test shows the friendlier assertion message:
Now, if you noticed, we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as _Test-Driven Development_ (TDD).
@@ -334,18 +325,17 @@ end
334
325
Now you can see even more output in the console from running the tests:
Notice the 'E'in the output. It denotes a test with error.
@@ -642,12 +632,9 @@ Here's what a freshly-generated integration test looks like:
642
632
require 'test_helper'
643
633
644
634
class UserFlowsTest < ActionDispatch::IntegrationTest
645
-
fixtures :all
646
-
647
-
# Replace this with your real tests.
648
-
test "the truth" do
649
-
assert true
650
-
end
635
+
# test "the truth" do
636
+
# assert true
637
+
# end
651
638
end
652
639
```
653
640
@@ -755,23 +742,28 @@ end
755
742
Rake Tasks for Running your Tests
756
743
---------------------------------
757
744
758
-
You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rails project.
745
+
You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of commands to help in testing. The table below lists all commands that come along in the default Rakefile when you initiate a Rails project.
746
+
747
+
| Tasks | Description |
748
+
| ------------------------ | ----------- |
749
+
| `rails test` | Runs all unit, functional and integration tests. You can also simply run `rails test` as Rails will run all the tests by default|
750
+
| `rails test controllers` | Runs all the controller tests from `test/controllers`|
751
+
| `rails test functionals` | Runs all the functional tests from `test/controllers`, `test/mailers`, and `test/functional`|
752
+
| `rails test helpers` | Runs all the helper tests from `test/helpers`|
753
+
| `rails test integration` | Runs all the integration tests from `test/integration`|
754
+
| `rails test mailers` | Runs all the mailer tests from `test/mailers`|
755
+
| `rails test models` | Runs all the model tests from `test/models`|
756
+
| `rails test units` | Runs all the unit tests from `test/models`, `test/helpers`, and `test/unit`|
759
757
760
-
| Tasks | Description |
761
-
| ------------------------------- | ----------- |
762
-
| `rake test` | Runs all unit, functional and integration tests. You can also simply run `rake` as the _test_ target is the default.|
763
-
| `rake test:controllers` | Runs all the controller tests from `test/controllers`|
764
-
| `rake test:functionals` | Runs all the functional tests from `test/controllers`, `test/mailers`, and `test/functional`|
765
-
| `rake test:helpers` | Runs all the helper tests from `test/helpers`|
766
-
| `rake test:integration` | Runs all the integration tests from `test/integration`|
767
-
| `rake test:mailers` | Runs all the mailer tests from `test/mailers`|
768
-
| `rake test:models` | Runs all the model tests from `test/models`|
769
-
| `rake test:recent` | Tests recent changes|
770
-
| `rake test:uncommitted` | Runs all the tests which are uncommitted. Supports Subversion and Git|
771
-
| `rake test:units` | Runs all the unit tests from `test/models`, `test/helpers`, and `test/unit`|
758
+
There're also some test commands which you can initiate by running rake tasks:
772
759
760
+
| Tasks | Description |
761
+
| ------------------------ | ----------- |
762
+
| `rake test` | Runs all unit, functional and integration tests. You can also simply run `rake` as the _test_ target is the default.|
763
+
| `rake test:recent` | Tests recent changes|
764
+
| `rake test:uncommitted` | Runs all the tests which are uncommitted. Supports Subversion and Git|
773
765
774
-
Brief Note About `Test::Unit`
766
+
Brief Note About `MiniTest`
775
767
-----------------------------
776
768
777
769
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
0 commit comments