Class: BeanCounter::EnqueuedExpectation
- Inherits:
-
Object
- Object
- BeanCounter::EnqueuedExpectation
- Extended by:
- Forwardable
- Defined in:
- lib/bean_counter/enqueued_expectation.rb
Instance Attribute Summary collapse
-
#expected ⇒ Hash
readonly
The Hash of options given at instantiation that the expectation expects when matching.
-
#expected_count ⇒ Numeric, Range
readonly
The number of matching jobs the expectation expects.
-
#found ⇒ Array<Strategy::Job>
readonly
The jobs found by the expecation during matching.
Instance Method Summary collapse
-
#collection_matcher(collection) ⇒ Boolean
Iterates over the provided collection searching for jobs matching the Hash of expected options provided during instantiation.
-
#expected_count? ⇒ Boolean
Returns a Boolean indicating whether a specific number of jobs are expected.
-
#failure_message ⇒ String
Builds the failure message used in the event of a positive expectation failure.
-
#initialize(expected) ⇒ EnqueuedExpectation
constructor
Create a new enqueued expectation.
-
#matches?(given = nil) ⇒ Boolean
Checks the beanstalkd pool for jobs matching the Hash of expected options provided at instantiation.
-
#negative_failure_message ⇒ String
Builds the failure message used in the event of a negative expectation failure.
-
#proc_matcher(block) ⇒ Boolean
Evaluates jobs enqueued during the execution of the provided block to determine if any jobs were enqueued that match the expected options provided at instantiation.
Constructor Details
#initialize(expected) ⇒ EnqueuedExpectation
Create a new enqueued expectation. Uses the given expected
Hash to determine
if any jobs are enqueued that match the expected options.
Each key
in expected
is a String or a Symbol that identifies an attribute
of a job that the corresponding value
should be compared against. All attribute
comparisons are performed using the triple-equal (===) operator/method of
the given value
.
expected
may additionally include a count
key of 'count' or :count that
can be used to specify that a particular number of matching jobs are found.
See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of attributes that can be used when matching.
92 93 94 95 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 92 def initialize(expected) @expected = expected @expected_count = [expected.delete(:count), expected.delete('count')].compact.first end |
Instance Attribute Details
#expected ⇒ Hash (readonly)
The Hash of options given at instantiation that the expectation expects when matching.
10 11 12 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 10 def expected @expected end |
#expected_count ⇒ Numeric, Range (readonly)
The number of matching jobs the expectation expects
14 15 16 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 14 def expected_count @expected_count end |
#found ⇒ Array<Strategy::Job> (readonly)
The jobs found by the expecation during matching
18 19 20 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 18 def found @found end |
Instance Method Details
#collection_matcher(collection) ⇒ Boolean
Iterates over the provided collection searching for jobs matching the Hash of expected options provided during instantiation.
28 29 30 31 32 33 34 35 36 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 28 def collection_matcher(collection) @found = collection.send(expected_count? ? :select : :detect) do |job| strategy.job_matches?(job, expected) end @found = [@found] unless expected_count? || @found.nil? expected_count? ? expected_count === found.to_a.length : !found.nil? end |
#expected_count? ⇒ Boolean
Returns a Boolean indicating whether a specific number of jobs are expected.
43 44 45 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 43 def expected_count? return !!expected_count end |
#failure_message ⇒ String
Builds the failure message used in the event of a positive expectation failure.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 53 def if found.nil? found_count = 'none' found_string = nil else materialized_found = found.to_a found_count = "#{materialized_found.length}:" found_string = materialized_found.map {|job| strategy.pretty_print_job(job) }.join("\n") end [ "expected #{expected_count || 'any number of'} jobs matching #{expected.to_s},", "found #{found_count}", found_string, ].compact.join(' ') end |
#matches?(given = nil) ⇒ Boolean
Checks the beanstalkd pool for jobs matching the Hash of expected options provided at instantiation.
If no count
option is provided, the expectation succeeds if any job is found
that matches all of the expected options. If no jobs are found that match the
expected options, the expecation fails.
If a count
option is provided the expectation only succeeds if the triple-equal
(===) operator/method of the value of count
evaluates to true when given the
total number of matching jobs. Otherwise the expecation fails. The use of ===
allows for more advanced comparisons using Procs, Ranges, Regexps, etc.
See Strategy#job_matches? and/or the #job_matches? method of the strategy in use for more detailed information on how it is determined whether or not a job matches the options expected.
See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of attributes that can be used when matching.
125 126 127 128 129 130 131 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 125 def matches?(given = nil) if given.kind_of?(Proc) return proc_matcher(given) else return collection_matcher(strategy.jobs) end end |
#negative_failure_message ⇒ String
Builds the failure message used in the event of a negative expectation failure
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 139 def return '' if found.nil? || found == [] found_count = found.length found_string = found.map {|job| strategy.pretty_print_job(job) }.join("\n") if expected_count? job_count = expected_count job_word = expected_count == 1 ? 'job' : 'jobs' else job_count = 'any' job_word = 'jobs' end return [ "did not expect #{job_count} #{job_word} matching #{expected.to_s},", "found #{found_count}:", found_string, ].join(' ') end |
#proc_matcher(block) ⇒ Boolean
Evaluates jobs enqueued during the execution of the provided block to determine if any jobs were enqueued that match the expected options provided at instantiation
167 168 169 170 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 167 def proc_matcher(block) new_jobs = strategy.collect_new_jobs(&block) return collection_matcher(new_jobs) end |