Class: Qe::EnqueueMatcher::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/qe/testing/rspec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker, scheduled) ⇒ Matcher

Returns a new instance of Matcher.



8
9
10
11
12
# File 'lib/qe/testing/rspec.rb', line 8

def initialize(worker, scheduled)
  @worker = worker
  @options = nil
  @scheduled = scheduled
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



6
7
8
# File 'lib/qe/testing/rspec.rb', line 6

def date
  @date
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/qe/testing/rspec.rb', line 6

def options
  @options
end

#scheduledObject (readonly)

Returns the value of attribute scheduled.



6
7
8
# File 'lib/qe/testing/rspec.rb', line 6

def scheduled
  @scheduled
end

#workerObject (readonly)

Returns the value of attribute worker.



6
7
8
# File 'lib/qe/testing/rspec.rb', line 6

def worker
  @worker
end

Instance Method Details

#build_message(base) ⇒ Object



68
69
70
71
72
# File 'lib/qe/testing/rspec.rb', line 68

def build_message(base)
  base << ((options || {}).empty? ? "" : " with #{options.inspect}")
  base << " on #{date.inspect}" if date
  base
end

#datetime?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/qe/testing/rspec.rb', line 80

def datetime?
  [Date, Time, DateTime].find {|klass| date.kind_of?(klass) }
end

#descriptionObject



54
55
56
# File 'lib/qe/testing/rspec.rb', line 54

def description
  "enqueue job for #{worker.inspect} worker"
end

#does_not_match?(block) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
# File 'lib/qe/testing/rspec.rb', line 43

def does_not_match?(block)
  block.call

  jobs.none? do |job|
    condition = job[:worker] != worker
    condition = condition && job[:options] != (options.kind_of?(Proc) ? options.call : options) if options
    condition = condition && job[:run_at].to_i != date.to_i if date
    condition
  end
end

#failure_messageObject Also known as: failure_message_for_should



58
59
60
# File 'lib/qe/testing/rspec.rb', line 58

def failure_message
  build_message "expect #{worker.inspect} to be enqueued"
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



63
64
65
# File 'lib/qe/testing/rspec.rb', line 63

def failure_message_when_negated
  build_message "expect #{worker.inspect} not to be enqueued"
end

#jobsObject



74
75
76
77
78
# File 'lib/qe/testing/rspec.rb', line 74

def jobs
  Qe.jobs.select do |job|
    scheduled ? job.key?(:run_at) : true
  end
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/qe/testing/rspec.rb', line 28

def matches?(block)
  block.call
  @options = @options.call if @options.respond_to?(:call)

  result = jobs.any? do |job|
    condition = job[:worker] == worker
    condition = condition && datetime? if scheduled
    condition = condition && job[:options] == (options.kind_of?(Proc) ? options.call : options) if options
    condition = condition && job[:run_at].to_i == date.to_i if date
    condition
  end

  !!result
end

#on(date) ⇒ Object



23
24
25
26
# File 'lib/qe/testing/rspec.rb', line 23

def on(date)
  @date = date
  self
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/qe/testing/rspec.rb', line 14

def supports_block_expectations?
  true
end

#with(options = nil, &block) ⇒ Object



18
19
20
21
# File 'lib/qe/testing/rspec.rb', line 18

def with(options = nil, &block)
  @options = block || options
  self
end