Oban test assert_enqueued not picking up jobs

So I’m trying to test my workers, and I have queues and plugins set to false in my config/test.exs file. It seems like the jobs are being completed before I can assert that they’re enqueued??

config :app, Oban, plugins: false, queues: false, testing: :inline

    test "successfully enqueues jobs for valid youtube playlist subscriptions" do
      subs = insert_list(2, :subscription)

      assert :ok = perform_job(SubscriptionsWorker, %{})

      Enum.each(subs, fn sub ->
        assert_enqueued(
          [worker: YouTubeAPIWorker, args: %{"playlist_id" => sub.external_id}],
          100
        )
      end)
    end

And in my SubscriptionsWorker here is the perform function:

def perform(_job) do
    Subscriptions.list_subscriptions()
    |> Enum.map(&subscription_to_job/1)
    |> Enum.reject(&(&1 == :drop))
    |> Oban.insert_all()
    |> IO.inspect() # prints the jobs as already completed

    :ok
  end

But still I’m getting an error that that there are no matches:

    # Expected a job matching:

     %{
       args: %{"playlist_id" => "subscription_-576460752303423484"},
       worker: YouTubeAPIWorker
     }

You’ve set the testing mode to :inline, which executes jobs immediately without them hitting the database. To assert that they are enqueued you need :manual testing mode:

config :app, Oban, testing: :manual

Note that it doesn’t override plugins or queues. Testing mode does that automatically, after it validates the configuration.

There’s more about all of this in the testing guides.

2 Likes