Module: BeanCounter::TestAssertions

Defined in:
lib/bean_counter/test_assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_enqueued(options = {}) ⇒ true

Asserts that some number of jobs are enqueued that match the given Hash of options. If no options are given, asserts that at least one job exists.

Each key in options 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.

options may additionally include a count key ('count' or :count) that can be used to assert that a particular number of matching jobs are found.

If no count option is provided, the assertion succeeds if any job is found that matches all of the options given. If no jobs are found that match the options given, the assertion fails.

If a count option is provided the assertion 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 assertion fails. The use of === allows for more advanced comparisons using Procs, Ranges, Regexps, etc.

See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of what attributes can be used when matching.

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 given options.

Examples:

assert_enqueued

assert_enqueued(:body => /test/, :count => 5)

assert_enqueued('state' => 'buried', 'count' => 3)

Parameters:

  • options (defaults to: {})

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] Options expected when evaluating match assertion.

Options Hash (options):

  • :count (Numeric, Range) — default: nil

    A particular number of matching jobs expected

Returns:

  • (true)

    if the assertion succeeds

See Also:



44
45
46
# File 'lib/bean_counter/test_assertions.rb', line 44

def assert_enqueued(options = {})
  enqueue_expectation(:assert, options)
end

#assert_enqueues(options = {}) { ... } ⇒ true

Asserts that the provided block enqueues some number of jobs that match the given Hash of options. If no options are given, asserts that at least one job is enqueued during the execution of the block.

Unlike #assert_enqueued, which will evaluate all jobs in the Beanstalkd pool, this method will only evaluate jobs that were enqueued during the execution of the given block. This can be useful for performance and for making assertions that could return false positives if all jobs were evaluated.

Each key in options 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.

options may additionally include a count key ('count' or :count) that can be used to assert that a particular number of matching jobs are found.

If no count option is provided, the assertion succeeds if any job is found that matches all of the options given. If no jobs are found that match the options given, the assertion fails.

If a count option is provided the assertion 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 assertion fails. The use of === allows for more advanced comparisons using Procs, Ranges, Regexps, etc.

See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of what attributes can be used when matching.

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 given options.

Examples:

assert_enqueues(:body => /test/) do
  enqueue_some_jobs
end

Parameters:

  • options (defaults to: {})

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] Options expected when evaluating match assertion.

Options Hash (options):

  • :count (Numeric, Range) — default: nil

    A particular number of matching jobs expected

Yields:

  • No value is yielded to the provided block.

Returns:

  • (true)

    if the assertion succeeds

Raises:

  • (ArgumentError)

    if no block is given.

See Also:



97
98
99
100
101
# File 'lib/bean_counter/test_assertions.rb', line 97

def assert_enqueues(options = {})
  raise ArgumentError, 'Block required' unless block_given?

  enqueue_expectation(:assert, options, &Proc.new)
end

#assert_tube(options = {}) ⇒ true

Asserts that at least one tube exist that matches the given Hash of options. If no options are given, asserts that at least one tube exists, which will always succeed.

Each key in options is a String or a Symbol that identifies an attribute of a tube that the corresponding value should be compared against. All attribute comparisons are performed using the triple-equal (===) operator/method of the given value.

The assertion succeeds if any tube exists that matches all of the given options. If no tube exists matching all of the given options, the assertion fails.

See Strategy::MATCHABLE_TUBE_ATTRIBUTES for a list of those attributes that can be used when matching.

See Strategy#tube_matches? and/or the #tube_matches? method of the strategy in use for more detailed information on how it is determined whether or not a tube matches the given options.

Examples:

assert_tube

assert_tube(:name => /test/)

assert_tube('current-jobs-ready' => 1..10)

Parameters:

  • options (Hash{Symbol, String => Numeric, Proc, Range, Regexp, String, Symbol}) (defaults to: {})

    Options expected when evaluating match assertion.

Returns:

  • (true)

    if the assertion succeeds

See Also:



135
136
137
# File 'lib/bean_counter/test_assertions.rb', line 135

def assert_tube(options = {})
  tube_expectation(:assert, :failure_message, options)
end

#enqueue_expectation(assertion_method, options, &block) ⇒ Object (private)

Builds job expectation object, evaluates, and makes appropriate assertion with appropriate failure message.



