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
|
2011-06-29 16:49:45 -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
|
2011-07-19 10:49:00 -04:00
|
|
|
name "admin"
|
2011-06-29 16:49:45 -04:00
|
|
|
admin true
|
2011-08-08 18:59:48 -04:00
|
|
|
upper_email { email.upcase }
|
2011-06-29 16:49:45 -04:00
|
|
|
end
|
2011-07-13 09:51:51 -04:00
|
|
|
|
|
|
|
factory :guest do
|
|
|
|
email { "#{name}-guest@example.com" }
|
|
|
|
end
|
2011-08-21 17:07:48 -04:00
|
|
|
|
|
|
|
factory :no_email do
|
|
|
|
email ""
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :bill do
|
|
|
|
name { "Bill" } #block to make attribute dynamic
|
|
|
|
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
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:user) }
|
2011-06-29 16:49:45 -04:00
|
|
|
it { should_not be_admin }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:email) { should eq "john@example.com" }
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
2011-07-19 10:49:00 -04:00
|
|
|
describe "the child class redefining parent's static method used by a dynamic method" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:admin) }
|
2011-08-08 18:59:48 -04:00
|
|
|
it { should be_kind_of(User) }
|
|
|
|
it { should be_admin }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "admin" }
|
|
|
|
its(:email) { should eq "admin@example.com" }
|
|
|
|
its(:upper_email) { should eq "ADMIN@EXAMPLE.COM"}
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
2011-07-13 09:51:51 -04:00
|
|
|
|
2011-07-19 10:49:00 -04:00
|
|
|
describe "the child class redefining parent's dynamic method" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:guest) }
|
2011-07-19 10:49:00 -04:00
|
|
|
it { should_not be_admin }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "John" }
|
2011-07-13 09:51:51 -04:00
|
|
|
its(:email) { should eql "John-guest@example.com" }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:login) { should eq "John-guest@example.com" }
|
2011-07-13 09:51:51 -04:00
|
|
|
end
|
2011-08-21 17:07:48 -04:00
|
|
|
|
|
|
|
describe "the child class redefining parent's dynamic attribute with static attribute" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:no_email) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:email) { should eq "" }
|
2011-08-21 17:07:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "the child class redefining parent's static attribute with dynamic attribute" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:bill) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq "Bill" }
|
2011-08-21 17:07:48 -04:00
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
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
|
|
|
|
name "Basic User"
|
|
|
|
|
|
|
|
factory :male_user do
|
|
|
|
name "John Doe"
|
|
|
|
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
|