module Matchers
  class BeRetriedByDelayedJob
    def matches?(expected)
      @failure_message = "This expectation only works with Delayed::Worker.delay_jobs = true" and return false unless Delayed::Worker.delay_jobs
      begin
        expected.reload
      rescue => e
        @failure_message = "The job no longer exists in the database" and return false
      end

      @failure_message = "The job has a failed_at date so will not be retried" and return false unless expected.failed_at.blank?
      true
    end

    def failure_message
      @failure_message || "Job will not be retried"
    end

  end

  def be_retried_by_delayed_job(*args, &block)
    BeRetriedByDelayedJob.new(*args, &block)
  end
end
