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/create_list_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

82 lines
1.9 KiB
Ruby

require 'spec_helper'
describe "create multiple instances" do
before do
define_model('Post', title: :string)
FactoryGirl.define do
factory(:post) do |post|
post.title "Through the Looking Glass"
end
end
end
context "without default attributes" do
subject { FactoryGirl.create_list(:post, 20) }
its(:length) { should == 20 }
it "creates all the posts" do
subject.each do |record|
record.should_not be_new_record
end
end
it "uses the default factory values" do
subject.each do |record|
record.title.should == "Through the Looking Glass"
end
end
end
context "with default attributes" do
subject { FactoryGirl.create_list(:post, 20, title: "The Hunting of the Snark") }
it "overrides the default values" do
subject.each do |record|
record.title.should == "The Hunting of the Snark"
end
end
end
end
describe "multiple creates and ignored attributes to dynamically build attribute lists" do
before do
define_model('User', name: :string) do
has_many :posts
end
define_model('Post', title: :string, user_id: :integer) do
belongs_to :user
end
FactoryGirl.define do
factory :post do
title "Through the Looking Glass"
user
end
factory :user do
name "John Doe"
factory :user_with_posts do
ignore do
posts_count 5
end
after(:create) do |user, evaluator|
FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
end
end
end
end
end
it "generates the correct number of posts" do
FactoryGirl.create(:user_with_posts).posts.length.should == 5
end
it "allows the number of posts to be modified" do
FactoryGirl.create(:user_with_posts, posts_count: 2).posts.length.should == 2
end
end