2010-11-12 15:58:25 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "an instance generated by a factory that inherits from another factory" do
|
|
|
|
before do
|
2011-08-08 23:46:26 -04:00
|
|
|
define_model("User", :name => :string, :admin => :boolean, :email => :string, :upper_email => :string, :login => :string)
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
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
|
|
|
|
subject { FactoryGirl.create(:user) }
|
|
|
|
it { should_not be_admin }
|
|
|
|
its(:email) { should == "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
|
2011-08-08 18:59:48 -04: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 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
|
2011-07-13 09:51:51 -04:00
|
|
|
subject { FactoryGirl.create(:guest) }
|
2011-07-19 10:49:00 -04:00
|
|
|
it { should_not be_admin }
|
|
|
|
its(:name) { should == "John" }
|
2011-07-13 09:51:51 -04: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 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
|
|
|
|
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 15:58:25 -05:00
|
|
|
end
|
|
|
|
|