Awesome linter for FactoryBot
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
Installation
Add these line to your application's Gemfile:
gem 'factory_bot-awesome_linter'
Usage
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)
Rake task
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'
TODO
- Add tests
Contributing
- 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
Lint
bundle exec rubocop
License & credits
Inspired by the awesome work from Thoughtbot.
Please see LICENSE for further details.
Contributors: ./graphs/contributors