Class: RSpec::Rails::Matchers::HaveEnqueuedMail Private
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
RSpec::Rails::Matchers::ActiveJob::HaveEnqueuedJob
|
Defined in: | rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb |
Overview
Matcher class for #have_enqueued_mail. Should not be instantiated directly.
Constant Summary
-
MAILER_JOB_METHOD =
# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 16'deliver_now'.freeze
BaseMatcher
- Inherited
ActiveJob::HaveEnqueuedJob
- Inherited
FAILURE_MESSAGE_EXPECTATION_ACTION, MESSAGE_EXPECTATION_ACTION
Class Method Summary
- .new(mailer_class, method_name) ⇒ HaveEnqueuedMail constructor Internal use only
ActiveJob::HaveEnqueuedJob
- Inherited
ActiveJob::Base
- Inherited
BaseMatcher
- Inherited
.matcher_name, .new, | |
.underscore | Borrowed from ActiveSupport. |
Instance Attribute Summary
ActiveJob::Base
- Inherited
BaseMatcher
- Inherited
#actual, | |
#diffable? |
|
#expected, #expects_call_stack_jump?, #matcher_name, #matcher_name=, #rescued_exception, | |
#supports_block_expectations? | Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false. |
Instance Method Summary
- #description Internal use only
- #failure_message Internal use only
- #failure_message_when_negated Internal use only
- #matches?(block) ⇒ Boolean Internal use only
- #with(*args, &block) Internal use only
- #arguments_match?(job) ⇒ Boolean private Internal use only
- #base_mailer_args private Internal use only
- #base_message private Internal use only
- #check_active_job_adapter private Internal use only
-
#deserialize_arguments(job)
private
Internal use only
Ruby 3.1 changed how params were serialized on
::RSpec::Rails
6.1 so we override the active job implementation and customize it here. - #detect_args_signature_mismatch(jobs) private Internal use only
- #expected_count_message private Internal use only
- #extract_args_without_parameterized_params(job) private Internal use only
- #job_matches?(job) ⇒ Boolean private Internal use only
- #legacy_mail?(job) ⇒ Boolean private Internal use only
- #mail_job_message(job) private Internal use only
- #mailer_class_name private Internal use only
- #parameterized_mail?(job) ⇒ Boolean private Internal use only
- #unified_mail?(job) ⇒ Boolean private Internal use only
- #unmatching_mail_jobs private Internal use only
- #unmatching_mail_jobs_message private Internal use only
- #yield_mail_args(block) private Internal use only
::RSpec::Mocks::ArgumentMatchers
- Included
#a_kind_of | Alias for Mocks::ArgumentMatchers#kind_of. |
#an_instance_of | Alias for Mocks::ArgumentMatchers#instance_of. |
#any_args | Acts like an arg splat, matching any number of args at any point in an arg list. |
#anything | Matches any argument at all. |
#array_excluding | Matches an array that excludes the specified items. |
#array_including | Matches an array that includes the specified items at least once. |
#boolean | Matches a boolean value. |
#duck_type | Matches if the actual argument responds to the specified messages. |
#hash_excluding | Matches a hash that doesn’t include the specified key(s) or key/value. |
#hash_including | Matches a hash that includes the specified key(s) or key/value pairs. |
#hash_not_including | Alias for Mocks::ArgumentMatchers#hash_excluding. |
#instance_of | Matches if ‘arg.instance_of?(klass)`. |
#kind_of | Matches if ‘arg.kind_of?(klass)`. |
#no_args | Matches no arguments. |
ActiveJob::HaveEnqueuedJob
- Inherited
ActiveJob::Base
- Inherited
BaseMatcher
- Inherited
#actual_formatted, | |
#description | Generates a description using |
#expected_formatted, | |
#match_unless_raises | Used to wrap a block of code that will indicate failure by raising one of the named exceptions. |
#matches? | Indicates if the match is successful. |
#assert_ivars, #present_ivars |
BaseMatcher::DefaultFailureMessages
- Included
#failure_message | Provides a good generic failure message. |
#failure_message_when_negated | Provides a good generic negative failure message. |
BaseMatcher::HashFormatting
- Included
#improve_hash_formatting | ‘{ |
::RSpec::Matchers::Composable
- Included
#& | Alias for Matchers::Composable#and. |
#=== | Delegates to #matches?. |
#and | Creates a compound |
#description_of | Returns the description of the given object in a way that is aware of composed matchers. |
#or | Creates a compound |
#values_match? | This provides a generic way to fuzzy-match an expected value against an actual value. |
#| | Alias for Matchers::Composable#or. |
#should_enumerate? | We should enumerate arrays as long as they are not recursive. |
#surface_descriptions_in | Transforms the given data structure (typically a hash or array) into a new data structure that, when |
#unreadable_io?, | |
#with_matchers_cloned | Historically, a single matcher instance was only checked against a single value. |
Instance Method Details
#arguments_match?(job) ⇒ Boolean
(private)
# File 'rspec-rails/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
#base_mailer_args (private)
[ GitHub ]# File 'rspec-rails/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
#base_message (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 57
def [mailer_class_name, @method_name].compact.join('.').tap do |msg| msg << " #{}" 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
#check_active_job_adapter (private)
#description
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 27
def description "enqueues #{mailer_class_name}.#{@method_name}" end
#deserialize_arguments(job) (private)
Ruby 3.1 changed how params were serialized on ::RSpec::Rails
6.1 so we override the active job implementation and customize it here.
# File 'rspec-rails/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
#detect_args_signature_mismatch(jobs) (private)
[ GitHub ]# File 'rspec-rails/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
#expected_count_message (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 67
def "#{} #{@expected_number} #{@expected_number == 1 ? 'time' : 'times'}" end
#extract_args_without_parameterized_params(job) (private)
[ GitHub ]# File 'rspec-rails/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
#failure_message
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 43
def return @failure_message if defined?(@failure_message) "expected to enqueue #{}".tap do |msg| msg << "\n#{}" if unmatching_mail_jobs.any? end end
#failure_message_when_negated
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 51
def "expected not to enqueue #{}" end
#job_matches?(job) ⇒ Boolean
(private)
# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 75
def job_matches?(job) legacy_mail?(job) || parameterized_mail?(job) || unified_mail?(job) end
#legacy_mail?(job) ⇒ Boolean
(private)
# File 'rspec-rails/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
#mail_job_message(job) (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 141
def (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
#mailer_class_name (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 71
def mailer_class_name @mailer_class ? @mailer_class.name : 'ActionMailer::Base' end
#matches?(block) ⇒ Boolean
# File 'rspec-rails/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
#parameterized_mail?(job) ⇒ Boolean
(private)
# File 'rspec-rails/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
#unified_mail?(job) ⇒ Boolean
(private)
# File 'rspec-rails/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
#unmatching_mail_jobs (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 125
def unmatching_mail_jobs @unmatching_jobs.select do |job| job_matches?(job) end end
#unmatching_mail_jobs_message (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb', line 131
def msg = "Queued deliveries:" unmatching_mail_jobs.each do |job| msg << "\n #{ (job)}" end msg end
#with(*args, &block)
[ GitHub ]# File 'rspec-rails/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
#yield_mail_args(block) (private)
[ GitHub ]# File 'rspec-rails/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