-
-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for validation contexts #246
Conversation
|
||
context "without the validation context" do | ||
it "allows a bad value" do | ||
model.should allow_value("xyz").for(:attr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using your let
above and model.should
here (and lines 43, 47), take advantage of the validating_format
method that's already available (from spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb
). It'll look something like this:
validating_format(:with => /abc/, :on => :customisable).should allow_value("xyz").for(:attr)
I like this idea, and it looks like this would fulfill #131 as well, so that's awesome! However, please rebase and check out my comment on the spec. |
Also, I just noticed that your |
Sorry for the delays, but I've rebased and fixed up those specs—master has moved fast! Should be good to go now. |
👍 |
Thanks @sj26! Merged. |
Any reason why this functionality wasn't added to |
I believe this should work with all matchers, is this not working with validate_numericality_of? |
This is on version 2.3.0. I'll try and open a PR (or at least a separate issue). |
Okay, thanks. |
@anujbiyani did #313 fix your issue? There was an oversight which means some matchers weren't accepting |
Ah, no, I see— ValidateNumericalityOfMatcher doesn't inherit from ValidationMatcher since 6ba8c9f. Have you submitted a pull yet, or shall I? (I don't see anything in your activity so I'll have a stab.) |
There's no easy way to test validation contexts with the current shoulda matchers. The context must be provided in the call to the instance's
valid?
method. This patch adds a simple way to optionally pass a context.Example:
Previously this would have to involve stubbing the implementation detail validation_context, simply setting it is not enough as it is overridden by every run of valid?:
This can also be used for the idiomatic activerecord contexts: