sorentwo/oban

Add test helpers similar to assert_raise

Closed this issue · 3 comments

Is your feature request related to a problem?

We use Oban extensively and sometimes rely on Oban jobs to be inserted when another record is created or updated. For a given workflow to be tested, this may happen in multiple places, including as part of the test setups. The problem with the current test helpers, namely assert_enqueued and refute_enqueued is that they pick up any job that was enqueued up to that point, no matter if it was part of the test setup or actual code under testing.

Of course, we could add flags everywhere in our functions that disable enqueuing the job. But that would be changing the code just to cater to the way we (need to) test it.

Solution

Add two test helpers assert_enqueue and refute_enqueue that receive a function and detect only jobs that were (or were not) enqueued while running that function, similar to assert_raise/3.

For example:

conn =
  assert_enqueue %{worker: Worker.Timecards.Recalc, queue: "recalc"}, fn ->
    post(conn, update_path, default_params: @valid_default_params)
  end

Considerations

IMO, the helpers should ...

  • check what jobs were already enqueued before the function runs, and ignore them for the assertion
  • return what the function returns (if the assertion passes)
  • allow for a way to pattern-match the job payload(s)

Hope that makes sense. Thanks a lot for the relentless work on this great library! ❤️

@arnodirlam Your proposal makes a lot of sense, and I'm in favor of it. The only quirk is pattern matching on args, as that's currently done with a database query, but I'm sure we can work around that.

Great to hear that it makes sense to you!

I've looked at how assert_enqueued is implemented - dynamically generating filters for a db query, as you mentioned - and I think that's totally fine.

I can take a stab at implementing this, if you like?

Cheers, Arno

I can take a stab at implementing this, if you like?

Feel free! If you don't have time, I'll tackle it in the next few weeks.