factory_girl 4.5.0 → 4.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +25 -5
- data/Appraisals +11 -7
- data/CONTRIBUTING.md +44 -6
- data/GETTING_STARTED.md +175 -59
- data/Gemfile +5 -0
- data/Gemfile.lock +86 -63
- data/LICENSE +1 -1
- data/NEWS +12 -1
- data/README.md +21 -10
- data/Rakefile +1 -3
- data/factory_girl.gemspec +6 -14
- data/gemfiles/3.2.gemfile +4 -1
- data/gemfiles/3.2.gemfile.lock +84 -52
- data/gemfiles/4.0.gemfile +4 -1
- data/gemfiles/4.0.gemfile.lock +75 -49
- data/gemfiles/4.1.gemfile +4 -1
- data/gemfiles/4.1.gemfile.lock +78 -48
- data/gemfiles/4.2.gemfile +10 -0
- data/gemfiles/4.2.gemfile.lock +123 -0
- data/gemfiles/5.0.gemfile +10 -0
- data/gemfiles/5.0.gemfile.lock +126 -0
- data/lib/factory_girl.rb +13 -4
- data/lib/factory_girl/attribute/dynamic.rb +4 -1
- data/lib/factory_girl/attribute_assigner.rb +2 -2
- data/lib/factory_girl/configuration.rb +4 -0
- data/lib/factory_girl/decorator.rb +2 -2
- data/lib/factory_girl/decorator/class_key_hash.rb +2 -1
- data/lib/factory_girl/definition_proxy.rb +2 -2
- data/lib/factory_girl/evaluator.rb +4 -0
- data/lib/factory_girl/factory_runner.rb +7 -1
- data/lib/factory_girl/find_definitions.rb +1 -1
- data/lib/factory_girl/linter.rb +65 -14
- data/lib/factory_girl/strategy/stub.rb +25 -1
- data/lib/factory_girl/strategy_syntax_method_registrar.rb +4 -0
- data/lib/factory_girl/version.rb +1 -1
- metadata +26 -185
- data/features/find_definitions.feature +0 -75
- data/features/step_definitions/database_steps.rb +0 -9
- data/features/step_definitions/factory_girl_steps.rb +0 -36
- data/features/support/env.rb +0 -10
- data/features/support/factories.rb +0 -16
- data/gemfiles/3.1.gemfile +0 -7
- data/gemfiles/3.1.gemfile.lock +0 -91
- data/spec/acceptance/activesupport_instrumentation_spec.rb +0 -62
- data/spec/acceptance/aliases_spec.rb +0 -19
- data/spec/acceptance/attribute_aliases_spec.rb +0 -45
- data/spec/acceptance/attribute_existing_on_object_spec.rb +0 -68
- data/spec/acceptance/attributes_for_spec.rb +0 -97
- data/spec/acceptance/attributes_from_instance_spec.rb +0 -53
- data/spec/acceptance/attributes_ordered_spec.rb +0 -51
- data/spec/acceptance/build_list_spec.rb +0 -56
- data/spec/acceptance/build_spec.rb +0 -86
- data/spec/acceptance/build_stubbed_spec.rb +0 -161
- data/spec/acceptance/callbacks_spec.rb +0 -253
- data/spec/acceptance/create_list_spec.rb +0 -97
- data/spec/acceptance/create_pair_spec.rb +0 -32
- data/spec/acceptance/create_spec.rb +0 -116
- data/spec/acceptance/define_child_before_parent_spec.rb +0 -21
- data/spec/acceptance/defining_methods_inside_a_factory_spec.rb +0 -17
- data/spec/acceptance/definition_camel_string_spec.rb +0 -15
- data/spec/acceptance/definition_spec.rb +0 -21
- data/spec/acceptance/definition_without_block_spec.rb +0 -15
- data/spec/acceptance/global_initialize_with_spec.rb +0 -82
- data/spec/acceptance/global_to_create_spec.rb +0 -122
- data/spec/acceptance/initialize_with_spec.rb +0 -217
- data/spec/acceptance/keyed_by_class_spec.rb +0 -22
- data/spec/acceptance/lint_spec.rb +0 -65
- data/spec/acceptance/modify_factories_spec.rb +0 -184
- data/spec/acceptance/modify_inherited_spec.rb +0 -52
- data/spec/acceptance/nested_attributes_spec.rb +0 -32
- data/spec/acceptance/overrides_spec.rb +0 -61
- data/spec/acceptance/parent_spec.rb +0 -90
- data/spec/acceptance/private_attributes_spec.rb +0 -20
- data/spec/acceptance/register_strategies_spec.rb +0 -136
- data/spec/acceptance/sequence_context_spec.rb +0 -52
- data/spec/acceptance/sequence_spec.rb +0 -61
- data/spec/acceptance/skip_create_spec.rb +0 -19
- data/spec/acceptance/stub_spec.rb +0 -62
- data/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb +0 -51
- data/spec/acceptance/traits_spec.rb +0 -727
- data/spec/acceptance/transient_attributes_spec.rb +0 -151
- data/spec/factory_girl/aliases_spec.rb +0 -31
- data/spec/factory_girl/attribute/association_spec.rb +0 -28
- data/spec/factory_girl/attribute/dynamic_spec.rb +0 -52
- data/spec/factory_girl/attribute/sequence_spec.rb +0 -16
- data/spec/factory_girl/attribute/static_spec.rb +0 -19
- data/spec/factory_girl/attribute_list_spec.rb +0 -143
- data/spec/factory_girl/attribute_spec.rb +0 -16
- data/spec/factory_girl/callback_spec.rb +0 -41
- data/spec/factory_girl/declaration/implicit_spec.rb +0 -25
- data/spec/factory_girl/declaration_list_spec.rb +0 -67
- data/spec/factory_girl/definition_proxy_spec.rb +0 -215
- data/spec/factory_girl/definition_spec.rb +0 -64
- data/spec/factory_girl/disallows_duplicates_registry_spec.rb +0 -19
- data/spec/factory_girl/evaluator_class_definer_spec.rb +0 -77
- data/spec/factory_girl/factory_spec.rb +0 -259
- data/spec/factory_girl/find_definitions_spec.rb +0 -110
- data/spec/factory_girl/null_factory_spec.rb +0 -13
- data/spec/factory_girl/null_object_spec.rb +0 -22
- data/spec/factory_girl/registry_spec.rb +0 -68
- data/spec/factory_girl/sequence_spec.rb +0 -95
- data/spec/factory_girl/strategy/attributes_for_spec.rb +0 -18
- data/spec/factory_girl/strategy/build_spec.rb +0 -7
- data/spec/factory_girl/strategy/create_spec.rb +0 -25
- data/spec/factory_girl/strategy/stub_spec.rb +0 -38
- data/spec/factory_girl/strategy_calculator_spec.rb +0 -29
- data/spec/factory_girl_spec.rb +0 -22
- data/spec/spec_helper.rb +0 -26
- data/spec/support/macros/define_constant.rb +0 -86
- data/spec/support/matchers/callback.rb +0 -9
- data/spec/support/matchers/declaration.rb +0 -83
- data/spec/support/matchers/delegate.rb +0 -44
- data/spec/support/matchers/trait.rb +0 -11
- data/spec/support/shared_examples/strategy.rb +0 -90
@@ -1,97 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "create multiple instances" do
|
4
|
-
before do
|
5
|
-
define_model('Post', title: :string, position: :integer)
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
factory(:post) do |post|
|
9
|
-
post.title "Through the Looking Glass"
|
10
|
-
post.position { rand(10**4) }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context "without default attributes" do
|
16
|
-
subject { FactoryGirl.create_list(:post, 20) }
|
17
|
-
|
18
|
-
its(:length) { should eq 20 }
|
19
|
-
|
20
|
-
it "creates all the posts" do
|
21
|
-
subject.each do |record|
|
22
|
-
expect(record).not_to be_new_record
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
it "uses the default factory values" do
|
27
|
-
subject.each do |record|
|
28
|
-
expect(record.title).to eq "Through the Looking Glass"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context "with default attributes" do
|
34
|
-
subject { FactoryGirl.create_list(:post, 20, title: "The Hunting of the Snark") }
|
35
|
-
|
36
|
-
it "overrides the default values" do
|
37
|
-
subject.each do |record|
|
38
|
-
expect(record.title).to eq "The Hunting of the Snark"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context "with a block" do
|
44
|
-
subject do
|
45
|
-
FactoryGirl.create_list(:post, 20, title: "The Listing of the Block") do |post|
|
46
|
-
post.position = post.id
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
it "uses the new values" do
|
51
|
-
subject.each_with_index do |record, index|
|
52
|
-
expect(record.position).to eq record.id
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe "multiple creates and transient attributes to dynamically build attribute lists" do
|
59
|
-
before do
|
60
|
-
define_model('User', name: :string) do
|
61
|
-
has_many :posts
|
62
|
-
end
|
63
|
-
|
64
|
-
define_model('Post', title: :string, user_id: :integer) do
|
65
|
-
belongs_to :user
|
66
|
-
end
|
67
|
-
|
68
|
-
FactoryGirl.define do
|
69
|
-
factory :post do
|
70
|
-
title "Through the Looking Glass"
|
71
|
-
user
|
72
|
-
end
|
73
|
-
|
74
|
-
factory :user do
|
75
|
-
name "John Doe"
|
76
|
-
|
77
|
-
factory :user_with_posts do
|
78
|
-
transient do
|
79
|
-
posts_count 5
|
80
|
-
end
|
81
|
-
|
82
|
-
after(:create) do |user, evaluator|
|
83
|
-
FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
it "generates the correct number of posts" do
|
91
|
-
expect(FactoryGirl.create(:user_with_posts).posts.length).to eq 5
|
92
|
-
end
|
93
|
-
|
94
|
-
it "allows the number of posts to be modified" do
|
95
|
-
expect(FactoryGirl.create(:user_with_posts, posts_count: 2).posts.length).to eq 2
|
96
|
-
end
|
97
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "create multiple instances" do
|
4
|
-
before do
|
5
|
-
define_model('Post', title: :string, position: :integer)
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
factory(:post) do |post|
|
9
|
-
post.title "Through the Looking Glass"
|
10
|
-
post.position { rand(10**4) }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context "without default attributes" do
|
16
|
-
subject { FactoryGirl.create_pair(:post) }
|
17
|
-
|
18
|
-
its(:length) { should eq 2 }
|
19
|
-
|
20
|
-
it "creates all the posts" do
|
21
|
-
subject.each do |record|
|
22
|
-
expect(record).not_to be_new_record
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
it "uses the default factory values" do
|
27
|
-
subject.each do |record|
|
28
|
-
expect(record.title).to eq "Through the Looking Glass"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,116 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "a created instance" do
|
4
|
-
include FactoryGirl::Syntax::Methods
|
5
|
-
|
6
|
-
before do
|
7
|
-
define_model('User')
|
8
|
-
|
9
|
-
define_model('Post', user_id: :integer) do
|
10
|
-
belongs_to :user
|
11
|
-
end
|
12
|
-
|
13
|
-
FactoryGirl.define do
|
14
|
-
factory :user
|
15
|
-
|
16
|
-
factory :post do
|
17
|
-
user
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
subject { create('post') }
|
23
|
-
|
24
|
-
it { should_not be_new_record }
|
25
|
-
|
26
|
-
it "assigns and saves associations" do
|
27
|
-
expect(subject.user).to be_kind_of(User)
|
28
|
-
expect(subject.user).not_to be_new_record
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "a created instance, specifying strategy: :build" do
|
33
|
-
include FactoryGirl::Syntax::Methods
|
34
|
-
|
35
|
-
before do
|
36
|
-
define_model('User')
|
37
|
-
|
38
|
-
define_model('Post', user_id: :integer) do
|
39
|
-
belongs_to :user
|
40
|
-
end
|
41
|
-
|
42
|
-
FactoryGirl.define do
|
43
|
-
factory :user
|
44
|
-
|
45
|
-
factory :post do
|
46
|
-
association(:user, strategy: :build)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
subject { create(:post) }
|
52
|
-
|
53
|
-
it "saves associations (strategy: :build only affects build, not create)" do
|
54
|
-
expect(subject.user).to be_kind_of(User)
|
55
|
-
expect(subject.user).not_to be_new_record
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe "a custom create" do
|
60
|
-
include FactoryGirl::Syntax::Methods
|
61
|
-
|
62
|
-
before do
|
63
|
-
define_class('User') do
|
64
|
-
def initialize
|
65
|
-
@persisted = false
|
66
|
-
end
|
67
|
-
|
68
|
-
def persist
|
69
|
-
@persisted = true
|
70
|
-
end
|
71
|
-
|
72
|
-
def persisted?
|
73
|
-
@persisted
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
FactoryGirl.define do
|
78
|
-
factory :user do
|
79
|
-
to_create do |user|
|
80
|
-
user.persist
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
it "uses the custom create block instead of save" do
|
87
|
-
expect(FactoryGirl.create(:user)).to be_persisted
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe "calling `create` with a block" do
|
92
|
-
include FactoryGirl::Syntax::Methods
|
93
|
-
|
94
|
-
before do
|
95
|
-
define_model('Company', name: :string)
|
96
|
-
|
97
|
-
FactoryGirl.define do
|
98
|
-
factory :company
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
it "passes the created instance" do
|
103
|
-
create(:company, name: 'thoughtbot') do |company|
|
104
|
-
expect(company.name).to eq('thoughtbot')
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
it "returns the created instance" do
|
109
|
-
expected = nil
|
110
|
-
result = create(:company) do |company|
|
111
|
-
expected = company
|
112
|
-
"hello!"
|
113
|
-
end
|
114
|
-
expect(result).to eq expected
|
115
|
-
end
|
116
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "defining a child factory before a parent" do
|
4
|
-
before do
|
5
|
-
define_model("User", name: :string, admin: :boolean, email: :string, upper_email: :string, login: :string)
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
factory :admin, parent: :user do
|
9
|
-
admin true
|
10
|
-
end
|
11
|
-
|
12
|
-
factory :user do
|
13
|
-
name "awesome"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
it "creates admin factories correctly" do
|
19
|
-
expect(FactoryGirl.create(:admin)).to be_admin
|
20
|
-
end
|
21
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'defining methods inside FactoryGirl' do
|
4
|
-
it 'raises with a meaningful message' do
|
5
|
-
define_model('User')
|
6
|
-
|
7
|
-
expect do
|
8
|
-
FactoryGirl.define do
|
9
|
-
factory :user do
|
10
|
-
def generate_name
|
11
|
-
'John Doe'
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end.to raise_error FactoryGirl::MethodDefinitionError, /Defining methods in blocks \(trait or factory\) is not supported \(generate_name\)/
|
16
|
-
end
|
17
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "an instance generated by a factory named a camel case string " do
|
4
|
-
before do
|
5
|
-
define_model("UserModel")
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
factory 'UserModel', class: UserModel
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
it "registers the UserModel factory" do
|
13
|
-
expect(FactoryGirl.factory_by_name('UserModel')).to be_a(FactoryGirl::Factory)
|
14
|
-
end
|
15
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "an instance generated by a factory with a custom class name" do
|
4
|
-
before do
|
5
|
-
define_model("User", admin: :boolean)
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
factory :user
|
9
|
-
|
10
|
-
factory :admin, class: User do
|
11
|
-
admin { true }
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
subject { FactoryGirl.create(:admin) }
|
17
|
-
|
18
|
-
it { should be_kind_of(User) }
|
19
|
-
it { should be_admin }
|
20
|
-
end
|
21
|
-
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "an instance generated by a factory" do
|
4
|
-
before do
|
5
|
-
define_model("User")
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
factory :user
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
it "registers the user factory" do
|
13
|
-
expect(FactoryGirl.factory_by_name(:user)).to be_a(FactoryGirl::Factory)
|
14
|
-
end
|
15
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'global initialize_with' do
|
4
|
-
before do
|
5
|
-
define_class('User') do
|
6
|
-
attr_accessor:name
|
7
|
-
|
8
|
-
def initialize(name)
|
9
|
-
@name = name
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
define_class('Post') do
|
14
|
-
attr_reader :name
|
15
|
-
|
16
|
-
def initialize(name)
|
17
|
-
@name = name
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
FactoryGirl.define do
|
22
|
-
initialize_with { new("initialize_with") }
|
23
|
-
|
24
|
-
trait :with_initialize_with do
|
25
|
-
initialize_with { new("trait initialize_with") }
|
26
|
-
end
|
27
|
-
|
28
|
-
factory :user do
|
29
|
-
factory :child_user
|
30
|
-
|
31
|
-
factory :child_user_with_trait do
|
32
|
-
with_initialize_with
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
factory :post do
|
37
|
-
factory :child_post
|
38
|
-
|
39
|
-
factory :child_post_with_trait do
|
40
|
-
with_initialize_with
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'handles base initialize_with' do
|
47
|
-
expect(FactoryGirl.build(:user).name).to eq 'initialize_with'
|
48
|
-
expect(FactoryGirl.build(:post).name).to eq 'initialize_with'
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'handles child initialize_with' do
|
52
|
-
expect(FactoryGirl.build(:child_user).name).to eq 'initialize_with'
|
53
|
-
expect(FactoryGirl.build(:child_post).name).to eq 'initialize_with'
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'handles child initialize_with with trait' do
|
57
|
-
expect(FactoryGirl.build(:child_user_with_trait).name).to eq 'trait initialize_with'
|
58
|
-
expect(FactoryGirl.build(:child_post_with_trait).name).to eq 'trait initialize_with'
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'handles inline trait override' do
|
62
|
-
expect(FactoryGirl.build(:child_user, :with_initialize_with).name).to eq 'trait initialize_with'
|
63
|
-
expect(FactoryGirl.build(:child_post, :with_initialize_with).name).to eq 'trait initialize_with'
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'uses initialize_with globally across FactoryGirl.define' do
|
67
|
-
define_class('Company') do
|
68
|
-
attr_reader :name
|
69
|
-
|
70
|
-
def initialize(name)
|
71
|
-
@name = name
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
FactoryGirl.define do
|
76
|
-
factory :company
|
77
|
-
end
|
78
|
-
|
79
|
-
expect(FactoryGirl.build(:company).name).to eq 'initialize_with'
|
80
|
-
expect(FactoryGirl.build(:company, :with_initialize_with).name).to eq 'trait initialize_with'
|
81
|
-
end
|
82
|
-
end
|
@@ -1,122 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'global to_create' do
|
4
|
-
before do
|
5
|
-
define_model('User', name: :string)
|
6
|
-
define_model('Post', name: :string)
|
7
|
-
|
8
|
-
FactoryGirl.define do
|
9
|
-
to_create { |instance| instance.name = 'persisted!' }
|
10
|
-
|
11
|
-
trait :override_to_create do
|
12
|
-
to_create { |instance| instance.name = 'override' }
|
13
|
-
end
|
14
|
-
|
15
|
-
factory :user do
|
16
|
-
name 'John Doe'
|
17
|
-
|
18
|
-
factory :child_user
|
19
|
-
|
20
|
-
factory :child_user_with_trait do
|
21
|
-
override_to_create
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
factory :post do
|
26
|
-
name 'Great title'
|
27
|
-
|
28
|
-
factory :child_post
|
29
|
-
|
30
|
-
factory :child_post_with_trait do
|
31
|
-
override_to_create
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'handles base to_create' do
|
38
|
-
expect(FactoryGirl.create(:user).name).to eq 'persisted!'
|
39
|
-
expect(FactoryGirl.create(:post).name).to eq 'persisted!'
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'handles child to_create' do
|
43
|
-
expect(FactoryGirl.create(:child_user).name).to eq 'persisted!'
|
44
|
-
expect(FactoryGirl.create(:child_post).name).to eq 'persisted!'
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'handles child to_create with trait' do
|
48
|
-
expect(FactoryGirl.create(:child_user_with_trait).name).to eq 'override'
|
49
|
-
expect(FactoryGirl.create(:child_post_with_trait).name).to eq 'override'
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'handles inline trait override' do
|
53
|
-
expect(FactoryGirl.create(:child_user, :override_to_create).name).to eq 'override'
|
54
|
-
expect(FactoryGirl.create(:child_post, :override_to_create).name).to eq 'override'
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'uses to_create globally across FactoryGirl.define' do
|
58
|
-
define_model('Company', name: :string)
|
59
|
-
|
60
|
-
FactoryGirl.define do
|
61
|
-
factory :company
|
62
|
-
end
|
63
|
-
|
64
|
-
expect(FactoryGirl.create(:company).name).to eq 'persisted!'
|
65
|
-
expect(FactoryGirl.create(:company, :override_to_create).name).to eq 'override'
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe 'global skip_create' do
|
70
|
-
before do
|
71
|
-
define_model('User', name: :string)
|
72
|
-
define_model('Post', name: :string)
|
73
|
-
|
74
|
-
FactoryGirl.define do
|
75
|
-
skip_create
|
76
|
-
|
77
|
-
trait :override_to_create do
|
78
|
-
to_create { |instance| instance.name = 'override' }
|
79
|
-
end
|
80
|
-
|
81
|
-
factory :user do
|
82
|
-
name 'John Doe'
|
83
|
-
|
84
|
-
factory :child_user
|
85
|
-
|
86
|
-
factory :child_user_with_trait do
|
87
|
-
override_to_create
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
factory :post do
|
92
|
-
name 'Great title'
|
93
|
-
|
94
|
-
factory :child_post
|
95
|
-
|
96
|
-
factory :child_post_with_trait do
|
97
|
-
override_to_create
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'does not persist any record' do
|
104
|
-
expect(FactoryGirl.create(:user)).to be_new_record
|
105
|
-
expect(FactoryGirl.create(:post)).to be_new_record
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'does not persist child records' do
|
109
|
-
expect(FactoryGirl.create(:child_user)).to be_new_record
|
110
|
-
expect(FactoryGirl.create(:child_post)).to be_new_record
|
111
|
-
end
|
112
|
-
|
113
|
-
it 'honors overridden to_create' do
|
114
|
-
expect(FactoryGirl.create(:child_user_with_trait).name).to eq 'override'
|
115
|
-
expect(FactoryGirl.create(:child_post_with_trait).name).to eq 'override'
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'honors inline trait to_create' do
|
119
|
-
expect(FactoryGirl.create(:child_user, :override_to_create).name).to eq 'override'
|
120
|
-
expect(FactoryGirl.create(:child_post, :override_to_create).name).to eq 'override'
|
121
|
-
end
|
122
|
-
end
|