2010-11-12 15:58:25 -05:00
|
|
|
describe "an instance generated by a factory that inherits from another factory" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string, admin: :boolean, email: :string, upper_email: :string, login: :string)
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2010-11-12 15:58:25 -05:00
|
|
|
factory :user do
|
2020-06-05 15:15:18 -04:00
|
|
|
name { "John" }
|
2011-07-19 10:49:00 -04:00
|
|
|
email { "#{name.downcase}@example.com" }
|
2011-08-08 23:46:26 -04:00
|
|
|
login { email }
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2011-06-29 16:49:45 -04:00
|
|
|
factory :admin do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "admin" }
|
|
|
|
admin { true }
|
2011-08-08 18:59:48 -04:00
|
|
|
upper_email { email.upcase }
|
2011-06-29 16:49:45 -04:00
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-29 16:49:45 -04:00
|
|
|
describe "the parent class" do
|
2020-06-05 15:15:18 -04:00
|
|
|
subject { FactoryBot.create(:user) }
|
|
|
|
it { should_not be_admin }
|
|
|
|
its(:name) { should eq "John" }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:email) { should eq "john@example.com" }
|
2018-09-28 16:57:46 -04:00
|
|
|
its(:login) { should eq "john@example.com" }
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
2018-09-28 16:57:46 -04:00
|
|
|
describe "the child class redefining parent's attributes" do
|
2020-06-05 15:15:18 -04:00
|
|
|
subject { FactoryBot.create(:admin) }
|
|
|
|
it { should be_kind_of(User) }
|
|
|
|
it { should be_admin }
|
|
|
|
its(:name) { should eq "admin" }
|
|
|
|
its(:email) { should eq "admin@example.com" }
|
|
|
|
its(:login) { should eq "admin@example.com" }
|
2018-09-27 21:35:05 -04:00
|
|
|
its(:upper_email) { should eq "ADMIN@EXAMPLE.COM" }
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-23 00:51:51 -05:00
|
|
|
describe "nested factories with different parents" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string)
|
2012-01-23 00:51:51 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-01-23 00:51:51 -05:00
|
|
|
factory :user do
|
2020-06-05 15:15:18 -04:00
|
|
|
name { "Basic User" }
|
2012-01-23 00:51:51 -05:00
|
|
|
|
|
|
|
factory :male_user do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "John Doe" }
|
2012-01-23 00:51:51 -05:00
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
factory :uppercase_male_user, parent: :male_user do
|
2013-12-14 22:33:15 -05:00
|
|
|
after(:build) { |user| user.name = user.name.upcase }
|
2012-01-23 00:51:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "honors :parent over the factory block nesting" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:uppercase_male_user).name).to eq "JOHN DOE"
|
2012-01-23 00:51:51 -05:00
|
|
|
end
|
|
|
|
end
|