Created
March 30, 2019 16:14
-
-
Save ydaniju/cbeb4b1ccde4f0914c439ca6bab4f34f to your computer and use it in GitHub Desktop.
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
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