Factory Girl
Defining Factories
Section titled “Defining Factories”If you have a ActiveRecord User class with name and email attributes, you could create a factory for it by making the FactoryGirl guess it:
FactoryGirl.define do factory :user do # it will guess the User class name "John" email "john@example.com" endendOr you can make it explicit and even change its name:
FactoryGirl.define do factory :user_jack, class: User do name "Jack" email "jack@example.com" endendThen in your spec you can use the FactoryGirl’s methods with these, like this:
# To create a non saved instance of the User class filled with John's databuild(:user)# and to create a non saved instance of the User class filled with Jack's databuild(:user_jack)The most common methods are:
# Build returns a non saved instanceuser = build(:user)
# Create returns a saved instanceuser = create(:user)
# Attributes_for returns a hash of the attributes used to build an instanceattrs = attributes_for(:user)