diff --git a/lib/rspec/rails/matchers/active_job.rb b/lib/rspec/rails/matchers/active_job.rb index 61cb35e8f8..d6edd1c7eb 100644 --- a/lib/rspec/rails/matchers/active_job.rb +++ b/lib/rspec/rails/matchers/active_job.rb @@ -97,7 +97,7 @@ def supports_block_expectations? def check(jobs) @matching_jobs, @unmatching_jobs = jobs.partition do |job| - if job_match?(job) && arguments_match?(job) && other_attributes_match?(job) + if job_match?(job) && arguments_match?(job) && queue_match?(job) && at_match?(job) args = deserialize_arguments(job) @block.call(*args) true @@ -148,19 +148,17 @@ def arguments_match?(job) end end - def other_attributes_match?(job) - serialized_attributes.all? { |key, value| value == job[key] } - end + def queue_match?(job) + return true unless @queue - def serialized_attributes - {}.tap do |attributes| - attributes[:at] = serialized_at if @at - attributes[:queue] = @queue if @queue - end + @queue == job[:queue] end - def serialized_at - @at == :no_wait ? nil : @at.to_f + def at_match?(job) + return true unless @at + return job[:at].nil? if @at == :no_wait + + values_match?(@at, Time.at(job[:at])) end def set_expected_number(relativity, count) diff --git a/spec/rspec/rails/matchers/active_job_spec.rb b/spec/rspec/rails/matchers/active_job_spec.rb index 04c44de9d9..bad4a2f773 100644 --- a/spec/rspec/rails/matchers/active_job_spec.rb +++ b/spec/rspec/rails/matchers/active_job_spec.rb @@ -211,6 +211,14 @@ def self.name; "LoggingJob"; end }.to have_enqueued_job.at(date) end + it "accepts composable matchers as an at date" do + future = 1.minute.from_now + slightly_earlier = 58.seconds.from_now + expect { + hello_job.set(:wait_until => slightly_earlier).perform_later + }.to have_enqueued_job.at(a_value_within(5.seconds).of(future)) + end + it "has an enqueued job when providing at of :no_wait and there is no wait" do expect { hello_job.perform_later @@ -379,5 +387,13 @@ def self.name; "LoggingJob"; end expect(heavy_lifting_job).not_to have_been_enqueued }.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 2/) end + + it "accepts composable matchers as an at date" do + future = 1.minute.from_now + slightly_earlier = 58.seconds.from_now + heavy_lifting_job.set(:wait_until => slightly_earlier).perform_later + expect(heavy_lifting_job) + .to have_been_enqueued.at(a_value_within(5.seconds).of(future)) + end end end diff --git a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb index 4e46c335f9..749d521d43 100644 --- a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb +++ b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb @@ -254,6 +254,15 @@ def email_with_args(arg1, arg2); end }.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time at #{send_time.strftime('%F %T')}/) end + it "accepts composable matchers as an at date" do + future = 1.minute.from_now + slightly_earlier = 58.seconds.from_now + + expect { + TestMailer.test_email.deliver_later(:wait_until => slightly_earlier) + }.to have_enqueued_email(TestMailer, :test_email).at(a_value_within(5.seconds).of(future)) + end + it "passes when deliver_later is called with a queue argument" do expect { TestMailer.test_email.deliver_later(:queue => 'urgent_mail')