Skip to content

Instantly share code, notes, and snippets.

@otaviomedeiros
Created March 14, 2012 00:05
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save otaviomedeiros/2032846 to your computer and use it in GitHub Desktop.
Save otaviomedeiros/2032846 to your computer and use it in GitHub Desktop.
RSpec matcher for validates_with
# RSpec matcher for validates_with.
# https://gist.github.com/2032846
# Usage:
#
# describe User do
# it { should validate_with CustomValidator }
# end
RSpec::Matchers.define :validate_with do |validator|
match do |subject|
subject.class.validators.map(&:class).include? validator
end
description do
"RSpec matcher for validates_with"
end
failure_message_for_should do |text|
"expected to validate with #{validator}"
end
failure_message_for_should_not do |text|
"do not expected to validate with #{validator}"
end
end
@Bartuz
Copy link

Bartuz commented Nov 30, 2015

🎉 thanks

@Bartuz
Copy link

Bartuz commented Dec 1, 2015

changed deprecated methods and added chaining for options hash https://gist.github.com/Bartuz/98abc9301adbc883b510

it { is_expected.to validate_with(MyValidator).with_options(on: :admin) }

@rdh
Copy link

rdh commented Jul 30, 2017

Just FYI, I've included this in my personal gem of spec helpers ... https://github.com/schrodingersbox/spec_cat#credits ... please open an issue if you object or would like any changes to the attribution.

And thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment