Adds a new formatting option to rspec that counts your ActiveRecord queries and object creations.
Why? Because database interaction is really slow, and careless creation of large graphs of objects is a primary cause of insanely slow test suites. This project can help you diagnose where you're doing the most damage so that you can start to fix it.
This library plays nicely with DatabaseCleaner.
Normal bundle stuff.
gem rspec-activerecord-formatter
The current answer is to change your .rspec
initializer to include the following:
--require rails_helper
--format ActiveRecordFormatter
We have to include the rails_helper so that ActiveRecord is loaded prior to trying to load the formatter. That way, we can hook into AR creations.
Once you set the formatter, you should now see the number of objects created and total queries for each of your tests:
You'll also get a summary at the end of your test run:
- The method I was using to count AR objects doesn't work well with DatabaseCleaner when not explicitly wiring the library into
before
blocks. I'd like to be able to go back to a method other than scanning forINSERT INTO
strings. - Configuration, especially formatting the output to optionally outdent the counts.
- Add a
--profile
-like behavior to output the most offending tests. - The current dependency versions are a vague guess. They can and should clearly be more lenient.
- I dunno, tests.
Contributions are very welcome. Fork, fix, submit pulls.
Contribution is expected to conform to the Contributor Covenant.
This software is released under the MIT License.