Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Active Job testing guide #48526

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activejob/test/cases/test_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ def test_include_helper
def test_set_test_adapter
assert_kind_of ActiveJob::QueueAdapters::TestAdapter, queue_adapter
end

def test_does_not_perform_enqueued_jobs_by_default
assert_nil queue_adapter.perform_enqueued_jobs
end
end
32 changes: 24 additions & 8 deletions guides/source/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -1933,13 +1933,7 @@ class BillingJobTest < ActiveJob::TestCase
end
```

This test is pretty simple and only asserts that the job got the work done
as expected.

By default, `ActiveJob::TestCase` will set the queue adapter to `:test` so that
your jobs are performed inline. It will also ensure that all previously performed
and enqueued jobs are cleared before any test run so you can safely assume that
no jobs have already been executed in the scope of each test.
This test is pretty simple and only asserts that the job did work that was expected.

### Custom Assertions and Testing Jobs inside Other Components

Expand All @@ -1948,7 +1942,7 @@ Active Job ships with a bunch of custom assertions that can be used to lessen th
It's a good practice to ensure that your jobs correctly get enqueued or performed
wherever you invoke them (e.g. inside your controllers). This is precisely where
the custom assertions provided by Active Job are pretty useful. For instance,
within a model:
within a model, you could confirm that a job was enqueued:

```ruby
require "test_helper"
Expand All @@ -1960,10 +1954,32 @@ class ProductTest < ActiveSupport::TestCase
assert_enqueued_with(job: BillingJob) do
product.charge(account)
end
assert_not account.reload.charged_for?(product)
end
end
```

The default adapter, `:test`, does not perform jobs when they are enqueued.
You have to tell it when you want jobs to be performed:

```ruby
require "test_helper"

class ProductTest < ActiveSupport::TestCase
include ActiveJob::TestHelper

test "billing job scheduling" do
perform_enqueued_jobs(only: BillingJob) do
product.charge(account)
end
assert account.reload.charged_for?(product)
end
end
```

All previously performed and enqueued jobs are cleared before any test runs,
so you can safely assume that no jobs have already been executed in the scope of each test.

Testing Action Cable
--------------------

Expand Down