1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/acceptance/parent_spec.rb
Flavio Castelli 28f541eefc Ensure static attributes are executed before dynamic ones.
Static attributes must be executed first because dynamic attributes might
rely on them. This is really important when using the :parent relationship.

Previous code didn't work fine in situations like this one:
  Factory.define(:generic_user, :class => User) do |u|
    u.email { |user| "#{user.name}@example.com }
  end

  Factory.define(:flavio, :parent => :generic_user) do |u|
    u.name "flavio"
  end

When building a :user object the previous code would have set the email
attribute and then the name attribute. This results in a user object with
an invalid email address: the 'name' attribute is yet not set while the
'email' attribute is evaluated.

Closes #159
2011-08-05 11:11:34 -04:00

46 lines
1.3 KiB
Ruby

require 'spec_helper'
require 'acceptance/acceptance_helper'
describe "an instance generated by a factory that inherits from another factory" do
before do
define_model("User", :name => :string, :admin => :boolean, :email => :string)
FactoryGirl.define do
factory :user do
name "John"
email { "#{name.downcase}@example.com" }
factory :admin do
name "admin"
admin true
end
factory :guest do
email { "#{name}-guest@example.com" }
end
end
end
end
describe "the parent class" do
subject { FactoryGirl.create(:user) }
it { should_not be_admin }
its(:email) { should == "john@example.com" }
end
describe "the child class redefining parent's static method used by a dynamic method" do
subject { FactoryGirl.create(:admin) }
it { should be_kind_of(User) }
it { should be_admin }
its(:name) { should == "admin" }
its(:email) { should == "admin@example.com" }
end
describe "the child class redefining parent's dynamic method" do
subject { FactoryGirl.create(:guest) }
it { should_not be_admin }
its(:name) { should == "John" }
its(:email) { should eql "John-guest@example.com" }
end
end