Oban.Testing.perform_job

You're seeing just the function perform_job, go back to Oban.Testing module for more information.
Link to this function

perform_job(worker, args, opts)

View Source (since 2.0.0)

Specs

perform_job(
  worker :: Oban.Worker.t(),
  args :: Oban.Job.args(),
  opts :: [Oban.Job.option()]
) :: Oban.Worker.result()

Construct a job and execute it with a worker module.

This reduces boiler plate when constructing jobs for unit tests and checks for common pitfalls. For example, it automatically converts args to string keys before calling perform/1, ensuring that perform clauses aren't erroneously trying to match atom keys.

The helper makes the following assertions:

  • That the worker implements the Oban.Worker behaviour
  • That the options provided build a valid job
  • That the return is valid, e.g. :ok, {:ok, value}, {:error, value} etc.

If all of the assertions pass then the function returns the result of perform/1 for you to make additional assertions on.

Examples

Successfully execute a job with some string arguments:

assert :ok = perform_job(MyWorker, %{"id" => 1})

Successfully execute a job and assert that it returns an error tuple:

assert {:error, _} = perform_job(MyWorker, %{"bad" => "arg"})

Execute a job with the args keys automatically stringified:

assert :ok = perform_job(MyWorker, %{id: 1})

Exercise custom attempt handling within a worker by passing options:

assert :ok = perform_job(MyWorker, %{}, attempt: 42)

Cause a test failure because the provided worker isn't real:

assert :ok = perform_job(Vorker, %{"id" => 1})