https://github.com/sixarm/sixarm_ruby_factory_girl_test_examples
SixArm.com » Ruby » Factory Girl test examples for Rails
https://github.com/sixarm/sixarm_ruby_factory_girl_test_examples
factory factory-girl gem ruby test
Last synced: 12 months ago
JSON representation
SixArm.com » Ruby » Factory Girl test examples for Rails
- Host: GitHub
- URL: https://github.com/sixarm/sixarm_ruby_factory_girl_test_examples
- Owner: SixArm
- License: other
- Created: 2011-03-07T01:33:50.000Z (over 15 years ago)
- Default Branch: main
- Last Pushed: 2025-04-14T09:18:49.000Z (about 1 year ago)
- Last Synced: 2025-04-14T10:29:36.133Z (about 1 year ago)
- Topics: factory, factory-girl, gem, ruby, test
- Language: Ruby
- Homepage: http://sixarm.com
- Size: 32.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Citation: CITATION.cff
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# SixArm.com → Ruby →
Factory Girl test examples for Rails
* Docs:
* Repo:
## Introduction
Factory Girl examples for the ThoughtBot factory_girl gem.
These are useful for our testing Ruby On Rails applications.
For docs go to
Want to help? We're happy to get pull requests.
## Install
To install using a Gemfile, add this:
gem "", ">= ", "< 1"
To install using the command line, run this:
gem install -v ">= , < 1"
To install using the command line with high security, run this:
wget http://sixarm.com/sixarm.pem
gem cert --add sixarm.pem && gem sources --add http://sixarm.com
gem install -v ">= , < 1" --trust-policy HighSecurity
To require the gem in your code:
require ""
## Details
These examples are built independent of any mock data tool.
You will most likely want to change these examples to suit
whichever mock data tool you like to use in your project.
We like these mock data tools:
* forgery: best for custom dictionaries and generators.
* faker: best as a straight port of the Perl Faker module.
* ffaker: faster faker, an optimization of the faker gem.
* random_data: good for plausible anames, ddresses, etc.
## To create a collection of items
Simply call the factory like this:
3.times { Factory(:user) }
## To create a collection for the "many" in a has_many
Do it in Factory Girl after_create on the belonging object.
So if you wanted a test with a Company with 3 Users:
Factory.define :company_with_three_users do |blog|
company.after_create do |belonging|
3.times { Factory(:user, :company => belonging) }
end
end
## To create records that share parameters
Write a helper method:
# in test/factories.rb or spec/factories.rb
def two_companies_for(user = Factory(:user))
[Factory(:company), Factory(:company)]
end
## To connect cucumber steps for dynamic factories
Parse the cucumber step then send the match as a parameter:
Given /^typical (\w+)/ do |name|
Factory(name)
end
## To do more advanced expert setup with callbacks
See this post about callbacks before and after factories:
http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl