Class: RSpec::Rails::Matchers::ActiveJob::Base Private
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
RSpec::Rails::Matchers::HaveEnqueuedMail, RSpec::Rails::Matchers::ActiveJob::HaveBeenEnqueued, RSpec::Rails::Matchers::ActiveJob::HaveBeenPerformed, RSpec::Rails::Matchers::ActiveJob::HaveEnqueuedJob, RSpec::Rails::Matchers::ActiveJob::HavePerformedJob
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
RSpec::Rails::Matchers::BaseMatcher
|
Defined in: | rspec-rails/lib/rspec/rails/matchers/active_job.rb |
Overview
rubocop: disable Metrics/ClassLength
Constant Summary
::RSpec::Rails::Matchers::BaseMatcher
- Inherited
Class Method Summary
- .new ⇒ Base constructor Internal use only
::RSpec::Rails::Matchers::BaseMatcher
- Inherited
.matcher_name, .new, | |
.underscore | Borrowed from ActiveSupport. |
Instance Attribute Summary
- #supports_block_expectations? ⇒ Boolean readonly Internal use only
- #skip_signature_verification? ⇒ Boolean readonly private Internal use only
::RSpec::Rails::Matchers::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
- #at(time_or_date) Internal use only
- #at_least(count) Internal use only
- #at_most(count) Internal use only
- #at_priority(priority) Internal use only
- #exactly(count) Internal use only
- #failure_message Internal use only
- #failure_message_when_negated Internal use only
- #message_expectation_modifier Internal use only
- #on_queue(queue) Internal use only
- #once Internal use only
- #thrice Internal use only
- #times Internal use only
- #twice Internal use only
- #with(*args, &block) Internal use only
- #arguments_match?(job) ⇒ Boolean private Internal use only
- #at_match?(job) ⇒ Boolean private Internal use only
- #base_job_message(job) private Internal use only
- #base_message private Internal use only
- #check(jobs) private Internal use only
- #check_args_signature_mismatch(job_class, job_method, args) private Internal use only
- #check_for_inprecise_value(scheduled_at) private Internal use only
- #deserialize_arguments(job) private Internal use only
- #detect_args_signature_mismatch(jobs) private Internal use only
- #job_matches?(job) ⇒ Boolean private Internal use only
- #matches_constraints?(job) ⇒ Boolean private Internal use only
- #priority_match?(job) ⇒ Boolean private Internal use only
- #queue_adapter private Internal use only
- #queue_match?(job) ⇒ Boolean private Internal use only
- #serialize_and_deserialize_arguments(args) private Internal use only
- #set_expected_number(relativity, count) private Internal use only
::RSpec::Rails::Matchers::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 |
::RSpec::Rails::Matchers::BaseMatcher::DefaultFailureMessages
- Included
#failure_message | Provides a good generic failure message. |
#failure_message_when_negated | Provides a good generic negative failure message. |
::RSpec::Rails::Matchers::BaseMatcher::HashFormatting
- Included
#improve_hash_formatting | ‘{ |
::RSpec::Matchers::Composable
- Included
#& | Alias for Matchers::Composable#and. |
#=== | Delegates to |
#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 Attribute Details
#skip_signature_verification? ⇒ Boolean
(readonly, private)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 194
def skip_signature_verification? !RSpec::Mocks.configuration.verify_partial_doubles? || RSpec::Mocks.configuration.temporarily_suppress_partial_double_verification end
#supports_block_expectations? ⇒ Boolean
(readonly)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 104
def supports_block_expectations? true end
Instance Method Details
#arguments_match?(job) ⇒ Boolean
(private)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 170
def arguments_match?(job) if @args.any? args = serialize_and_deserialize_arguments(@args) deserialized_args = deserialize_arguments(job) RSpec::Mocks::ArgumentListMatcher.new(*args).args_match?(*deserialized_args) else true end end
#at(time_or_date)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 39
def at(time_or_date) case time_or_date when Time then @at = Time.at(time_or_date.to_f) else @at = time_or_date end self end
#at_least(count)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 53
def at_least(count) set_expected_number(:at_least, count) self end
#at_match?(job) ⇒ Boolean
(private)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 220
def at_match?(job) return true unless @at return job[:at].nil? if @at == :no_wait return false unless job[:at] scheduled_at = Time.at(job[:at]) values_match?(@at, scheduled_at) || check_for_inprecise_value(scheduled_at) end
#at_most(count)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 58
def at_most(count) set_expected_number(:at_most, count) self end
#at_priority(priority)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 34
def at_priority(priority) @priority = priority.to_i self end
#base_job_message(job) (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 145
def (job) msg_parts = [] msg_parts << "with #{deserialize_arguments(job)}" if job[:args].any? msg_parts << "on queue #{job[:queue]}" if job[:queue] msg_parts << "at #{Time.at(job[:at])}" if job[:at] msg_parts << if job[:priority] "with priority #{job[:priority]}" else "with no priority specified" end "#{job[:job].name} job".tap do |msg| msg << " #{msg_parts.join(', ')}" if msg_parts.any? end end
#base_message (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 135
def "#{} #{@expected_number} jobs,".tap do |msg| msg << " with #{@args}," if @args.any? msg << " on queue #{@queue}," if @queue msg << " at #{@at.inspect}," if @at msg << " with priority #{@priority}," if @priority msg << " but #{self.class::MESSAGE_EXPECTATION_ACTION} #{@matching_jobs_count}" end end
#check(jobs) (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 110
def check(jobs) @matching_jobs, @unmatching_jobs = jobs.partition do |job| if matches_constraints?(job) args = deserialize_arguments(job) @block.call(*args) true else false end end if (signature_mismatch = detect_args_signature_mismatch(@matching_jobs)) @failure_message = signature_mismatch return false end @matching_jobs_count = @matching_jobs.size case @expectation_type when :exactly then @expected_number == @matching_jobs_count when :at_most then @expected_number >= @matching_jobs_count when :at_least then @expected_number <= @matching_jobs_count end end
#check_args_signature_mismatch(job_class, job_method, args) (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 199
def check_args_signature_mismatch(job_class, job_method, args) signature = Support::MethodSignature.new(job_class.public_instance_method(job_method)) verifier = Support::StrictSignatureVerifier.new(signature, args) unless verifier.valid? "Incorrect arguments passed to #{job_class.name}: #{verifier.}" end end
#check_for_inprecise_value(scheduled_at) (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 229
def check_for_inprecise_value(scheduled_at) return unless Time === @at && values_match?(@at.change(usec: 0), scheduled_at) RSpec.warn_with((<<-WARNING).gsub(/^\s+\|/, '').chomp) |[WARNING] Your expected `at(...)` value does not match the job scheduled_at value |unless microseconds are removed. This precision error often occurs when checking |values against `Time.current` / `Time.now` which have usec precision, but Rails |uses `n.seconds.from_now` internally which has a usec count of `0`. | |Use `change(usec: 0)` to correct these values. For example: | |`Time.current.change(usec: 0)` | |Note: RSpec cannot do this for you because jobs can be scheduled with usec |precision and we do not know whether it is on purpose or not. | | WARNING false end
#deserialize_arguments(job) (private)
[ GitHub ]#detect_args_signature_mismatch(jobs) (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 180
def detect_args_signature_mismatch(jobs) return if skip_signature_verification? jobs.each do |job| args = deserialize_arguments(job) if (signature_mismatch = check_args_signature_mismatch(job.fetch(:job), :perform, args)) return signature_mismatch end end nil end
#exactly(count)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 48
def exactly(count) set_expected_number(:exactly, count) self end
#failure_message
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 79
def return @failure_message if defined?(@failure_message) "expected to #{self.class::FAILURE_MESSAGE_EXPECTATION_ACTION} #{}".tap do |msg| if @unmatching_jobs.any? msg << "\nQueued jobs:" @unmatching_jobs.each do |job| msg << "\n #{ (job)}" end end end end
#failure_message_when_negated
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 92
def "expected not to #{self.class::FAILURE_MESSAGE_EXPECTATION_ACTION} #{}" end
#job_matches?(job) ⇒ Boolean
(private)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 166
def job_matches?(job) @job ? @job == job[:job] : true end
#matches_constraints?(job) ⇒ Boolean
(private)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 162
def matches_constraints?(job) job_matches?(job) && arguments_match?(job) && queue_match?(job) && at_match?(job) && priority_match?(job) end
#message_expectation_modifier
[ GitHub ]#on_queue(queue)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 29
def on_queue(queue) @queue = queue.to_s self end
#once
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 67
def once exactly(:once) end
#priority_match?(job) ⇒ Boolean
(private)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 214
def priority_match?(job) return true unless @priority @priority == job[:priority] end
#queue_adapter (private)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 273
def queue_adapter ::ActiveJob::Base.queue_adapter end
#queue_match?(job) ⇒ Boolean
(private)
# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 208
def queue_match?(job) return true unless @queue @queue == job[:queue] end
#serialize_and_deserialize_arguments(args) (private)
[ GitHub ]#set_expected_number(relativity, count) (private)
[ GitHub ]#thrice
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 75
def thrice exactly(:thrice) end
#times
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 63
def times self end
#twice
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 71
def twice exactly(:twice) end
#with(*args, &block)
[ GitHub ]# File 'rspec-rails/lib/rspec/rails/matchers/active_job.rb', line 23
def with(*args, &block) @args = args @block = block if block.present? self end