View Source Kiq.Testing (Kiq v0.7.3)
This module simplifies making assertions about enqueued jobs during testing.
Testing assertions only work when Kiq is started with test_mode
set to
:sandbox
. In sandbox mode jobs are never flushed to Redis and are stored in
memory until the test run is over. Each enqueued job is associated with the
process that enqueued it, allowing asynchronous tests to check stored jobs
without any interference.
Using in Tests
If your application has defined a top level Kiq module as MyApp.Kiq
, then
you would use
the testing module inside your application's case templates
like so:
use Kiq.Testing, client: MyApp.Kiq.Client
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 stored while testing.
Given a simple module that enqueues a job:
defmodule MyApp.Business do
alias MyApp.Kiq
def work(args) do
Kiq.enqueue(class: "SomeWorker", args: args)
end
end
The behaviour can be exercised in your test code:
defmodule MyApp.BusinessTest do
use ExUnit.Case, async: true
use Kiq.Testing, client: MyApp.Kiq.Client
alias MyApp.Business
test "jobs are enqueued with provided arguments" do
Business.work([1, 2])
assert_enqueued(class: "SomeWorker", args: [1, 2])
end
end
Testing Jobs From Other Processes
All calls to assert_enqueued/3
and refute_enqueued/3
use the :sandbox
scope by default. That scope ensures that the current process can only find
its own enqueued jobs. Sometimes this behavior is undesirable. For example,
when jobs are being enqueued outside of the test process. If a separate
server or task enqueue a job you may use the :shared
scoping to make global
assertions.
Task.async(fn -> Kiq.enqueue(class: "MyWorker", args: [1, 2]) end)
assert_enqueued(:shared, class: "MyWorker")
Summary
Functions
Assert that a job with particular options has been enqueued.
Refute that a job with particular options has been enqueued.
Functions
@spec assert_enqueued(client :: identifier(), scoping :: atom(), args :: Enum.t()) :: any()
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 class: "MyWorker"
will match any jobs for that class,
regardless of the args.
@spec refute_enqueued(client :: identifier(), scoping :: atom(), args :: Enum.t()) :: any()
Refute that a job with particular options has been enqueued.
See assert_enqueued/2
for additional details.