Skip to content

Instantly share code, notes, and snippets.

@ydaniju
Created March 30, 2019 16:14
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 ydaniju/cbeb4b1ccde4f0914c439ca6bab4f34f to your computer and use it in GitHub Desktop.
Save ydaniju/cbeb4b1ccde4f0914c439ca6bab4f34f to your computer and use it in GitHub Desktop.
RSpec::Matchers.define :validate_with do |expected_validator|
match do |subject|
@validator = current_validator(subject, expected_validator)
if @validator.options.present?
@validator.options.all? do |_, condition|
subject.instance_eval(&condition)
end
end
end
def current_validator(subject, expected_validator)
subject.class.validators.find do |validator|
validator.class == expected_validator
end
end
description do
"validates with #{expected}"
end
failure_message do
"expected to validate with #{expected}"
end
failure_message_when_negated do
"expected to not validate with #{expected}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment