-
-
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
Numerical matchers #244
Numerical matchers #244
Conversation
@TiagoCardoso1983 Maybe I'm misunderstanding something here. In order for me to use this matcher do I have to actually set the attribute by hand? |
Well, yes, according to the AR, you say something like
and the spec should work accordingly
|
👍 |
@TiagoCardoso1983 Yes, you are correct about that in a sense. However, I believe @drapergeek (and correct me if I'm wrong) is referring to the
and the like. Our specs typically don't set attributes like that and test the matcher against those values. The specs should just verify that your matcher actually does the validate the way it's supposed to, e.g. that
actually does exactly
Check out spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb for reference. |
Has this issue been resolved? I ask the question because I'm still seeing the following message:
|
@conradwt We're still waiting for @TiagoCardoso1983 to fix the PR. This will be resolved when they do. |
Sorry for the lateness, was out of the internets for a while. I don't understand exactly the spec issue, but I tried to rewrite it in a more similar way to the other specs. If this doesn't solve the issue, please be more explicit in what it is wrong. |
@TiagoCardoso1983 I think you're still misunderstanding us. Having to set the attribute to a specific value in your test doesn't operate the way all our other tests work. The code should actually set this attribute without it having to happen in the test. The user shouldn't be responsible for that. |
Sorry, I'm really misunderstanding because I still don't see it. How should the spec look like then? I'm testing the matcher against all possible options already. |
Can someone send @TiagoCardoso1983 an example of one of the numeric matchers and the way it should be written as a spec? Another option would be for someone to pair with him to knock this out being that @TiagoCardoso1983 started this task. |
@TiagoCardoso1983 it { should validate_numericality_of(:age).greater_than(18) } because you would have to set your subject to equal 18. Does this make more sense? If you could write a spec that uses the full matcher I think you would understand why this is an issue. |
…. hope this is more in sync with the other specs
Again, sorry for the lateness. I perceived you had a problem with me explicitly assigning the attribute before the match, so I assumed you wanted a more idempotent way of instantiating objects. Please report back to me if this is still not in sync with what you want. |
@TiagoCardoso1983 You're shouldn't define explicit numerical cases, but rather check that the correct validation is being implemented irrespective of the values involved. Consider the shoulda's current test case for a validation requiring an odd number. It doesn't use any digits. Instead it creates a dummy model instance with the validation on the fly... def validating_numericality(options = {})
define_model :example, :attr => :string do
validates_numericality_of :attr, options
end.new
end ...and tests to check that the matcher exists: it 'allows odd number values for that attribute' do
validating_numericality(:odd => true).should matcher.odd
end
it 'rejects when the model does not enforce odd number values' do
validating_numericality.should_not matcher.odd
end |
Just noticed I can't really call a should_not matcher on top of a numerical validation. This: it { should_not validate_numericality_of(:attr) breaks because some negative_failure_message is not defined. Doesn't have anything to do with the matchers I inserted. Should I open an issue on this? |
I think you need a |
Not on the version of the gem I'm working on, these methods do not exist. |
@fny Thanks for your explanation! You're exactly right about what we're looking for in the specs. @TiagoCardoso1983 The |
Conflicts: lib/shoulda/matchers/active_model.rb
…here, also submatchers, returning usually the same result as the main matcher
Is it now good to go? |
module Shoulda # :nodoc: | ||
module Matchers | ||
module ActiveModel # :nodoc: | ||
|
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.
Would you mind tightening up this whitespace a bit? Just remove the empty lines before and after the class comment and right before def initialize
.
@TiagoCardoso1983 I left a couple of comments. You don't seem to have any opposite tests for the validation -- that is, what if you have a model that doesn't What if you do this... have Additionally, can you fold the For instance you might have something like: context 'is_greater_than' do
it "accepts an instance with a validation" do
instance_with_validation(:greater_than => 2).should matcher.is_greater_than(2)
end
it "rejects an instance without a validation" do
instance_without_validations.should_not matcher.is_greater_than(2)
end
end I think if you do that we should be good to go. |
|
||
def initialize(value, operator) | ||
@value = value | ||
@options = { :operator => operator } |
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.
Is there a reason to store this in @options
? Can you not store this in simply @operator
instead?
…cific matcher value
@mcmire , I don't see the use case there. less_than and greater_than are options of the numericality validation, they should be preceded by a numericality validation, I guess. |
@TiagoCardoso1983 Right, I don't expect people to try to use |
@TiagoCardoso1983 Okay so I think we're almost ready to merge this. Do you mind squashing your commits as it makes sense and/or rebasing if necessary before I do so? |
Everything's up-to-date, no need to rebase. I didn't understand what you meant by "squashing commits", but I'd say it is good to go. |
@TiagoCardoso1983, as a rule when we're pulling in a feature, we 'squash' all the commits down into a singlle commit; @mcmire should be able to do this for you before he merges it in so its not a big deal. Thanks for putting this together! |
Merged! |
Is this in the current build (2.1.0) of the gem yet? Or not until 2.2.0? I installed 2.1.0 and get errors saying is_greater_than is not defined (and all of the other new matchers) |
The current v2.1.0 release on Rubygems does not contain the comparison matchers. You'll have to install the gem directly from the repo for now. # in your Gemfile
gem 'shoulda-matchers', github: 'thoughtbot/shoulda-matchers' |
That's what I figured. Thanks. |
Correction to the previous pull request ( #236 ) which was breaking the ruby 1.8 tests