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

Projects
None yet
3 participants
@byroot
Copy link
Member

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

This comment has been minimized.

Copy link
Contributor

commented Jul 24, 2015

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

This comment has been minimized.

Copy link
Member

commented Aug 11, 2015

LGTM, could you add a CHANGELOG entry?

@byroot byroot force-pushed the byroot:activejob-test-helpers branch from 380bfd8 to b6d3a47 Aug 11, 2015

@byroot

This comment has been minimized.

Copy link
Member Author

commented Aug 11, 2015

Changelog entry added, thanks!

rafaelfranca added a commit that referenced this pull request Aug 11, 2015

Merge pull request #21010 from byroot/activejob-test-helpers
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

Include deserialized arguments in jobs returned by AJ test helpers
As an undocumented feature, `assert_enqueued_with` and `assert_performed_with`
return a instantiated instance of the matching job for further assertion
(rails#21010).

Before this commit the `argument` method on the returned instance
returns a serialized version of the argument.

XrXr added a commit to XrXr/rails that referenced this pull request Oct 12, 2018

Include deserialized arguments in jobs returned by AJ test helpers
As an undocumented feature, `assert_enqueued_with` and `assert_performed_with`
return a instantiated instance of the matching job for further assertion
(rails#21010).

Before this commit the `argument` method on the returned instance
returns a serialized version of the argument.

XrXr added a commit to XrXr/rails that referenced this pull request Oct 12, 2018

Include deserialized arguments in jobs returned by AJ test helpers
`assert_enqueued_with` and `assert_performed_with` return a instantiated
instance of the matching job for further assertion (rails#21010).

Before this commit the `argument` method on the returned instance
returns a serialized version of the argument.

XrXr added a commit to XrXr/rails that referenced this pull request Oct 12, 2018

Include deserialized arguments in jobs returned by AJ test helpers
`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
You can’t perform that action at this time.