Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test helpers similar to assert_raise #915

Closed
arnodirlam opened this issue Jul 4, 2023 · 3 comments
Closed

Add test helpers similar to assert_raise #915

arnodirlam opened this issue Jul 4, 2023 · 3 comments
Labels
area:pro Related to Oban Pro kind:enhancement New feature or request

Comments

@arnodirlam
Copy link

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! ❤️

@sorentwo sorentwo added kind:enhancement New feature or request area:oss Related to Oban OSS labels Jul 5, 2023
@sorentwo
Copy link
Member

sorentwo commented Jul 5, 2023

@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.

@arnodirlam
Copy link
Author

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

@sorentwo
Copy link
Member

sorentwo commented Jul 5, 2023

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.

@sorentwo sorentwo added area:pro Related to Oban Pro and removed area:oss Related to Oban OSS labels Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:pro Related to Oban Pro kind:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants