Skip to content

Instantly share code, notes, and snippets.

@christiannelson
Created April 28, 2012 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiannelson/2521776 to your computer and use it in GitHub Desktop.
Save christiannelson/2521776 to your computer and use it in GitHub Desktop.
Factory Girl Build and Create
# factory_girl: 3.3.0, ruby 1.9.3-p194
FactoryGirl.define do
sequence(:item_name) { %w( Milk Tea Coffee Danish Sugar Bagel Donut Toast ).sample(1) }
factory :user do |user|
name 'Christian'
email 'christian@example.com'
factory :user_with_profile do
# I would expect this to work, but it doesn't produce a profile in the build case.
#profile
# This works however
association :profile, factory: :profile, strategy: :build
end
end
factory :profile do
description 'All arms and legs.'
born_on { Date.new(1974, 5, 18) }
gender 'male'
end
factory :list do
name 'Groceries'
factory :list_with_items do
ignore do
item_count 5
end
after(:build) do |list, evaluator|
FactoryGirl.build_list(:item, evaluator.item_count, list: list)
end
end
end
factory :item do
name { FactoryGirl.generate(:item_name) }
done false
end
end
class Item < ActiveRecord::Base
belongs_to :list, inverse_of: :items
attr_accessible :name, :done
end
class List < ActiveRecord::Base
attr_accessible :name
has_many :items, inverse_of: :list, dependent: :destroy
end
require 'spec_helper'
# List has_many Items
describe List do
describe "factories" do
describe "create" do
it "should create a valid list without any items" do
list = create(:list)
list.should be_valid
list.should_not be_new_record
list.items.should be_empty
end
it "should create a list with persisted items" do
list = create(:list_with_items)
list.items.should_not be_empty # FAIL: expected empty? to return false, got true
list.items.each do |item|
item.should_not be_new_record
end
end
end
describe "build" do
it "should build a list with unpersisted items" do
list = build(:list_with_items)
list.should be_new_record
list.items.should_not be_empty # FAIL: expected empty? to return false, got true
list.items.each do |item|
item.should be_new_record
end
end
end
end
end
require 'spec_helper'
describe User do
describe "factories" do
describe "create" do
it "should create a valid user" do
user = create(:user)
user.should be_valid
end
it "should create a valid user with a persisted profile" do
user = create(:user_with_profile)
user.should_not be_new_record
user.profile.should be_present
user.profile.should_not be_new_record
user.profile.user.should == user
end
end
describe "build" do
it "should build a valid user" do
user = build(:user)
user.should be_valid
end
it "should build a valid user with an unpersisted profile" do
user = build(:user_with_profile)
user.should be_new_record
user.profile.should be_present
user.profile.should be_new_record # FAIL: expected nil to be a new record
user.profile.user.should == user
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment