1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/acceptance/global_to_create_spec.rb
Joshua Clayton f7efc610fd Allow to_create defined for all factories
This allows users to define to_create for every factory run through
factory_girl.

    FactoryGirl.define do
      to_create {|instance| instance.persist! }
    end

If you want to override this default, set it per factory or in a trait
(and include the trait).

Closes #341
2012-05-12 18:10:02 -04:00

122 lines
3 KiB
Ruby

require 'spec_helper'
describe 'global to_create' do
before do
define_model('User', name: :string)
define_model('Post', name: :string)
FactoryGirl.define do
to_create {|instance| instance.name = 'persisted!' }
trait :override_to_create do
to_create {|instance| instance.name = 'override' }
end
factory :user do
name 'John Doe'
factory :child_user
factory :child_user_with_trait do
override_to_create
end
end
factory :post do
name 'Great title'
factory :child_post
factory :child_post_with_trait do
override_to_create
end
end
end
end
it 'handles base to_create' do
FactoryGirl.create(:user).name.should == 'persisted!'
FactoryGirl.create(:post).name.should == 'persisted!'
end
it 'handles child to_create' do
FactoryGirl.create(:child_user).name.should == 'persisted!'
FactoryGirl.create(:child_post).name.should == 'persisted!'
end
it 'handles child to_create with trait' do
FactoryGirl.create(:child_user_with_trait).name.should == 'override'
FactoryGirl.create(:child_post_with_trait).name.should == 'override'
end
it 'handles inline trait override' do
FactoryGirl.create(:child_user, :override_to_create).name.should == 'override'
FactoryGirl.create(:child_post, :override_to_create).name.should == 'override'
end
it 'uses to_create globally across FactoryGirl.define' do
define_model('Company', name: :string)
FactoryGirl.define do
factory :company
end
FactoryGirl.create(:company).name.should == 'persisted!'
FactoryGirl.create(:company, :override_to_create).name.should == 'override'
end
end
describe 'global skip_create' do
before do
define_model('User', name: :string)
define_model('Post', name: :string)
FactoryGirl.define do
skip_create
trait :override_to_create do
to_create {|instance| instance.name = 'override' }
end
factory :user do
name 'John Doe'
factory :child_user
factory :child_user_with_trait do
override_to_create
end
end
factory :post do
name 'Great title'
factory :child_post
factory :child_post_with_trait do
override_to_create
end
end
end
end
it 'does not persist any record' do
FactoryGirl.create(:user).should be_new_record
FactoryGirl.create(:post).should be_new_record
end
it 'does not persist child records' do
FactoryGirl.create(:child_user).should be_new_record
FactoryGirl.create(:child_post).should be_new_record
end
it 'honors overridden to_create' do
FactoryGirl.create(:child_user_with_trait).name.should == 'override'
FactoryGirl.create(:child_post_with_trait).name.should == 'override'
end
it 'honors inline trait to_create' do
FactoryGirl.create(:child_user, :override_to_create).name.should == 'override'
FactoryGirl.create(:child_post, :override_to_create).name.should == 'override'
end
end