-
-
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
fix have_db_column.with_options misspelled options #1358
fix have_db_column.with_options misspelled options #1358
Conversation
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.
Hi @rodriggochaves — thank you for this! It's nice when PRs are small :) Just had a few comments below.
@@ -137,6 +138,13 @@ def description | |||
|
|||
protected | |||
|
|||
OPTIONS = %i(precision limit default null scale primary) |
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 putting this at the top of the class? That way it "pops" out.
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.
I moved but I've a question about the visibility of this constant. Shoud I use something to hide like #private_constant
?
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.
Sure, that would make sense if you want to do that.
it 'throws an ArgumentError' do | ||
expect { | ||
have_db_column(:salary).with_options(preccision: 5) | ||
}.to raise_error(ArgumentError) |
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.
What do you think about testing what the error message is going to be rather than what kind of error it is? When you do that you might see that error could be improved a bit. I believe that as invalid_options
is an array, when it gets interpolated it will appear like an array in the message. Not sure if that's what you were going after or not.
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.
Ah! Good observation!! I just changed the test and improved the message raised. Let me know what did you think about it?
|
||
def validate_options(opts) | ||
invalid_options = opts.keys.map(&:to_sym) - OPTIONS | ||
raise ArgumentError, "Unknown option(s) '#{invalid_options}'" unless invalid_options.empty? |
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.
Just a little style thing: we don't tend to use modifiers or unless
— would you mind formatting this as:
if invalid_options.any?
raise ArgumentError, "Unknown option(s) '#{invalid_options}'"
end
(although see note below about possibly reformatting the message)
c5b93bf
to
1659842
Compare
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.
One more little thing but otherwise I'm good with this!
it 'raises an error with the unknown options' do | ||
expect { | ||
have_db_column(:salary).with_options(preccision: 5, primaryy: true) | ||
}.to raise_error("Unknown option(s): 'preccision, primaryy'") |
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.
Okay, so now that you're testing multiple options, it seems like we could improve this message a bit. What if you change the above to
raise ArgumentError, "Unknown option(s): #{invalid_options.map(&:inspect).join(", ")}"
Then you should get something like:
expect {
have_db_column(:salary).with_options(preccision: 5, primaryy: true)
}.to raise_error("Unknown option(s): :preccision, :primaryy")
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.
Ok! I improved the message.
have_db_column().with_options() should raise an ArgumentError if receives a misspelled argument
1659842
to
60a1973
Compare
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.
Sorry for the delay on this. This looks good to me, so thank you! It sounds like we may want to push this to the version after the next immediate one, so I'm going to hold off on merging but this looks ready to go once we are ready to merge.
Thank you very much, @rodriggochaves! @mcmire, I noticed that this PR was selected to be part of the 4.5.0. So, I think we can merge it. |
@vsppedro Ah, sorry! For some reason I thought we were inserting a patch release before 4.5.0. In that case I'll merge this. |
These were introduced after thoughtbot#1358 was merged. Rather bizarrely, RuboCop didn't run when the PR was opened.
These were introduced after #1358 was merged. Rather bizarrely, RuboCop didn't run when the PR was opened.
Hi!! This is my first contribuition to
shoulda-matchers
. If you got any feedback for this PR, I'll be very happy to listen.This PR looks to solve the bug reported here #1281.
Solution descripton
When there is an argument this matcher doesn't expect, it should raise an
ArgumentError
. I created a new protected methodvalidate_options
to be responsible for this.