Factory Bot Instruments

Purpose of this gem

Factory Bot is probably the base of your Rails test suite, but how deeply you
understand the models and the associations that are created in your tests?

Factory Bot Instruments help in these three aspects:

Slow test suites: Factory Bot is used for the bulk of tests in Rails. Even a
small performance improvement in one of your factories can dramatically
improve the speed of your overall test suite.

Hint: Run FactoryBot.benchmark_all.

Deeper understanding of the database state: By tracing factory bot and SQL
calls you can get a deeper understanding of what is actually created in your
tests, helping you to debug the issues faster.

Hint: Run FactoryBot.trace { FactoryBot.create(:user) }.

Find issues with missconfigured factories: When a factory is properly set up
it is a bliss to work with it. However, if there is a hidden deep in the
association chain debugging the created model can be a hellish experience.

Hint: Run FactoryBot.trace { FactoryBot.create(:user) } and observe the
chain of calls.

Install

Add the following to your Gemfile:

gem 'factory_bot_instruments'

and run bundle install from your shell.

To install the gem manually from your shell, run:

gem install factory_bot_instruments

Documentation

Benchmarking one Factory Bot model

If you have a user factory, you can benchmark it with:

FactoryBot.benchmark(:user)

By default, the FactoryBot.create(<model>) is called. You can pass :method
to override this:

FactoryBot.benchmark(:user, :method => :build_stubbed)

The above snippet will call FactoryBot.build_stubbed(:user).

Benchmarking all Factory Bot models

To collect benchmarking information from all Factory Bot models:

FactoryBot.benchmark_all

To skip a factory, pass the :except options:

FactoryBot.benchmark_all(:except => [:user])

By default, benchmarks for FactoryBot.create(<model>),
FactoryBot.build(<model>), FactoryBot.build_stubbed(<model>) are
collected. You can override this by passing an array of methods:

Development

After checking out the repo, run bin/setup to install dependencies. Then,
run rake spec to run the tests. You can also run bin/console for an
interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.
To release a new version, update the version number in version.rb, and then
run bundle exec rake release, which will create a git tag for the version,
push git commits and tags, and push the .gem file
to rubygems.org.