Created
May 10, 2012 19:24
-
-
Save adamcrown/2655278 to your computer and use it in GitHub Desktop.
RSpec Rails Validation Helper
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
# Examples: | |
# test_valid_attribute Link, :url => 'http://example.com' | |
# test_invalid_attribute Link, :url => 'OH NOES!!!1!' | |
module ValidationHelpers | |
def test_valid_attribute(model, attr_val_hash) | |
test_attribute(:valid, model, attr_val_hash) | |
end | |
def test_invalid_attribute(model, attr_val_hash) | |
test_attribute(:invalid, model, attr_val_hash) | |
end | |
def test_attribute(test, model, attr_val_hash) | |
test_methods = {:valid => :should, :invalid => :should_not} | |
raise ArgumentError, 'unsupported test type' unless test_methods.keys.include?(test) | |
object = model.new(attr_val_hash) | |
object.valid? | |
attr_val_hash.keys.each do |attr| | |
object.errors[attr].send(test_methods[test], be_empty) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment