2010-11-12 14:58:25 -06:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
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 14:58:25 -06:00
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user do
|
2011-06-29 16:49:45 -04:00
|
|
|
name "John"
|
2011-07-19 16:49:00 +02:00
|
|
|
email { "#{name.downcase}@example.com" }
|
2011-08-08 23:46:26 -04:00
|
|
|
login { email }
|
2010-11-12 14:58:25 -06:00
|
|
|
|
2011-06-29 16:49:45 -04:00
|
|
|
factory :admin do
|
2011-07-19 16:49:00 +02:00
|
|
|
name "admin"
|
2011-06-29 16:49:45 -04:00
|
|
|
admin true
|
2011-08-08 15:59:48 -07:00
|
|
|
upper_email { email.upcase }
|
2011-06-29 16:49:45 -04:00
|
|
|
end
|
2011-07-13 06:51:51 -07:00
|
|
|
|
|
|
|
factory :guest do
|
|
|
|
email { "#{name}-guest@example.com" }
|
|
|
|
end
|
2011-08-21 14:07:48 -07:00
|
|
|
|
|
|
|
factory :no_email do
|
|
|
|
email ""
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :bill do
|
|
|
|
name { "Bill" } #block to make attribute dynamic
|
|
|
|
end
|
2010-11-12 14:58:25 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-29 16:49:45 -04:00
|
|
|
describe "the parent class" do
|
|
|
|
subject { FactoryGirl.create(:user) }
|
|
|
|
it { should_not be_admin }
|
|
|
|
its(:email) { should == "john@example.com" }
|
2010-11-12 14:58:25 -06:00
|
|
|
end
|
|
|
|
|
2011-07-19 16:49:00 +02:00
|
|
|
describe "the child class redefining parent's static method used by a dynamic method" do
|
2011-08-08 15:59:48 -07:00
|
|
|
subject { FactoryGirl.create(:admin) }
|
|
|
|
it { should be_kind_of(User) }
|
|
|
|
it { should be_admin }
|
|
|
|
its(:name) { should == "admin" }
|
|
|
|
its(:email) { should == "admin@example.com" }
|
|
|
|
its(:upper_email) { should == "ADMIN@EXAMPLE.COM"}
|
2010-11-12 14:58:25 -06:00
|
|
|
end
|
2011-07-13 06:51:51 -07:00
|
|
|
|
2011-07-19 16:49:00 +02:00
|
|
|
describe "the child class redefining parent's dynamic method" do
|
2011-07-13 06:51:51 -07:00
|
|
|
subject { FactoryGirl.create(:guest) }
|
2011-07-19 16:49:00 +02:00
|
|
|
it { should_not be_admin }
|
|
|
|
its(:name) { should == "John" }
|
2011-07-13 06:51:51 -07:00
|
|
|
its(:email) { should eql "John-guest@example.com" }
|
2011-08-08 23:46:26 -04:00
|
|
|
its(:login) { should == "John-guest@example.com" }
|
2011-07-13 06:51:51 -07:00
|
|
|
end
|
2011-08-21 14:07:48 -07:00
|
|
|
|
|
|
|
describe "the child class redefining parent's dynamic attribute with static attribute" do
|
|
|
|
subject { FactoryGirl.create(:no_email) }
|
|
|
|
its(:email) { should == "" }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "the child class redefining parent's static attribute with dynamic attribute" do
|
|
|
|
subject { FactoryGirl.create(:bill) }
|
|
|
|
its(:name) { should == "Bill" }
|
|
|
|
end
|
2010-11-12 14:58:25 -06: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
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
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
|
2012-05-04 16:48:46 -04: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
|
|
|
|
FactoryGirl.build(:uppercase_male_user).name.should == "JOHN DOE"
|
|
|
|
end
|
|
|
|
end
|