class RSpec::Rails::Matchers::HaveEnqueuedMail
Matcher class for ‘have_enqueued_mail`. Should not be instantiated directly.
@private @see RSpec::Rails::Matchers#have_enqueued_mail
Constants
- MAILER_JOB_METHOD
Public Class Methods
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 20 def initialize(mailer_class, method_name) super(nil) @mailer_class = mailer_class @method_name = method_name @mail_args = [] end
Calls superclass method
RSpec::Rails::Matchers::ActiveJob::HaveEnqueuedJob::new
Public Instance Methods
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 27 def description "enqueues #{mailer_class_name}.#{@method_name}" end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 43 def failure_message return @failure_message if defined?(@failure_message) "expected to enqueue #{base_message}".tap do |msg| msg << "\n#{unmatching_mail_jobs_message}" if unmatching_mail_jobs.any? end end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 51 def failure_message_when_negated "expected not to enqueue #{base_message}" end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 36 def matches?(block) raise ArgumentError, 'have_enqueued_mail and enqueue_mail only work with block arguments' unless block.respond_to?(:call) check_active_job_adapter super end
Calls superclass method
RSpec::Rails::Matchers::ActiveJob::HaveEnqueuedJob#matches?
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 31 def with(*args, &block) @mail_args = args block.nil? ? super : super(&yield_mail_args(block)) end
Calls superclass method
RSpec::Rails::Matchers::ActiveJob::Base#with
Private Instance Methods
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 79 def arguments_match?(job) @args = if @mail_args.any? base_mailer_args + @mail_args elsif @mailer_class && @method_name base_mailer_args + [any_args] elsif @mailer_class [mailer_class_name, any_args] else [] end super(job) end
Calls superclass method
RSpec::Rails::Matchers::ActiveJob::Base#arguments_match?
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 111 def base_mailer_args [mailer_class_name, @method_name.to_s, MAILER_JOB_METHOD] end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 57 def base_message [mailer_class_name, @method_name].compact.join('.').tap do |msg| msg << " #{expected_count_message}" msg << " with #{@mail_args}," if @mail_args.any? msg << " on queue #{@queue}," if @queue msg << " at #{@at.inspect}," if @at msg << " but enqueued #{@matching_jobs.size}" end end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 119 def check_active_job_adapter return if ::ActiveJob::QueueAdapters::TestAdapter === ::ActiveJob::Base.queue_adapter raise StandardError, "To use HaveEnqueuedMail matcher set `ActiveJob::Base.queue_adapter = :test`" end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 157 def deserialize_arguments(job) args = super return args unless Hash === args.last hash = args.pop if hash.key?("_aj_ruby2_keywords") keywords = hash["_aj_ruby2_keywords"] original_hash = keywords.each_with_object({}) { |keyword, new_hash| new_hash[keyword.to_sym] = hash[keyword] } args + [original_hash] elsif hash.key?(:args) && hash.key?(:params) args + [hash] elsif hash.key?(:args) args + hash[:args] else args + [hash] end end
Ruby 3.1 changed how params were serialized on Rails
6.1 so we override the active job implementation and customize it here.
Calls superclass method
RSpec::Rails::Matchers::ActiveJob::Base#deserialize_arguments
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 94 def detect_args_signature_mismatch(jobs) return if @method_name.nil? return if skip_signature_verification? mailer_class = mailer_class_name.constantize jobs.each do |job| mailer_args = extract_args_without_parameterized_params(job) if (signature_mismatch = check_args_signature_mismatch(mailer_class, @method_name, mailer_args)) return signature_mismatch end end nil end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 67 def expected_count_message "#{message_expectation_modifier} #{@expected_number} #{@expected_number == 1 ? 'time' : 'times'}" end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 179 def extract_args_without_parameterized_params(job) args = deserialize_arguments(job) mailer_args = args - base_mailer_args if parameterized_mail?(job) mailer_args = mailer_args[1..-1] # ignore parameterized params elsif mailer_args.last.is_a?(Hash) && mailer_args.last.key?(:args) mailer_args = args.last[:args] end mailer_args end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 75 def job_matches?(job) legacy_mail?(job) || parameterized_mail?(job) || unified_mail?(job) end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 192 def legacy_mail?(job) RSpec::Rails::FeatureCheck.has_action_mailer_legacy_delivery_job? && job[:job] <= ActionMailer::DeliveryJob end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 141 def mail_job_message(job) job_args = deserialize_arguments(job) mailer_method = job_args[0..1].join('.') mailer_args = job_args[3..-1] msg_parts = [] msg_parts << "with #{mailer_args}" if mailer_args.any? msg_parts << "on queue #{job[:queue]}" if job[:queue] && job[:queue] != 'mailers' msg_parts << "at #{Time.at(job[:at])}" if job[:at] "#{mailer_method} #{msg_parts.join(', ')}".strip end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 71 def mailer_class_name @mailer_class ? @mailer_class.name : 'ActionMailer::Base' end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 196 def parameterized_mail?(job) RSpec::Rails::FeatureCheck.has_action_mailer_parameterized? && job[:job] <= ActionMailer::Parameterized::DeliveryJob end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 200 def unified_mail?(job) RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery? && job[:job] <= ActionMailer::MailDeliveryJob end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 125 def unmatching_mail_jobs @unmatching_jobs.select do |job| job_matches?(job) end end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 131 def unmatching_mail_jobs_message msg = "Queued deliveries:" unmatching_mail_jobs.each do |job| msg << "\n #{mail_job_message(job)}" end msg end
Source
# File lib/rspec/rails/matchers/have_enqueued_mail.rb, line 115 def yield_mail_args(block) proc { |*job_args| block.call(*(job_args - base_mailer_args)) } end