2018-10-07 21:45:51 -04:00
|
|
|
describe "global initialize_with" do
|
2012-05-12 18:53:17 -04:00
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_class("User") do
|
2015-10-30 22:49:32 -04:00
|
|
|
attr_accessor :name
|
2012-05-12 18:53:17 -04:00
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
define_class("Post") do
|
2012-05-12 18:53:17 -04:00
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-05-12 18:53:17 -04:00
|
|
|
initialize_with { new("initialize_with") }
|
|
|
|
|
|
|
|
trait :with_initialize_with do
|
|
|
|
initialize_with { new("trait initialize_with") }
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :user do
|
|
|
|
factory :child_user
|
|
|
|
|
|
|
|
factory :child_user_with_trait do
|
|
|
|
with_initialize_with
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :post do
|
|
|
|
factory :child_post
|
|
|
|
|
|
|
|
factory :child_post_with_trait do
|
|
|
|
with_initialize_with
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "handles base initialize_with" do
|
|
|
|
expect(FactoryBot.build(:user).name).to eq "initialize_with"
|
|
|
|
expect(FactoryBot.build(:post).name).to eq "initialize_with"
|
2012-05-12 18:53:17 -04:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "handles child initialize_with" do
|
|
|
|
expect(FactoryBot.build(:child_user).name).to eq "initialize_with"
|
|
|
|
expect(FactoryBot.build(:child_post).name).to eq "initialize_with"
|
2012-05-12 18:53:17 -04:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "handles child initialize_with with trait" do
|
|
|
|
expect(FactoryBot.build(:child_user_with_trait).name).to eq "trait initialize_with"
|
|
|
|
expect(FactoryBot.build(:child_post_with_trait).name).to eq "trait initialize_with"
|
2012-05-12 18:53:17 -04:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "handles inline trait override" do
|
|
|
|
expect(FactoryBot.build(:child_user, :with_initialize_with).name).to eq "trait initialize_with"
|
|
|
|
expect(FactoryBot.build(:child_post, :with_initialize_with).name).to eq "trait initialize_with"
|
2012-05-12 18:53:17 -04:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "uses initialize_with globally across FactoryBot.define" do
|
|
|
|
define_class("Company") do
|
2012-05-12 18:53:17 -04:00
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-05-12 18:53:17 -04:00
|
|
|
factory :company
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
expect(FactoryBot.build(:company).name).to eq "initialize_with"
|
|
|
|
expect(FactoryBot.build(:company, :with_initialize_with).name).to eq "trait initialize_with"
|
2012-05-12 18:53:17 -04:00
|
|
|
end
|
|
|
|
end
|