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
Josh Clayton and Jason Draper bef5a01b31 Introduce new callback syntax
Instead of calling before_create, after_build, after_create, or
after_stub, you can now call:

    before(:create) {|instance| instance.name = "overridden!" }
    after(:create)  {|instance| instance.name = "overridden!" }
    after(:build)   {|instance| instance.name = "overridden!" }
    after(:stub)    {|instance| instance.name = "overridden!" }

Additionally, you can declare callbacks longhand:

    callback(:after_stub) {|instance| instance.name = "overridden!" }

This allows for custom callbacks to be defined:

    callback(:custom_callback) {|instance| instance.name = "overridden!" }

Which can then be used from a custom strategy:

    class CustomStrategy
      def association(runner); end

      def result(evaluation)
        evaluation.object.tap do |instance|
          evaluation.notify(:custom_callback, instance)
        end
      end
    end

    FactoryGirl.register_strategy(:custom, CustomStrategy)

This would allow for calling:

    FactoryGirl.custom(:user)

Which would return the user instance but execute the :custom_callback callback
on the user instance first.
2012-05-04 17:59:34 -04:00

90 lines
2.5 KiB
Ruby

require 'spec_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, upper_email: :string, login: :string)
FactoryGirl.define do
factory :user do
name "John"
email { "#{name.downcase}@example.com" }
login { email }
factory :admin do
name "admin"
admin true
upper_email { email.upcase }
end
factory :guest do
email { "#{name}-guest@example.com" }
end
factory :no_email do
email ""
end
factory :bill do
name { "Bill" } #block to make attribute dynamic
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" }
its(:upper_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" }
its(:login) { should == "John-guest@example.com" }
end
describe "the child class redefining parent's dynamic attribute with static attribute" do
subject { FactoryGirl.create(:no_email) }
its(:email) { should == "" }
end
describe "the child class redefining parent's static attribute with dynamic attribute" do
subject { FactoryGirl.create(:bill) }
its(:name) { should == "Bill" }
end
end
describe "nested factories with different parents" do
before do
define_model("User", name: :string)
FactoryGirl.define do
factory :user do
name "Basic User"
factory :male_user do
name "John Doe"
end
factory :uppercase_male_user, parent: :male_user do
after(:build) {|user| user.name = user.name.upcase }
end
end
end
end
it "honors :parent over the factory block nesting" do
FactoryGirl.build(:uppercase_male_user).name.should == "JOHN DOE"
end
end