Add a Rake task for linting factories
Enhance not only local development but our CI too by adding a Rake task for linting factory_bot
factories:
The starting task can be like that (taken from the official doc):
# lib/tasks/factory_bot.rake
namespace :factory_bot do
desc "Verify that all FactoryBot factories are valid"
task lint: :environment do
if Rails.env.test?
conn = ActiveRecord::Base.connection
conn.transaction do
FactoryBot.lint
raise ActiveRecord::Rollback
end
else
system("bundle exec rake factory_bot:lint RAILS_ENV='test'")
fail if $?.exitstatus.nonzero?
end
end
end
I propose to add as-is but exclude Z::
factories from it as we transition to zuora_remote
factories and old factories can produce many errors:
# lib/tasks/factory_bot.rake
namespace :factory_bot do
desc "Verify that all FactoryBot factories are valid"
task lint: :environment do
if Rails.env.test?
conn = ActiveRecord::Base.connection
conn.transaction do
# Exclude factories from the `z` folder
factories_to_lint = FactoryBot.factories.reject do |factory|
factory.name =~ /^z_/
end
FactoryBot.lint factories_to_lint
raise ActiveRecord::Rollback
end
else
system("bundle exec rake factory_bot:lint RAILS_ENV='test'")
fail if $?.exitstatus.nonzero?
end
end
end
Edited by Vitaly Slobodin