-
Notifications
You must be signed in to change notification settings - Fork 898
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
Validate towhat policy field #18032
Validate towhat policy field #18032
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -307,4 +307,22 @@ | |
) | ||
end | ||
end | ||
|
||
context '.validates' do | ||
it 'validates towhat' do | ||
expect(described_class.create!(:towhat => "Host", | ||
:active => false, | ||
:description => 'x',)).to have_attributes(:towhat => "Host", | ||
:active => false) | ||
end | ||
|
||
it 'reports invalid towhat' do | ||
expect do | ||
described_class.create!(:towhat => "BobsYourUncle", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar here expect(FactoryGirl.build(:miq_policy, :towhat => "BobsYourUncle")).not_to be_valid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bdunne Here again, if you feel strongly I'd be glad to change this test but for me it's much easier to quickly grasp what the test is doing, that a particular exception is expected and it's more consistent with existing tests in the spec. Let me know if you are OK with it as is. Thank you for the review feedback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be ok with testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jrafanie Thank you for explaining. Will do. JoeV |
||
:active => false, | ||
:description => 'x') | ||
end.to raise_error(ActiveRecord::RecordInvalid, | ||
"Validation failed: MiqPolicy: Towhat is not included in the list") | ||
end | ||
end | ||
end |
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.
You don't need to save the record to test this, you can
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.
Should
mode
even be tested here since it didn't change in this PR?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'm usually not a huge fan of this syntax, but you could also
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.
@bdunne Thank you for the input. I'll have the new test focus on
towhat
I like the syntax the way it is and if you don't have strong feelings about it I'd like to leave it as is, mostly because it's more consistent with the tests through the spec.
Let me know if you are OK with leaving the test as is.
Thank you. JoeV
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 guess I don't see the point of writing to the database if we just want to make sure the instance is valid. Also, using
FactoryGirl
makes it much more obvious that you're only testing:towhat
and not any other validation.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 guess the question is: are you testing that it's valid or testing if it raises an exception. If you're testing validity: you don't need to create the record, you can do as @bdunne suggests. If you're testing exception raising, you need to
create!
It looks like your testing validity so
create!
isn't needed here. It's minor and I know other code does this but it's fine to mix patterns in the specs until you can convert the rest to the new way.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.
@bdunne and @jrafanie Ah I see the reasoning now. Thank you for explaining. Will do.
JoeV
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.
yeah, I prefer expecting the specific thing (validity) rather than the side effect of rails raising an error when calling
create!
on an invalid instance.