Skip to content

Commit

Permalink
have_enqueued_job supports Time object
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Aug 20, 2019
1 parent 9c6d8f3 commit 493fbb9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/rspec/rails/matchers/active_job.rb
Expand Up @@ -140,8 +140,9 @@ def job_match?(job)

def arguments_match?(job)
if @args.any?
args = round_time_arguments(@args)
deserialized_args = deserialize_arguments(job)
RSpec::Mocks::ArgumentListMatcher.new(*@args).args_match?(*deserialized_args)
RSpec::Mocks::ArgumentListMatcher.new(*args).args_match?(*deserialized_args)
else
true
end
Expand Down Expand Up @@ -172,6 +173,19 @@ def set_expected_number(relativity, count)
end
end

def round_time_arguments(argument)
case argument
when Time, ActiveSupport::TimeWithZone, DateTime
argument.change(usec: 0)
when Hash
argument.transform_values { |value| round_time_arguments(value) }
when Array
argument.map { |element| round_time_arguments(element) }
else
argument
end
end

def deserialize_arguments(job)
::ActiveJob::Arguments.deserialize(job[:args])
rescue ::ActiveJob::DeserializationError
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/rails/matchers/active_job_spec.rb
Expand Up @@ -328,6 +328,24 @@ def self.name; "LoggingJob"; end
expect(arg).to eq("asdf")
}
end

if Rails.version.to_f >= 6.0
it "passes with Time" do
usec_time = Time.iso8601('2016-07-01T00:00:00.000001Z')

expect {
hello_job.perform_later(usec_time)
}.to have_enqueued_job(hello_job).with(usec_time)
end

it "passes with ActiveSupport::TimeWithZone" do
usec_time = Time.iso8601('2016-07-01T00:00:00.000001Z').in_time_zone

expect {
hello_job.perform_later(usec_time)
}.to have_enqueued_job(hello_job).with(usec_time)
end
end
end

describe "have_been_enqueued" do
Expand Down

0 comments on commit 493fbb9

Please sign in to comment.