Last Updated: February 25, 2016
· phlipper

FactoryGirl syntax sugar for MiniTest

minitest is a great testing library and factory_girl is a handy fixture replace library, especially when working with Rails.

It can get tedious to repeat FactoryGirl. for every invocation of build, create, etcetera. Here is a handy shortcut to help reduce your Carpal Tunnel pain:

# for Test::Unit assertion style
class MiniTest::Rails::ActiveSupport::TestCase
  include FactoryGirl::Syntax::Methods

# for Spec expectation style
class MiniTest::Spec
  include FactoryGirl::Syntax::Methods

Place this in a file such as test/support/factory_girl.rb.

This allows you to use the core set of syntax methods (build, build_stubbed, create, attributes_for, and their *_list counterparts) without having to call them on FactoryGirl directly:

describe Awesome do
  # do this
  subject { create(:awesome) }

  # don't do this
  subject { FactoryGirl.create(:awesome) }

2 Responses
Add your response

Very nice

over 1 year ago ·

You must checkout our new gem

over 1 year ago ·