2018-08-04 10:56:37 -04:00
|
|
|
describe "global to_create" do
|
2012-05-12 00:42:44 -04:00
|
|
|
before do
|
2018-08-04 10:56:37 -04:00
|
|
|
define_model("User", name: :string)
|
|
|
|
define_model("Post", name: :string)
|
2012-05-12 00:42:44 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2018-08-04 10:56:37 -04:00
|
|
|
to_create { |instance| instance.name = "persisted!" }
|
2012-05-12 00:42:44 -04:00
|
|
|
|
|
|
|
trait :override_to_create do
|
2018-08-04 10:56:37 -04:00
|
|
|
to_create { |instance| instance.name = "override" }
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
factory :user do
|
2018-08-04 10:56:37 -04:00
|
|
|
name { "John Doe" }
|
2012-05-12 00:42:44 -04:00
|
|
|
|
|
|
|
factory :child_user
|
|
|
|
|
|
|
|
factory :child_user_with_trait do
|
|
|
|
override_to_create
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :post do
|
2018-08-04 10:56:37 -04:00
|
|
|
name { "Great title" }
|
2012-05-12 00:42:44 -04:00
|
|
|
|
|
|
|
factory :child_post
|
|
|
|
|
|
|
|
factory :child_post_with_trait do
|
|
|
|
override_to_create
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
it "handles base to_create" do
|
|
|
|
expect(FactoryBot.create(:user).name).to eq "persisted!"
|
|
|
|
expect(FactoryBot.create(:post).name).to eq "persisted!"
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
it "handles child to_create" do
|
|
|
|
expect(FactoryBot.create(:child_user).name).to eq "persisted!"
|
|
|
|
expect(FactoryBot.create(:child_post).name).to eq "persisted!"
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
it "handles child to_create with trait" do
|
|
|
|
expect(FactoryBot.create(:child_user_with_trait).name).to eq "override"
|
|
|
|
expect(FactoryBot.create(:child_post_with_trait).name).to eq "override"
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
it "handles inline trait override" do
|
|
|
|
user = FactoryBot.create(:child_user, :override_to_create)
|
|
|
|
post = FactoryBot.create(:child_post, :override_to_create)
|
|
|
|
|
|
|
|
expect(user.name).to eq "override"
|
|
|
|
expect(post.name).to eq "override"
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
it "uses to_create globally across FactoryBot.define" do
|
|
|
|
define_model("Company", name: :string)
|
2012-05-12 00:42:44 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-05-12 00:42:44 -04:00
|
|
|
factory :company
|
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
company = FactoryBot.create(:company)
|
|
|
|
override_company = FactoryBot.create(:company, :override_to_create)
|
|
|
|
|
|
|
|
expect(company.name).to eq "persisted!"
|
|
|
|
expect(override_company.name).to eq "override"
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-04 10:56:37 -04:00
|
|
|
describe "global skip_create" do
|
2012-05-12 00:42:44 -04:00
|
|
|
before do
|
2018-08-04 10:56:37 -04:00
|
|
|
define_model("User", name: :string)
|
|
|
|
define_model("Post", name: :string)
|
2012-05-12 00:42:44 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-05-12 00:42:44 -04:00
|
|
|
skip_create
|
|
|
|
|
|
|
|
trait :override_to_create do
|
2018-08-04 10:56:37 -04:00
|
|
|
to_create { |instance| instance.name = "override" }
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
factory :user do
|
2018-08-04 10:56:37 -04:00
|
|
|
name { "John Doe" }
|
2012-05-12 00:42:44 -04:00
|
|
|
|
|
|
|
factory :child_user
|
|
|
|
|
|
|
|
factory :child_user_with_trait do
|
|
|
|
override_to_create
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :post do
|
2018-08-04 10:56:37 -04:00
|
|
|
name { "Great title" }
|
2012-05-12 00:42:44 -04:00
|
|
|
|
|
|
|
factory :child_post
|
|
|
|
|
|
|
|
factory :child_post_with_trait do
|
|
|
|
override_to_create
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "does not persist any record" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.create(:user)).to be_new_record
|
|
|
|
expect(FactoryBot.create(:post)).to be_new_record
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "does not persist child records" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.create(:child_user)).to be_new_record
|
|
|
|
expect(FactoryBot.create(:child_post)).to be_new_record
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "honors overridden to_create" do
|
|
|
|
expect(FactoryBot.create(:child_user_with_trait).name).to eq "override"
|
|
|
|
expect(FactoryBot.create(:child_post_with_trait).name).to eq "override"
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
it "honors inline trait to_create" do
|
|
|
|
expect(FactoryBot.create(:child_user, :override_to_create).name).to eq "override"
|
|
|
|
expect(FactoryBot.create(:child_post, :override_to_create).name).to eq "override"
|
2012-05-12 00:42:44 -04:00
|
|
|
end
|
|
|
|
end
|