Skip to content
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

Adding factory to ExtManagementSystem with invalid credentials #16781

Conversation

douglasgabriel
Copy link
Member

This PR adds a factory to an ExtManagementSystem with invalid credentials

@Fryguy
Copy link
Member

Fryguy commented Jan 9, 2018

@chrisarcand Please review.

@Fryguy Fryguy assigned chrisarcand and unassigned bdunne Jan 9, 2018
Copy link
Member

@chrisarcand chrisarcand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a specific separate factory for ems_physical_infra, this should utilize FactoryGirl traits; you can see a couple in use within this factory file.

Just make a trait called :with_invalid_authentication which only has your after block with the authentication(s); then in your spec, use FactoryGirl.create(:ems_physical_infra, :with_invalid_authentication). This has the added benefit of now being able to have invalid authentication for any of the other ems types.

@douglasgabriel douglasgabriel force-pushed the add_factory_ems_ph_with_invalid_credentials branch 4 times, most recently from de9a376 to 0604e93 Compare January 10, 2018 11:35
@douglasgabriel
Copy link
Member Author

Awesome! It's done @chrisarcand

after(:create) do |x|
x.authentications = [FactoryGirl.build(:authentication, :resource => x, :status => "invalid")]
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right idea, but why inside the :ems_physical_infra factory? These authentications are the same for all providers, no? If you place it next to the other traits in :ext_management_system, this trait can then be used with all the other child factories in this file (including :ems_physical_infra).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, nice!

:parent => :ext_management_system do
trait :with_invalid_authentication do
after(:create) do |x|
x.authentications = [FactoryGirl.build(:authentication, :resource => x, :status => "invalid")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer:

x.authentications << FactoryGirl.build(:authentication, :status => "invalid")

@douglasgabriel douglasgabriel force-pushed the add_factory_ems_ph_with_invalid_credentials branch from 0604e93 to 6d5c25c Compare January 10, 2018 12:43
:parent => :ext_management_system do
end

factory :ems_physical_infra_with_invalid_authentication,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like something went wrong during a rebase and brought this factory back as well as all of the do / end blocks I deleted :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think this should be the end of those blocks, indeed.

@douglasgabriel douglasgabriel force-pushed the add_factory_ems_ph_with_invalid_credentials branch 2 times, most recently from 82e7f66 to 988204b Compare January 10, 2018 13:01

trait :with_invalid_authentication do
after(:create) do |x|
x.authentications << FactoryGirl.build(:authentication, :resource => x, :status => "invalid")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Last thing: It's good to reach for build first when you can, but one to many relationships may or may not work with initialized-but-not-persisted objects on them (as often, you're testing something where something else does a lookup on this authentications, for example), depending on what you're doing. As this is an optional trait that won't be used unless people explicitly want it, you likely want to use FactoryGirl.create here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it! create is really better in this case, thanks for pointing

@douglasgabriel douglasgabriel force-pushed the add_factory_ems_ph_with_invalid_credentials branch from 988204b to 2b23145 Compare January 10, 2018 13:44
@miq-bot
Copy link
Member

miq-bot commented Jan 10, 2018

Checked commit douglasgabriel@2b23145 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
1 file checked, 0 offenses detected
Everything looks fine. 🍪


trait :with_invalid_authentication do
after(:create) do |x|
x.authentications << FactoryGirl.create(:authentication, :resource => x, :status => "invalid")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you use the << operator on an association, you shouldn't need the :resource => x in create

@douglasgabriel douglasgabriel force-pushed the add_factory_ems_ph_with_invalid_credentials branch from 2b23145 to 49ec4be Compare January 10, 2018 14:51
Copy link
Member

@bdunne bdunne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM Thanks @douglasgabriel

@bdunne bdunne merged commit c025d68 into ManageIQ:master Jan 10, 2018
@bdunne bdunne assigned bdunne and unassigned chrisarcand Jan 10, 2018
@bdunne bdunne added this to the Sprint 77 Ending Jan 15, 2018 milestone Jan 10, 2018
@douglasgabriel douglasgabriel deleted the add_factory_ems_ph_with_invalid_credentials branch January 10, 2018 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants