Class: BeanCounter::EnqueuedExpectation
- Inherits:
-
Object
- Object
- BeanCounter::EnqueuedExpectation
- Extended by:
- Forwardable
- Defined in:
- lib/bean_counter/enqueued_expectation.rb
Instance Attribute Summary collapse
-
#expected ⇒ Object
readonly
The value that the expectation expects.
-
#expected_count ⇒ Object
readonly
The number of matching jobs the expectation expects.
-
#found ⇒ Object
readonly
The jobs found by the expecation during matching.
Instance Method Summary collapse
-
#collection_matcher(collection) ⇒ Object
Iterates over the provided collection searching for jobs matching the Hash of expected options provided at instantiation.
-
#expected_count? ⇒ Boolean
Returns a Boolean indicating whether a specific number of jobs are expected.
-
#failure_message ⇒ Object
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 ⇒ Object
Builds the failure message used in the event of a negative expectation failure.
-
#proc_matcher(block) ⇒ Object
Monitors the beanstalkd pool for new jobs enqueued during the provided block than passes any collected jobs to the collection matcher 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 BeanCounter::TestAssertions and/or BeanCounter::SpecMatchers for more information.
67 68 69 70 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 67 def initialize(expected) @expected = expected @expected_count = [expected.delete(:count), expected.delete('count')].compact.first end |
Instance Attribute Details
#expected ⇒ Object (readonly)
The value that the expectation expects
8 9 10 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 8 def expected @expected end |
#expected_count ⇒ Object (readonly)
The number of matching jobs the expectation expects
11 12 13 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 11 def expected_count @expected_count end |
#found ⇒ Object (readonly)
The jobs found by the expecation during matching
14 15 16 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 14 def found @found end |
Instance Method Details
#collection_matcher(collection) ⇒ Object
Iterates over the provided collection searching for jobs matching the Hash of expected options provided at instantiation.
19 20 21 22 23 24 25 26 27 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 19 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.
31 32 33 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 31 def expected_count? return !!expected_count end |
#failure_message ⇒ Object
Builds the failure message used in the event of a positive expectation failure
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 38 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 also BeanCounter::TestAssertions and/or BeanCounter::SpecMatchers for additional information.
91 92 93 94 95 96 97 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 91 def matches?(given = nil) if given.kind_of?(Proc) return proc_matcher(given) else return collection_matcher(strategy.jobs) end end |
#negative_failure_message ⇒ Object
Builds the failure message used in the event of a negative expectation failure
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 102 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) ⇒ Object
Monitors the beanstalkd pool for new jobs enqueued during the provided block than passes any collected jobs to the collection matcher to determine if any jobs were enqueued that match the expected options provided at instantiation
126 127 128 129 |
# File 'lib/bean_counter/enqueued_expectation.rb', line 126 def proc_matcher(block) new_jobs = strategy.collect_new_jobs(&block) collection_matcher(new_jobs) end |