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

Make assert_enqueued_with and assert_performed_with returns the matched job #21010

Merged
merged 1 commit into from
Aug 11, 2015

Conversation

byroot
Copy link
Member

@byroot byroot commented Jul 23, 2015

Sometimes job properties can't be asserted with pure equality. It might be because part of it is time sensitive and you need an assert_delta equivalent, or it might contain part of randomness.

Or it might even be that you only want to assert one of the passed arguments.

This PR makes assert_enqueued_with and assert_performed_with returns the matched job if any.

Example:

job = assert_enqueued_with(job: SomeJob) do
  some_business_logic
end

assert_in_delta 5.minutes.from_now, job.scheduled_at, 1
assert_equal 5, job.arguments.second

I know an assertion method returning a meaningful value might seems weird, but there is precedent for this. For example if you want to assert an exception message, the way do to with minitest is:

error = assert_raises TypeError do
  '1' + 2
end
assert_equal 'no implicit conversion of Fixnum into String', error.message

@rafaelfranca thoughts?

cc @etiennebarrie

@etiennebarrie
Copy link
Contributor

Especially when you compare with the equivalent with serialized arguments:

assert_enqueued_with(job: SomeJob, args: [user]) do
  some_business_logic
end

vs.

job = enqueued_jobs.first
assert_equal SomeJob, job[:job]
assert_equal ActiveJob::Arguments.serialize([user]), job[:args]
assert_in_delta 5.minutes.from_now.to_i, job[:at], 1

Note the need to serialize :at with to_i and the call to ActiveJob::Arguments.serialize

@rafaelfranca
Copy link
Member

LGTM, could you add a CHANGELOG entry?

@byroot
Copy link
Member Author

byroot commented Aug 11, 2015

Changelog entry added, thanks!

rafaelfranca added a commit that referenced this pull request Aug 11, 2015
Make assert_enqueued_with and assert_performed_with returns the matched job
@rafaelfranca rafaelfranca merged commit 9e66b38 into rails:master Aug 11, 2015
XrXr added a commit to XrXr/rails that referenced this pull request Oct 12, 2018
`assert_enqueued_with` and `assert_performed_with` return a instantiated
instance of the matching job for further assertion (rails#21010).

Before this commit the `arguments` method on the returned instance
returns a serialized version of the arguments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants