This gem enhances FactoryBot linter
-
it wraps factories creation:
- within DatabaseCleaner if present
- within ActiveRecord within transaction if DatabaseCleaner is not present but ActiveRecord is
-
it displays a progress bar and quickly display failures (just like fuubar)
-
it shows backtraces when factories failed
-
it lints all factories and traits by default
-
it allows easy selection of factories
-
it reloads cached factories before each run
Add these line to your application's Gemfile:
gem 'factory_bot-awesome_linter'
Just run:
FactoryBot::AwesomeLinter.lint!
All factories are included by default. You can select factories to lint by passing their names, a regexp, or factories:
FactoryBot::AwesomeLinter.lint!(:user)
FactoryBot::AwesomeLinter.lint!(:user, :account)
FactoryBot::AwesomeLinter.lint!(/^new_/)
factories_to_lint = FactoryBot.factories.reject do |factory|
factory.name =~ /^old_/
end
FactoryBot::AwesomeLinter.lint!(*factories_to_lint)
All traits are included by default.
You can exclude them:
FactoryBot::AwesomeLinter.lint!(traits: false)
Default linting strategy is :create
.
You can specify another or multiple strategies used for linting:
FactoryBot::AwesomeLinter.lint!(strategy: :build)
FactoryBot::AwesomeLinter.lint!(strategy: %i[create build_stubbed])
All arguments can be combined:
FactoryBot::AwesomeLinter.lint!(:user, strategy: :build, traits: false)
Create the following task in 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?
abort unless FactoryBot::AwesomeLinter.lint!
else
puts "Wrong environment detected to run factory_bot:lint"
puts "Running `bundle exec bin/rails factory_bot:lint RAILS_ENV='test'` instead"
system("bundle exec bin/rails factory_bot:lint RAILS_ENV='test'")
exit $CHILD_STATUS.exitstatus
end
end
end
Then run :
bundle exec bin/rails factory_bot:lint RAILS_ENV='test'
- Add tests
- Don't hesitate to submit your feature/idea/fix in issues
- Fork the repository
- Create your feature branch
- Ensure Rubocop are passing
- Create a pull request
bundle exec rubocop
Inspired by the awesome work from Thoughtbot.
Please see LICENSE for further details.
Contributors: ./graphs/contributors