2010-11-12 15:58:25 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "callbacks" do
|
|
|
|
before do
|
|
|
|
define_model("User", :first_name => :string, :last_name => :string)
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :user_with_callbacks, :class => :user do
|
|
|
|
after_stub { |user| user.first_name = 'Stubby' }
|
|
|
|
after_build { |user| user.first_name = 'Buildy' }
|
|
|
|
after_create { |user| user.last_name = 'Createy' }
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :user_with_inherited_callbacks, :parent => :user_with_callbacks do
|
2011-09-20 13:35:17 -04:00
|
|
|
after_stub { |user| user.last_name = 'Double-Stubby' }
|
|
|
|
after_build { |user| user.first_name = 'Child-Buildy' }
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs the after_stub callback when stubbing" do
|
2011-01-19 19:47:49 -05:00
|
|
|
user = FactoryGirl.build_stubbed(:user_with_callbacks)
|
2010-11-12 15:58:25 -05:00
|
|
|
user.first_name.should == 'Stubby'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs the after_build callback when building" do
|
2011-01-19 19:47:49 -05:00
|
|
|
user = FactoryGirl.build(:user_with_callbacks)
|
2010-11-12 15:58:25 -05:00
|
|
|
user.first_name.should == 'Buildy'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs both the after_build and after_create callbacks when creating" do
|
2011-01-19 19:47:49 -05:00
|
|
|
user = FactoryGirl.create(:user_with_callbacks)
|
2010-11-12 15:58:25 -05:00
|
|
|
user.first_name.should == 'Buildy'
|
|
|
|
user.last_name.should == 'Createy'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs both the after_stub callback on the factory and the inherited after_stub callback" do
|
2011-01-19 19:47:49 -05:00
|
|
|
user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
|
2010-11-12 15:58:25 -05:00
|
|
|
user.first_name.should == 'Stubby'
|
|
|
|
user.last_name.should == 'Double-Stubby'
|
|
|
|
end
|
2011-09-20 13:35:17 -04:00
|
|
|
|
|
|
|
it "runs child callback after parent callback" do
|
|
|
|
user = FactoryGirl.build(:user_with_inherited_callbacks)
|
|
|
|
user.first_name.should == 'Child-Buildy'
|
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|