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



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

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

#datetime?Boolean

Returns:

  • (Boolean)


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

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

#descriptionObject



50
51
52
# File 'lib/qe/testing/rspec.rb', line 50

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

#does_not_match?(block) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
# File 'lib/qe/testing/rspec.rb', line 39

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_message_for_shouldObject



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

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

#failure_message_for_should_notObject



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

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

#jobsObject



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

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

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/qe/testing/rspec.rb', line 24

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



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

def on(date)
  @date = date
  self
end

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



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

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