Class: SidekiqJobsEnqueuedMatcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/rspec/goodies/matchers/sidekiq.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual, worker_class, expected_size = nil, expected_properties = {}) ⇒ SidekiqJobsEnqueuedMatcher

Returns a new instance of SidekiqJobsEnqueuedMatcher.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec/goodies/matchers/sidekiq.rb', line 9

def initialize(actual, worker_class, expected_size = nil, expected_properties = {})
  @actual = actual
  @worker_class = worker_class

  # Size is optional to be passed in
  if expected_size.is_a?(Hash)
    @expected_properties = expected_size
    @expected_size = nil
  else
    @expected_properties = expected_properties
    @expected_size = expected_size
  end

  # Normalize
  @expected_properties.stringify_keys!
end

Instance Attribute Details

#new_jobsObject (readonly)

Returns the value of attribute new_jobs.



7
8
9
# File 'lib/rspec/goodies/matchers/sidekiq.rb', line 7

def new_jobs
  @new_jobs
end

#new_jobs_matching_propertiesObject (readonly)

Returns the value of attribute new_jobs_matching_properties.



7
8
9
# File 'lib/rspec/goodies/matchers/sidekiq.rb', line 7

def new_jobs_matching_properties
  @new_jobs_matching_properties
end

Instance Method Details

#matches?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rspec/goodies/matchers/sidekiq.rb', line 26

def matches?
  # Get jobs before so that we only perform new jobs
  jobs_before = @worker_class.jobs.clone

  # Calls the actual block of the matcher
  @actual.call

  jobs_after = @worker_class.jobs
  @new_jobs = jobs_after - jobs_before

  @new_jobs_matching_properties = @new_jobs.select do |job|
    matched_count = 0

    @expected_properties.each do |key, value|
      case key
      when "args"
        # Coerce to an array unless it's a matcher
        value = Array(value) unless value.respond_to?(:base_matcher)

        expect(Array(job[key])).to match(value)
      when "at"
        # If nil, then we are looking for jobs that aren't scheduled
        if value.nil?
          expect(job.key?("at")).to eq false
        else
          # It's a float in job
          scheduled_at = job[key]

          expect(scheduled_at).to eq(value.to_f)
        end
      when "bid"
        expect(job[key]).to match(value)
      when "queue", "unique_for", "unique_until"
        expect(job[key].to_s).to eq(value.to_s)
      when "metadata"
        # Even though we call this metadata, it's actually just keys in the job hash. We iterate through each key so
        # that it also works on checking for nil values
        value.each do |, |
          expect(job[]).to eq 
        end
      end

      matched_count += 1
    rescue RSpec::Expectations::ExpectationNotMetError
      # Doesn't contribute to matched_count if any expectations fail
    end

    matched_count == @expected_properties.count
  end

  # Check expected number of new jobs enqueued to match size if specified
  if @expected_size
    @new_jobs_matching_properties.size == @expected_size
  else
    @new_jobs_matching_properties.any?
  end
end

#new_jobs_sanitizedObject



84
85
86
# File 'lib/rspec/goodies/matchers/sidekiq.rb', line 84

def new_jobs_sanitized
  (@new_jobs || []).map { |j| j.except("jid", "backtrace", "retry", "created_at", "enqueued_at") }
end