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
Written by Phil Cohen
Related protips
2 Responses
Very nice
over 1 year ago
·
You must checkout our new gem https://github.com/xinminlabs/synvert
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#