mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
202 lines
5.7 KiB
Ruby
202 lines
5.7 KiB
Ruby
require "spec_helper"
|
|
|
|
describe "an instance generated by a factory with multiple traits" do
|
|
before do
|
|
define_model("User",
|
|
:name => :string,
|
|
:admin => :boolean,
|
|
:gender => :string,
|
|
:email => :string,
|
|
:date_of_birth => :date,
|
|
:great => :string)
|
|
|
|
FactoryGirl.define do
|
|
factory :user_without_admin_scoping, :class => User do
|
|
admin_trait
|
|
end
|
|
|
|
factory :user do
|
|
name "John"
|
|
|
|
trait :great do
|
|
great "GREAT!!!"
|
|
end
|
|
|
|
trait :admin do
|
|
admin true
|
|
end
|
|
|
|
trait :admin_trait do
|
|
admin true
|
|
end
|
|
|
|
trait :male do
|
|
name "Joe"
|
|
gender "Male"
|
|
end
|
|
|
|
trait :female do
|
|
name "Jane"
|
|
gender "Female"
|
|
end
|
|
|
|
factory :great_user do
|
|
great
|
|
end
|
|
|
|
factory :admin, :traits => [:admin]
|
|
|
|
factory :male_user do
|
|
male
|
|
|
|
factory :child_male_user do
|
|
date_of_birth { Date.parse("1/1/2000") }
|
|
end
|
|
end
|
|
|
|
factory :female, :traits => [:female] do
|
|
trait :admin do
|
|
admin true
|
|
name "Judy"
|
|
end
|
|
|
|
factory :female_admin_judy, :traits => [:admin]
|
|
end
|
|
|
|
factory :female_admin, :traits => [:female, :admin]
|
|
factory :female_after_male_admin, :traits => [:male, :female, :admin]
|
|
factory :male_after_female_admin, :traits => [:female, :male, :admin]
|
|
end
|
|
|
|
trait :email do
|
|
email { "#{name}@example.com" }
|
|
end
|
|
|
|
factory :user_with_email, :class => User, :traits => [:email] do
|
|
name "Bill"
|
|
end
|
|
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
|
|
|
|
context "the child class with one trait" do
|
|
subject { FactoryGirl.create(:admin) }
|
|
its(:name) { should == "John" }
|
|
its(:gender) { should be_nil }
|
|
it { should be_admin }
|
|
end
|
|
|
|
context "the other child class with one trait" do
|
|
subject { FactoryGirl.create(:female) }
|
|
its(:name) { should == "Jane" }
|
|
its(:gender) { should == "Female" }
|
|
it { should_not be_admin }
|
|
end
|
|
|
|
context "the child with multiple traits" do
|
|
subject { FactoryGirl.create(:female_admin) }
|
|
its(:name) { should == "Jane" }
|
|
its(:gender) { should == "Female" }
|
|
it { should be_admin }
|
|
end
|
|
|
|
context "the child with multiple traits 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
|
|
|
|
context "the child with multiple traits who override the same attribute" do
|
|
context "when the male assigns name after female" do
|
|
subject { FactoryGirl.create(:male_after_female_admin) }
|
|
its(:name) { should == "Joe" }
|
|
its(:gender) { should == "Male" }
|
|
it { should be_admin }
|
|
end
|
|
|
|
context "when the female assigns name after male" do
|
|
subject { FactoryGirl.create(:female_after_male_admin) }
|
|
its(:name) { should == "Jane" }
|
|
its(:gender) { should == "Female" }
|
|
it { should be_admin }
|
|
end
|
|
end
|
|
|
|
context "child class with scoped trait and inherited trait" do
|
|
subject { FactoryGirl.create(:female_admin_judy) }
|
|
its(:name) { should == "Judy" }
|
|
its(:gender) { should == "Female" }
|
|
it { should be_admin }
|
|
end
|
|
|
|
context "factory using global trait" do
|
|
subject { FactoryGirl.create(:user_with_email) }
|
|
its(:name) { should == "Bill" }
|
|
its(:email) { should == "Bill@example.com"}
|
|
end
|
|
|
|
context "factory created with alternate syntax for specifying trait" do
|
|
subject { FactoryGirl.create(:male_user) }
|
|
its(:gender) { should == "Male" }
|
|
end
|
|
|
|
context "factory created with alternate syntax where trait name and attribute are the same" do
|
|
subject { FactoryGirl.create(:great_user) }
|
|
its(:great) { should == "GREAT!!!" }
|
|
end
|
|
|
|
context "factory created with alternate syntax where trait name 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 trait 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
|
|
|
|
context "factory outside of scope" do
|
|
subject { FactoryGirl.create(:user_without_admin_scoping) }
|
|
it { expect { subject }.to raise_error(ArgumentError, "Trait not registered: admin_trait") }
|
|
end
|
|
end
|
|
|
|
describe "traits with callbacks" do
|
|
before do
|
|
define_model("User", :name => :string)
|
|
|
|
FactoryGirl.define do
|
|
factory :user do
|
|
name "John"
|
|
|
|
trait :great do
|
|
after_create {|user| user.name.upcase! }
|
|
end
|
|
|
|
factory :caps_user, :traits => [:great]
|
|
|
|
factory :caps_user_implicit_trait do
|
|
great
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
context "when the factory has a trait passed via arguments" do
|
|
subject { FactoryGirl.create(:caps_user) }
|
|
its(:name) { should == "JOHN" }
|
|
end
|
|
|
|
context "when the factory has an implicit trait" do
|
|
subject { FactoryGirl.create(:caps_user_implicit_trait) }
|
|
its(:name) { should == "JOHN" }
|
|
end
|
|
end
|