Created
May 27, 2017 04:53
-
-
Save kieraneglin/5df143357d2b7fcb07fd277449f939cc to your computer and use it in GitHub Desktop.
assert_differences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def assert_differences(expression_array, by:, message: nil, &block) | |
expressions = Array(expression_array) | |
change_by = Array(by).map(&:to_i) | |
if expressions.size != change_by.size | |
raise ArgumentError, 'Each expression must have a corresponding value to change by' | |
end | |
exps = expressions.map.with_index do |e, i| | |
callable = e.respond_to?(:call) ? e : lambda { eval(e, block.binding) } | |
{ | |
callable: callable, | |
before: callable.call, | |
expected_change: change_by[i] | |
} | |
end | |
retval = yield | |
expressions.zip(exps).each do |code, e| | |
difference = e[:expected_change] | |
error = "#{code.inspect} didn't change by #{difference}" | |
error = "#{message}.\n#{error}" if message | |
assert_equal(e[:before] + difference, e[:callable].call, error) | |
end | |
retval | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote about this and how to implement it in a blog post: https://kierancodes.com/blog/assert-multiple-differences-in-minitest-with-assert_differences