-
Notifications
You must be signed in to change notification settings - Fork 981
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
Refactor smart_proxy factories and tests #10143
base: develop
Are you sure you want to change the base?
Conversation
This makes the factories consistently use association, which works properly with build and create strategies. They're implemented as traits so can be mixed. Because they're now consistent, it can use loops to define all features.
before_save :sanitize_url, :associate_features | ||
# But it can be useful to skip in tests | ||
attr_accessor :skip_associate_features | ||
before_save :associate_features, unless: :skip_associate_features |
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 wasn't sure if this was the best way, but it's quite useful in tests to avoid stubbing things.
subnet = FactoryBot.build_stubbed(:subnet_ipv4, :name => 'My subnet') | ||
proxy = FactoryBot.build_stubbed(:smart_proxy, :dhcp, subnets: [subnet]) |
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.
By using build_stubbed
it saves a database hit. It would be interesting to see how much more we can save. I'd love to have pure unit tests that can run with nulldb. Perhaps it could even use build
.
proxy_features.each do |feature| | ||
trait feature do | ||
smart_proxy_features do | ||
[association(:smart_proxy_feature, feature, :smart_proxy => @instance)] |
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.
https://thoughtbot.github.io/factory_bot/cookbook/interconnected-associations.html said you should be able to use instance
, but I only got it to work with @instance
.
This makes the factories consistently use association, which works properly with build and create strategies. They're implemented as traits so can be mixed.
Because they're now consistent, it can use loops to define all features.
Currently a draft because I only did limited testing. If this works well, other factories should be cleaned up in a similar way. They can be made a lot shorter and more powerful. With association you can also use build_stubbed in more places, so eventually we can speed up more tests to avoid database interactions.