261
262
263
264
265
266
# File 'lib/bean_counter/test_assertions.rb', line 261

def enqueue_expectation(assertion_method, options, &block)
  message_type = assertion_method == :assert ? :failure_message : :negative_failure_message
  expectation = BeanCounter::EnqueuedExpectation.new(options)
  match = expectation.matches?(block_given? ? block : nil)
  send(assertion_method, match, expectation.send(message_type))
end

#refute_enqueued(options = {}) ⇒ false

Asserts that no jobs are enqueued that match the given Hash of options. If no options are given, asserts that no jobs exist.

Each key in options 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.

The refutation succeeds if no jobs are found that match all of the options given. If any matching jobs are found, the refutation fails.

See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of what attributes can be used when matching.

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 given options.

Examples:

refute_enqueued

refute_enqueued(:body => lambda {|body| body.length > 50})

refute_enqueued('state' => 'buried')

Parameters:

  • options (defaults to: {})

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] Options expected when evaluating match assertion.

Returns:

  • (false)

    if the refutation succeeds

See Also:



170
171
172
# File 'lib/bean_counter/test_assertions.rb', line 170

def refute_enqueued(options = {})
  enqueue_expectation(:refute, options)
end

#refute_enqueues(options = {}) { ... } ⇒ false

Asserts that the given block enqueues no jobs that match the given Hash of options. If no options are given, asserts that no job are enqueued by the given block.

Unlike #refute_enqueued, which will evaluate all jobs in the beanstalkd pool, this method will only evaluate jobs that were enqueued during the execution of the given block. This can be useful for performance and for making assertions that could return false positives if all jobs were evaluated.

Each key in options 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.

The refutation succeeds if no jobs are found that match all of the options given. If any matching jobs are found, the refutation fails.

See Strategy::MATCHABLE_JOB_ATTRIBUTES for a list of what attributes can be used when matching.

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 given options.

Examples:

refute_enqueues do
  do_some_work_that_should_not_enqueue_any_jobs
end

Parameters:

  • options (defaults to: {})

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] Options expected when evaluating match assertion.

Yields:

  • The provided block is given no parameters.

Returns:

  • (false)

    if the refutation succeeds

Raises:

  • (ArgumentError)

See Also:



211
212
213
214
215
# File 'lib/bean_counter/test_assertions.rb', line 211

def refute_enqueues(options = {})
  raise ArgumentError, 'Block required' unless block_given?

  enqueue_expectation(:refute, options, &Proc.new)
end

#refute_tube(options = {}) ⇒ false

Asserts that no tube exist that matches the given Hash of options. If no options are given, asserts that no tubes exist which will always fail.

Each key in options is a String or a Symbol that identifies an attribute of a tube that the corresponding value should be compared against. All attribute comparisons are performed using the triple-equal (===) operator/method of the given value.

The refutation succeeds if no tube exists that matches all of the given options. If any tubes exist that match all of the given options, the refutation fails.

See Strategy::MATCHABLE_TUBE_ATTRIBUTES for a list of those attributes that can be used when matching.

See Strategy#tube_matches? and/or the #tube_matches? method of the strategy in use for more detailed information on how it is determined whether or not a tube matches the given options.

Examples:

refute_tube
  #=> always fails

refute_tube(:name => /production/)

refute_tube('current-jobs-ready' => 0)

Parameters:

  • options (defaults to: {})

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] Options expected when evaluating match assertion.

Options Hash (options):

  • :count (Numeric, Range) — default: nil

    A particular number of matching jobs expected

Returns:

  • (false)

    if the refutation succeeds

See Also:



252
253
254
# File 'lib/bean_counter/test_assertions.rb', line 252

def refute_tube(options = {})
  tube_expectation(:refute, :negative_failure_message, options)
end

#tube_expectation(assertion_method, message_type, options) ⇒ Object (private)

Builds tube expectation object, evaluates, and makes appropriate assertion with appropriate failure message.



271
272
273
274
275
# File 'lib/bean_counter/test_assertions.rb', line 271

def tube_expectation(assertion_method, message_type, options)
  expectation = BeanCounter::TubeExpectation.new(options)
  match = expectation.matches?
  send(assertion_method, match, expectation.send(message_type))
end