FactoryGroup
By CodeBrahma.
FactoryGroup provides an abstraction on top of factory_girl, which will help you create reusable groups of factories which can be used across test cases.
For now we support only rails applications.
Installation
Add this line to your application's Gemfile:
gem 'factory_group', :group => :test
And then execute:
$ bundle
Or install it yourself as:
$ gem install factory_group
Usage
Define a factory group
# spec/factory_groups/user_group.rb
FactoryGroup.define :user_group do
user FactoryGirl.create(:user, :name => "Rajinikant")
end
Next require the factory_groups folder from the spec_helper
# spec/spec_helper.rb
Dir["spec/factory_groups/**/*.rb"].each { |f| require File.(f) }
Now from your spec call FactoryGroup.create(:user_group)
# spec/user_spec.rb
describe User do
let(:user_group) { FactoryGroup.create(:user_group) }
context "#name" do
it "returns Rajinikant" do
(user_group.user.name).to eq "Rajinikant"
end
end
end
Contributing
- Fork it ( https://github.com/[my-github-username]/factory_group/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request