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
Copy file name to clipboardExpand all lines: guides/source/testing.md
+31-34Lines changed: 31 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2074,20 +2074,21 @@ NOTE: The `assert_emails` method is not tied to a particular deliver method and
2074
2074
Testing Jobs
2075
2075
------------
2076
2076
2077
-
Since your custom jobs can be queued at different levels inside your application,
2078
-
you'll need to test both the jobs themselves (their behavior when they get enqueued)
2079
-
and that other entities correctly enqueue them.
2077
+
Jobs can be tested in isolation (focusing on the job's behavior) and in context
2078
+
(focusing on the calling code's behavior).
2080
2079
2081
-
### A Basic Test Case
2080
+
### Testing Jobs in Isolation
2082
2081
2083
-
By default, when you generate a job, an associated test will be generated as well
2084
-
under the `test/jobs` directory. Here's an example test with a billing job:
2082
+
When you generate a job, an associated test file will also be generated in the
2083
+
`test/jobs`directory.
2084
+
2085
+
Here is an example test for a billing job:
2085
2086
2086
2087
```ruby
2087
2088
require "test_helper"
2088
2089
2089
2090
class BillingJobTest < ActiveJob::TestCase
2090
-
test "that account is charged" do
2091
+
test "account is charged" do
2091
2092
perform_enqueued_jobs do
2092
2093
BillingJob.perform_later(account, product)
2093
2094
end
@@ -2096,52 +2097,48 @@ class BillingJobTest < ActiveJob::TestCase
2096
2097
end
2097
2098
```
2098
2099
2099
-
This test is pretty simple and only asserts that the job did work that was expected. You can also use `perform_now` to run the job inline, but if you have retries configured, any exceptions raised by the job will be silently ignored, whereas `perform_enqueued_jobs` will fail the test and print the exception information.
2100
+
The default queue adapter for tests will not perform jobs until
2101
+
[`perform_enqueued_jobs`][] is called. Additionally, it will clear all jobs
2102
+
before each test is run so that tests do not interfere with each other.
2103
+
2104
+
The test uses `perform_enqueued_jobs` and [`perform_later`][] instead of
2105
+
[`perform_now`][] so that if retries are configured, retry failures are caught
2106
+
by the test instead of being re-enqueued and ignored.
2100
2107
2101
-
### Custom Assertions and Testing Jobs inside Other Components
Active Job ships with a bunch of custom assertions that can be used to lessen the verbosity of tests. For a full list of available assertions, see the API documentation for [`ActiveJob::TestHelper`](https://api.rubyonrails.org/classes/ActiveJob/TestHelper.html).
2112
+
### Testing Jobs in Context
2104
2113
2105
-
It's a good practice to ensure that your jobs correctly get enqueued or performed
2106
-
wherever you invoke them (e.g. inside your controllers). This is precisely where
2107
-
the custom assertions provided by Active Job are pretty useful. For instance,
2108
-
within a model, you could confirm that a job was enqueued:
2114
+
It's good practice to test that jobs are correctly enqueued, for example, by a
2115
+
controller action. The [`ActiveJob::TestHelper`][] module provides several
2116
+
methods that can help with this, such as [`assert_enqueued_with`][].
2117
+
2118
+
Here is an example that tests an account model method:
2109
2119
2110
2120
```ruby
2111
2121
require "test_helper"
2112
2122
2113
-
class ProductTest < ActiveSupport::TestCase
2123
+
class AccountTest < ActiveSupport::TestCase
2114
2124
include ActiveJob::TestHelper
2115
2125
2116
-
test "billing job scheduling" do
2126
+
test "#charge_for enqueues billing job" do
2117
2127
assert_enqueued_with(job: BillingJob) do
2118
-
product.charge(account)
2128
+
account.charge_for(product)
2119
2129
end
2120
-
assert_not account.reload.charged_for?(product)
2121
-
end
2122
-
end
2123
-
```
2124
2130
2125
-
The default adapter, `:test`, does not perform jobs when they are enqueued.
2126
-
You have to tell it when you want jobs to be performed:
2131
+
assert_not account.reload.charged_for?(product)
2127
2132
2128
-
```ruby
2129
-
require "test_helper"
2133
+
perform_enqueued_jobs
2130
2134
2131
-
class ProductTest < ActiveSupport::TestCase
2132
-
include ActiveJob::TestHelper
2133
-
2134
-
test "billing job scheduling" do
2135
-
perform_enqueued_jobs(only: BillingJob) do
2136
-
product.charge(account)
2137
-
end
2138
2135
assert account.reload.charged_for?(product)
2139
2136
end
2140
2137
end
2141
2138
```
2142
2139
2143
-
All previously performed and enqueued jobs are cleared before any test runs,
2144
-
so you can safely assume that no jobs have already been executed in the scope of each test.
0 commit comments