Created
August 24, 2014 22:09
-
-
Save tcannonfodder/2b35449aacd76dbd1c54 to your computer and use it in GitHub Desktop.
Helpful test assertions for MiniTest suite in Rails
This file contains hidden or 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
# Some very helpful asserts to add on top of MiniTest, includes ordered array | |
# and set comparison, ActiveRecord attribute validation, and layout assertions | |
# This was originally written by @madrobby for Freckle: https://letsfreckle.com | |
module MiniTest | |
module Assertions | |
def assert_layout(layout, message=nil) | |
assert_equal "layouts/#{layout}", @response.layout, message | |
end | |
def assert_equal_list(expected, actual, message=nil) | |
if expected == actual | |
assert expected == actual | |
else | |
if (expected-actual).empty? | |
assert expected == actual, message || | |
"Lists have the same items, but not in the same order.\n\n#{diff(expected,actual)}" | |
else | |
assert_equal_set expected, actual | |
end | |
end | |
end | |
# to_a converts hashes into arrays for comparison | |
def assert_equal_set(expected, actual, message=nil) | |
assert (expected.to_a-actual.to_a).empty? && (actual.to_a-expected.to_a).empty?, message || | |
"Sets don't have the same items.\n\nMissing in actual:\n #{(expected.to_a-actual.to_a).inspect}\n\nExtra set items:\n #{(actual.to_a-expected.to_a).inspect}" | |
end | |
def assert_valid_attribute(object, attribute, value) | |
message = "Unexpected errors on #{attribute.inspect} with value `#{value.inspect}' after validation" | |
object.send("#{attribute}=", value) | |
object.valid? | |
if object.errors[attribute].kind_of? Array | |
assert object.errors[attribute].empty?, message | |
else | |
assert object.errors.on(attribute).nil?, message | |
end | |
end | |
def assert_invalid_attribute(object, attribute, value) | |
message = "Expected errors on #{attribute.inspect} with value `#{value.inspect}' after validation" | |
object.send("#{attribute}=", value) | |
object.valid? | |
if object.errors[attribute].kind_of? Array | |
assert !object.errors[attribute].empty?, message | |
else | |
assert !object.errors.on(attribute).nil?, message | |
end | |
end | |
def assert_valid(record) | |
assert record.valid?, "Expected record to be valid, but it is not: #{record.errors.full_messages.join("\n")}" | |
end | |
def assert_not_valid(record) | |
assert !record.valid?, 'Expected record to be not valid, but it is.' | |
end | |
def assert_valid_with(record, attribute, value) | |
current_value = record.send(attribute) | |
record.send("#{attribute.to_s}=", value) | |
if record.valid? | |
assert true | |
else | |
errors = record.errors.on(attribute) | |
assert errors.nil? || errors.empty?, "Expected #{value.inspect} to be valid for attribute #{attribute}: #{record.errors.full_messages.join("\n")}" | |
end | |
record.send("#{attribute.to_s}=", current_value) | |
end | |
def assert_not_valid_with(record, attribute, value) | |
current_value = record.send(attribute) | |
record.send("#{attribute.to_s}=", value) | |
record.valid? | |
assert !record.errors.nil? && !record.errors.empty? && | |
!record.errors.on(attribute).nil? && !record.errors.on(attribute).empty?, | |
"Expected #{value.inspect} to be not valid for attribute #{attribute}, but it is." | |
record.send("#{attribute.to_s}=", current_value) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment