Skip to content

Instantly share code, notes, and snippets.

@adamcrown
Created May 10, 2012 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrown/2655278 to your computer and use it in GitHub Desktop.
Save adamcrown/2655278 to your computer and use it in GitHub Desktop.
RSpec Rails Validation Helper
# 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