qmgr5q
Last Updated: February 25, 2016
·
4.987K
· phlipper
B61ce134adf2d6eb1d1f1d11122d6142

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
end

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

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) }
end

2 Responses
Add your response

161
2d8b1d444348889b37029644d54da31b

Very nice

over 1 year ago ·
13597
13c60e2a5806406b537daa542d3495da

You must checkout our new gem https://github.com/xinminlabs/synvert

over 1 year ago ·