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-12 10:35:41 -04:00
|
|
|
define_model("User",
|
2011-08-12 15:05:38 -04:00
|
|
|
:name => :string,
|
|
|
|
:admin => :boolean,
|
|
|
|
:gender => :string,
|
|
|
|
:email => :string,
|
|
|
|
:date_of_birth => :date,
|
|
|
|
:great => :string)
|
2011-08-12 10:35:41 -04:00
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user_without_admin_scoping, :class => User do
|
|
|
|
admin_attribute_group
|
|
|
|
end
|
2011-08-09 18:00:24 -04:00
|
|
|
|
|
|
|
factory :user do
|
|
|
|
name "John"
|
|
|
|
|
2011-08-12 15:05:38 -04:00
|
|
|
attribute_group :great do
|
|
|
|
great "GREAT!!!"
|
|
|
|
end
|
|
|
|
|
2011-08-11 12:31:35 -04:00
|
|
|
attribute_group :admin do
|
2011-08-09 18:00:24 -04:00
|
|
|
admin true
|
|
|
|
end
|
|
|
|
|
2011-08-12 10:35:41 -04:00
|
|
|
attribute_group :admin_attribute_group do
|
|
|
|
admin true
|
|
|
|
end
|
|
|
|
|
2011-08-10 22:23:36 -04:00
|
|
|
attribute_group :male do
|
2011-08-09 20:29:02 -04:00
|
|
|
name "Joe"
|
2011-08-09 18:00:24 -04:00
|
|
|
gender "Male"
|
|
|
|
end
|
|
|
|
|
2011-08-10 22:23:36 -04:00
|
|
|
attribute_group :female do
|
2011-08-09 18:00:24 -04:00
|
|
|
name "Jane"
|
|
|
|
gender "Female"
|
|
|
|
end
|
|
|
|
|
2011-08-12 15:05:38 -04:00
|
|
|
factory :great_user do
|
|
|
|
great
|
|
|
|
end
|
|
|
|
|
2011-08-12 10:35:41 -04:00
|
|
|
factory :admin, :attribute_groups => [:admin]
|
|
|
|
|
2011-08-10 23:33:50 -04:00
|
|
|
factory :male_user do
|
|
|
|
male
|
2011-08-12 15:05:38 -04:00
|
|
|
|
|
|
|
factory :child_male_user do
|
|
|
|
date_of_birth { Date.parse("1/1/2000") }
|
|
|
|
end
|
2011-08-10 23:33:50 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2011-08-10 22:23:36 -04:00
|
|
|
factory :female, :attribute_groups => [:female] do
|
|
|
|
attribute_group :admin do
|
2011-08-10 14:11:53 -04:00
|
|
|
admin true
|
|
|
|
name "Judy"
|
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
|
|
|
factory :female_admin_judy, :attribute_groups => [:admin]
|
2011-08-10 14:11:53 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
|
|
|
factory :female_admin, :attribute_groups => [:female, :admin]
|
2011-08-10 22:23:36 -04:00
|
|
|
factory :female_after_male_admin, :attribute_groups => [:male, :female, :admin]
|
|
|
|
factory :male_after_female_admin, :attribute_groups => [:female, :male, :admin]
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2011-08-10 22:23:36 -04:00
|
|
|
attribute_group :email do
|
2011-08-10 14:11:53 -04:00
|
|
|
email { "#{name}@example.com" }
|
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
|
|
|
factory :user_with_email, :class => User, :attribute_groups => [:email] do
|
2011-08-10 14:11:53 -04:00
|
|
|
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 }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should_not be_admin }
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -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 }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should be_admin }
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -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" }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should_not be_admin }
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -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" }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should be_admin }
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -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 }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should be_admin }
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -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" }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should be_admin }
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -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) }
|
|
|
|
its(:name) { should == "Jane" }
|
|
|
|
its(:gender) { should == "Female" }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should be_admin }
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2011-08-10 14:11:53 -04:00
|
|
|
context "child class with scoped attribute group and inherited attribute group" do
|
2011-08-12 10:35:41 -04:00
|
|
|
subject { FactoryGirl.create(:female_admin_judy) }
|
|
|
|
its(:name) { should == "Judy" }
|
2011-08-10 14:11:53 -04:00
|
|
|
its(:gender) { should == "Female" }
|
2011-08-12 10:35:41 -04:00
|
|
|
it { should be_admin }
|
2011-08-10 14:11:53 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2011-08-10 14:11:53 -04:00
|
|
|
context "factory using global attribute group" do
|
2011-08-12 10:35:41 -04:00
|
|
|
subject { FactoryGirl.create(:user_with_email) }
|
|
|
|
its(:name) { should == "Bill" }
|
2011-08-10 14:11:53 -04:00
|
|
|
its(:email) { should == "Bill@example.com"}
|
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2011-08-10 23:33:50 -04:00
|
|
|
context "factory created with alternate syntax for specifying attribute group" do
|
2011-08-12 10:35:41 -04:00
|
|
|
subject { FactoryGirl.create(:male_user) }
|
2011-08-10 23:33:50 -04:00
|
|
|
its(:gender) { should == "Male" }
|
|
|
|
end
|
|
|
|
|
2011-08-12 15:05:38 -04:00
|
|
|
context "factory created with alternate syntax where attribute group and attribute are the same" do
|
|
|
|
subject { FactoryGirl.create(:great_user) }
|
|
|
|
its(:great) { should == "GREAT!!!" }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "factory created with alternate syntax where attribute group and attribute are the same and attribute is overridden" do
|
|
|
|
subject { FactoryGirl.create(:great_user, :great => "SORT OF!!!") }
|
|
|
|
its(:great) { should == "SORT OF!!!" }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "child factory created where attribute group attributes are inherited" do
|
|
|
|
subject { FactoryGirl.create(:child_male_user) }
|
|
|
|
its(:gender) { should == "Male" }
|
|
|
|
its(:date_of_birth) { should == Date.parse("1/1/2000") }
|
|
|
|
end
|
|
|
|
|
2011-08-12 10:35:41 -04:00
|
|
|
context "factory outside of scope" do
|
|
|
|
subject { FactoryGirl.create(:user_without_admin_scoping) }
|
|
|
|
it { expect { subject }.to raise_error(ArgumentError, "Not registered: admin_attribute_group") }
|
|
|
|
end
|
2011-08-09 18:00:24 -04:00
|
|
|
end
|