Skip to content

Instantly share code, notes, and snippets.

@abarrak
Last active April 17, 2017 23:14
Show Gist options
  • Select an option

  • Save abarrak/379ed9ba1f3f67e725f2b5f61c829331 to your computer and use it in GitHub Desktop.

Select an option

Save abarrak/379ed9ba1f3f67e725f2b5f61c829331 to your computer and use it in GitHub Desktop.
Assert Difference With Multiple Count Values
# Runs assert_difference with a number of conditions and varying difference
# counts.
#
# Call as follows:
#
# assert_differences([['Model1.count', 2], ['Model2.count', 3]])
#
def assert_differences(expression_array, message = nil, &block)
  b = block.send(:binding)
  before = expression_array.map { |expr| eval(expr[0], b) }

  yield

  expression_array.each_with_index do |pair, i|
    e = pair[0]
    difference = pair[1]
    error = "#{e.inspect} didn't change by #{difference}"
    error = "#{message}\n#{error}" if message
    assert_equal(before[i] + difference, eval(e, b), error)
  end
end

Credits:

Assert Difference With Multiple Count Values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment