2011-08-09 18:00:24 -04:00
|
|
|
require "spec_helper"
|
|
|
|
require "acceptance/acceptance_helper"
|
|
|
|
|
|
|
|
describe "an instance generated by a factory with multiple attribute groups" do
|
|
|
|
before do
|
2011-08-10 14:11:53 -04:00
|
|
|
define_model("User", :name => :string, :admin => :boolean, :gender => :string, :email => :string)
|
2011-08-09 18:00:24 -04:00
|
|
|
|
2011-08-10 14:11:53 -04:00
|
|
|
FactoryGirl.define do
|
2011-08-09 18:00:24 -04:00
|
|
|
factory :user do
|
|
|
|
name "John"
|
|
|
|
|
2011-08-10 14:17:30 -04:00
|
|
|
attr_group :admin, :factory=>true do
|
2011-08-09 18:00:24 -04:00
|
|
|
admin true
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_group :male do
|
2011-08-09 20:29:02 -04:00
|
|
|
name "Joe"
|
2011-08-09 18:00:24 -04:00
|
|
|
gender "Male"
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_group :female do
|
|
|
|
name "Jane"
|
|
|
|
gender "Female"
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :male, :attr_groups => [:male]
|
2011-08-10 14:11:53 -04:00
|
|
|
factory :female, :attr_groups => [:female] do
|
|
|
|
attr_group :admin do
|
|
|
|
admin true
|
|
|
|
name "Judy"
|
|
|
|
end
|
|
|
|
factory :female_admin_judy, :attr_groups=>[:admin]
|
|
|
|
end
|
2011-08-09 18:00:24 -04:00
|
|
|
factory :female_admin, :attr_groups => [:female, :admin]
|
|
|
|
factory :female_after_male_admin, :attr_groups => [:male, :female, :admin]
|
|
|
|
factory :male_after_female_admin, :attr_groups => [:female, :male, :admin]
|
|
|
|
end
|
2011-08-10 14:11:53 -04:00
|
|
|
|
|
|
|
attr_group :email do
|
|
|
|
email { "#{name}@example.com" }
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :user_with_email, :class=>User, :attr_groups=>[:email] do
|
|
|
|
name "Bill"
|
|
|
|
end
|
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "the parent class" do
|
|
|
|
subject { FactoryGirl.create(:user) }
|
|
|
|
its(:name) { should == "John" }
|
|
|
|
its(:gender) { should be_nil }
|
|
|
|
it { should_not be_admin }
|
|
|
|
end
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
context "the child class with one attribute group" do
|
|
|
|
subject { FactoryGirl.create(:admin) }
|
|
|
|
its(:name) { should == "John" }
|
|
|
|
its(:gender) { should be_nil }
|
|
|
|
it { should be_admin }
|
|
|
|
end
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
context "the other child class with one attribute group" do
|
|
|
|
subject { FactoryGirl.create(:female) }
|
|
|
|
its(:name) { should == "Jane" }
|
|
|
|
its(:gender) { should == "Female" }
|
|
|
|
it { should_not be_admin }
|
|
|
|
end
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
context "the child with multiple attribute groups" do
|
|
|
|
subject { FactoryGirl.create(:female_admin) }
|
|
|
|
its(:name) { should == "Jane" }
|
|
|
|
its(:gender) { should == "Female" }
|
|
|
|
it { should be_admin }
|
|
|
|
end
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
context "the child with multiple attribute groups and overridden attributes" do
|
|
|
|
subject { FactoryGirl.create(:female_admin, :name => "Jill", :gender => nil) }
|
|
|
|
its(:name) { should == "Jill" }
|
|
|
|
its(:gender) { should be_nil }
|
|
|
|
it { should be_admin }
|
|
|
|
end
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
context "the child with multiple attribute groups who override the same attribute" do
|
|
|
|
context "when the male assigns name after female" do
|
|
|
|
subject { FactoryGirl.create(:male_after_female_admin) }
|
2011-08-09 20:29:02 -04:00
|
|
|
|
|
|
|
its(:name) { should == "Joe" }
|
2011-08-09 18:00:24 -04:00
|
|
|
its(:gender) { should == "Male" }
|
|
|
|
it { should be_admin }
|
|
|
|
end
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
context "when the female assigns name after male" do
|
|
|
|
subject { FactoryGirl.create(:female_after_male_admin) }
|
2011-08-09 20:29:02 -04:00
|
|
|
|
2011-08-09 18:00:24 -04:00
|
|
|
its(:name) { should == "Jane" }
|
|
|
|
its(:gender) { should == "Female" }
|
|
|
|
it { should be_admin }
|
|
|
|
end
|
|
|
|
end
|
2011-08-10 14:11:53 -04:00
|
|
|
|
|
|
|
context "child class with scoped attribute group and inherited attribute group" do
|
|
|
|
subject { FactoryGirl.create(:female_admin_judy) }
|
|
|
|
its(:name) { should == "Judy" }
|
|
|
|
its(:gender) { should == "Female" }
|
|
|
|
it { should be_admin }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "factory using global attribute group" do
|
|
|
|
subject { FactoryGirl.create(:user_with_email) }
|
|
|
|
its(:name) { should == "Bill" }
|
|
|
|
its(:email) { should == "Bill@example.com"}
|
|
|
|
end
|
2011-08-10 14:17:30 -04:00
|
|
|
|
|
|
|
context "factory created from attribute group" do
|
|
|
|
subject { FactoryGirl.create(:admin) }
|
|
|
|
it { should be_admin }
|
|
|
|
end
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|