Oban v0.5.0 Oban.Testing View Source

This module simplifies making assertions about enqueued jobs during testing.

Assertions may be made on any property of a job, but you'll typically want to check by args, queue or worker.

Using in Tests

The most convenient way to use Oban.Testing is to use the module:

use Oban.Testing, repo: MyApp.Repo

That will define two helper functions, assert_enqueued/1 and refute_enqueued/1. The functions can then be used to make assertions on the jobs that have been inserted in the database while testing.

assert_enqueued worker: MyWorker, args: %{id: 1}

# or

refute_enqueued queue: "special", args: %{id: 2}

Example

Given a simple module that enqueues a job:

defmodule MyApp.Business do
  def work(args) do
    args
    |> Oban.Job.new(worker: MyApp.Worker, queue: :special)
    |> MyApp.Repo.insert!()
  end
end

The behaviour can be exercised in your test code:

defmodule MyApp.BusinessTest do
  use ExUnit.Case, async: true
  use Oban.Testing, repo: MyApp.Repo

  alias MyApp.Business

  test "jobs are enqueued with provided arguments" do
    Business.work(%{id: 1, message: "Hello!"})

    assert_enqueued worker: MyApp.Worker, args: %{id: 1, message: "Hello!"}
  end
end

Adding to Case Templates

To include assert_enqueued/1 and refute_enqueued/1 in all of your tests you can add it to your case templates:

defmodule MyApp.DataCase do
  use ExUnit.CaseTemplate

  using do
    quote do
      use Oban.Testing, repo: MyApp.Repo

      import Ecto
      import Ecto.Changeset
      import Ecto.Query
      import MyApp.DataCase

      alias MyApp.Repo
    end
  end
end

Link to this section Summary

Functions

Assert that a job with particular options has been enqueued.

Refute that a job with particular options has been enqueued.

Link to this section Functions

Link to this function

assert_enqueued(repo, opts) View Source (since 0.3.0)
assert_enqueued(repo :: module(), opts :: Enum.t()) :: true

Assert that a job with particular options has been enqueued.

Only values for the provided arguments will be checked. For example, an assertion made on worker: "MyWorker" will match any jobs for that worker, regardless of the queue or args.

Link to this function

refute_enqueued(repo, opts) View Source (since 0.3.0)
refute_enqueued(repo :: module(), opts :: Enum.t()) :: false

Refute that a job with particular options has been enqueued.

See assert_enqueued/2 for additional details.