Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Generate reports on how FactoryGirl is being used during your test run

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 factory_inspector.gemspec
README.md

FactoryInspector

This very simple gem reports on how FactoryGirl is being used during your test runs. This is useful in understanding where the time is going during your test runs; while FactoryGirl is useful, overuse can lead to serious slowdowns due to a cascade of database writes when building test objects. The analysis relies on the changes brought in with FactoryGirl 3.2

This gem is a developer utility and makes no effort to sanitize inputs or help you avoid making mistakes, but hopefully it's too simple to need much of that!

Installation

Add this line to your application's Gemfile:

gem 'factory_inspector'

And then execute:

$ bundle

Or install it yourself as:

$ gem install factory_inspector

Usage

FactoryInspector's API is just two methods; start_inspection and generate_report

Assuming you're on a RSpec based project, the changes to your spec/spec_helper.rb file to introduce FactoryInspector would be:

  require 'factory_inspector'

  # From a project relative filename like 'log/filename.txt'
  # generate the full path.
  # TODO There must be simpler way to do this?
  def get_project_path_for(filename)
    "#{File.dirname(__FILE__)}/../#{filename}"
  end

  factory_inspector = FactoryInspector.new
  RSpec.configure do |config|

    config.before :suite do
      factory_inspector.start_inspection
    end

    config.after :suite do
      report_name = 'log/factory_inspector_report.txt'
      report_path = get_project_path_for report_name 
      factory_inspector.generate_report report_path
      puts "Factory Inspector report in '#{report_name}'"
    end
  end

After the tests have run, the nominated log file will have output similar to this:

FACTORY INSPECTOR - 46 FACTORIES USED
  FACTORY NAME                TOTAL  OVERALL   TIME PER  LONGEST   STRATEGIES
                              CALLS  TIME (s)  CALL (s)  CALL (s)            
  school_with_terms             1    0.4783    0.47827  0.4783      [:create]
  school_with_terms_and_cla     5    2.3859    0.47718  0.5184      [:create]
  school_leaver                 1    0.2581    0.25808  0.2581      [:create]
  pre_enrolled_pupil            1    0.2570    0.25704  0.2570      [:create]
  pupil_school_enrolment_fo     1    0.2008    0.20075  0.2008      [:build]
  announcement                  5    0.8973    0.17946  0.2327      [:create]
  sub_class_with_pupils         5    0.7961    0.15921  0.2163      [:create, :build]
  etc

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.