factory_girl 3.2.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Appraisals +3 -3
- data/GETTING_STARTED.md +74 -15
- data/Gemfile.lock +38 -44
- data/NEWS +12 -0
- data/gemfiles/3.0.gemfile +1 -1
- data/gemfiles/3.0.gemfile.lock +23 -24
- data/gemfiles/3.1.gemfile +1 -1
- data/gemfiles/3.1.gemfile.lock +19 -19
- data/gemfiles/3.2.gemfile +1 -1
- data/gemfiles/3.2.gemfile.lock +10 -10
- data/lib/factory_girl.rb +39 -37
- data/lib/factory_girl/aliases.rb +3 -4
- data/lib/factory_girl/attribute.rb +33 -11
- data/lib/factory_girl/attribute/association.rb +3 -2
- data/lib/factory_girl/attribute/dynamic.rb +3 -2
- data/lib/factory_girl/attribute/sequence.rb +1 -2
- data/lib/factory_girl/attribute/static.rb +3 -2
- data/lib/factory_girl/attribute_assigner.rb +6 -5
- data/lib/factory_girl/attribute_list.rb +16 -3
- data/lib/factory_girl/callback.rb +7 -3
- data/lib/factory_girl/callbacks_observer.rb +1 -0
- data/lib/factory_girl/configuration.rb +24 -0
- data/lib/factory_girl/declaration.rb +5 -4
- data/lib/factory_girl/declaration/association.rb +1 -0
- data/lib/factory_girl/declaration/dynamic.rb +1 -0
- data/lib/factory_girl/declaration/implicit.rb +1 -0
- data/lib/factory_girl/declaration/static.rb +1 -0
- data/lib/factory_girl/declaration_list.rb +1 -0
- data/lib/factory_girl/definition.rb +22 -3
- data/lib/factory_girl/definition_list.rb +31 -0
- data/lib/factory_girl/definition_proxy.rb +19 -4
- data/lib/factory_girl/evaluation.rb +3 -3
- data/lib/factory_girl/evaluator.rb +24 -14
- data/lib/factory_girl/evaluator_class_definer.rb +2 -13
- data/lib/factory_girl/factory.rb +22 -24
- data/lib/factory_girl/factory_runner.rb +6 -3
- data/lib/factory_girl/find_definitions.rb +2 -2
- data/lib/factory_girl/null_factory.rb +3 -1
- data/lib/factory_girl/null_object.rb +1 -0
- data/lib/factory_girl/reload.rb +4 -6
- data/lib/factory_girl/sequence.rb +3 -2
- data/lib/factory_girl/step_definitions.rb +1 -0
- data/lib/factory_girl/strategy/attributes_for.rb +1 -1
- data/lib/factory_girl/strategy/stub.rb +6 -6
- data/lib/factory_girl/strategy_calculator.rb +1 -0
- data/lib/factory_girl/strategy_syntax_method_registrar.rb +37 -0
- data/lib/factory_girl/syntax.rb +5 -4
- data/lib/factory_girl/syntax/blueprint.rb +5 -8
- data/lib/factory_girl/syntax/default.rb +18 -6
- data/lib/factory_girl/syntax/generate.rb +10 -13
- data/lib/factory_girl/syntax/make.rb +8 -11
- data/lib/factory_girl/syntax/methods.rb +76 -36
- data/lib/factory_girl/syntax/sham.rb +3 -2
- data/lib/factory_girl/syntax/vintage.rb +9 -9
- data/lib/factory_girl/syntax_runner.rb +1 -0
- data/lib/factory_girl/trait.rb +5 -4
- data/lib/factory_girl/version.rb +1 -2
- data/spec/acceptance/activesupport_instrumentation_spec.rb +15 -2
- data/spec/acceptance/callbacks_spec.rb +113 -9
- data/spec/acceptance/create_list_spec.rb +1 -1
- data/spec/acceptance/global_initialize_with_spec.rb +82 -0
- data/spec/acceptance/global_to_create_spec.rb +122 -0
- data/spec/acceptance/modify_factories_spec.rb +2 -2
- data/spec/acceptance/parent_spec.rb +1 -1
- data/spec/acceptance/register_strategies_spec.rb +8 -0
- data/spec/acceptance/syntax/vintage_spec.rb +8 -8
- data/spec/acceptance/traits_spec.rb +145 -3
- data/spec/acceptance/transient_attributes_spec.rb +1 -1
- data/spec/factory_girl/attribute_list_spec.rb +66 -1
- data/spec/factory_girl/attribute_spec.rb +1 -1
- data/spec/factory_girl/definition_proxy_spec.rb +6 -6
- data/spec/factory_girl/definition_spec.rb +22 -16
- data/spec/factory_girl/factory_spec.rb +6 -4
- data/spec/factory_girl/strategy/build_spec.rb +2 -2
- data/spec/factory_girl/strategy/create_spec.rb +1 -1
- data/spec/factory_girl/strategy/stub_spec.rb +2 -2
- data/spec/factory_girl/strategy_calculator_spec.rb +20 -14
- data/spec/support/shared_examples/strategy.rb +8 -9
- metadata +94 -29
- data/gemfiles/2.3.gemfile +0 -7
@@ -22,11 +22,12 @@ module FactoryGirl
|
|
22
22
|
# value does not provide any tangible benefits over an ascending sequence.
|
23
23
|
#
|
24
24
|
# This syntax was derived from Pete Yandell's machinist.
|
25
|
+
# @api private
|
25
26
|
module Sham
|
26
|
-
module Sham
|
27
|
+
module Sham
|
27
28
|
def self.method_missing(name, *args, &block)
|
28
29
|
if block_given?
|
29
|
-
ActiveSupport::Deprecation.warn
|
30
|
+
ActiveSupport::Deprecation.warn 'Sham.sequence is deprecated; use the FactoryGirl.define syntax instead', caller
|
30
31
|
start_value = args.first
|
31
32
|
FactoryGirl.register_sequence(Sequence.new(name, start_value || 1, &block))
|
32
33
|
else
|
@@ -21,7 +21,7 @@ module FactoryGirl
|
|
21
21
|
# Yields: +Factory+
|
22
22
|
# The newly created factory.
|
23
23
|
def self.define(name, options = {})
|
24
|
-
ActiveSupport::Deprecation.warn
|
24
|
+
ActiveSupport::Deprecation.warn 'Factory.define is deprecated; use the FactoryGirl.define block syntax to declare your factory.', caller
|
25
25
|
factory = FactoryGirl::Factory.new(name, options)
|
26
26
|
proxy = FactoryGirl::DefinitionProxy.new(factory)
|
27
27
|
yield(proxy)
|
@@ -44,7 +44,7 @@ module FactoryGirl
|
|
44
44
|
#
|
45
45
|
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
|
46
46
|
def self.sequence(name, start_value = 1, &block)
|
47
|
-
ActiveSupport::Deprecation.warn
|
47
|
+
ActiveSupport::Deprecation.warn 'Factory.sequence is deprecated; use the FactoryGirl.define block syntax to declare your sequence.', caller
|
48
48
|
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
|
49
49
|
end
|
50
50
|
|
@@ -57,7 +57,7 @@ module FactoryGirl
|
|
57
57
|
# Returns:
|
58
58
|
# The next value in the sequence. (Object)
|
59
59
|
def self.next(name)
|
60
|
-
ActiveSupport::Deprecation.warn
|
60
|
+
ActiveSupport::Deprecation.warn 'Factory.next is deprecated; use FactoryGirl.generate instead.', caller
|
61
61
|
FactoryGirl.generate(name)
|
62
62
|
end
|
63
63
|
|
@@ -86,31 +86,31 @@ module FactoryGirl
|
|
86
86
|
# # will be used instead.
|
87
87
|
# Factory(:post, user_id: 1)
|
88
88
|
def self.alias(pattern, replace)
|
89
|
-
ActiveSupport::Deprecation.warn
|
89
|
+
ActiveSupport::Deprecation.warn 'Factory.alias is deprecated; use FactoryGirl.aliases << [pattern, replace] instead.', caller
|
90
90
|
FactoryGirl.aliases << [pattern, replace]
|
91
91
|
end
|
92
92
|
|
93
93
|
# Alias for FactoryGirl.attributes_for
|
94
94
|
def self.attributes_for(name, overrides = {})
|
95
|
-
ActiveSupport::Deprecation.warn
|
95
|
+
ActiveSupport::Deprecation.warn 'Factory.attributes_for is deprecated; use FactoryGirl.attributes_for instead.', caller
|
96
96
|
FactoryGirl.attributes_for(name, overrides)
|
97
97
|
end
|
98
98
|
|
99
99
|
# Alias for FactoryGirl.build
|
100
100
|
def self.build(name, overrides = {})
|
101
|
-
ActiveSupport::Deprecation.warn
|
101
|
+
ActiveSupport::Deprecation.warn 'Factory.build is deprecated; use FactoryGirl.build instead.', caller
|
102
102
|
FactoryGirl.build(name, overrides)
|
103
103
|
end
|
104
104
|
|
105
105
|
# Alias for FactoryGirl.create
|
106
106
|
def self.create(name, overrides = {})
|
107
|
-
ActiveSupport::Deprecation.warn
|
107
|
+
ActiveSupport::Deprecation.warn 'Factory.create is deprecated; use FactoryGirl.create instead.', caller
|
108
108
|
FactoryGirl.create(name, overrides)
|
109
109
|
end
|
110
110
|
|
111
111
|
# Alias for FactoryGirl.build_stubbed.
|
112
112
|
def self.stub(name, overrides = {})
|
113
|
-
ActiveSupport::Deprecation.warn
|
113
|
+
ActiveSupport::Deprecation.warn 'Factory.stub is deprecated; use FactoryGirl.build_stubbed instead.', caller
|
114
114
|
FactoryGirl.build_stubbed(name, overrides)
|
115
115
|
end
|
116
116
|
end
|
@@ -120,7 +120,7 @@ module FactoryGirl
|
|
120
120
|
# Example:
|
121
121
|
# Factory(:user, name: 'Joe')
|
122
122
|
def Factory(name, attrs = {})
|
123
|
-
ActiveSupport::Deprecation.warn
|
123
|
+
ActiveSupport::Deprecation.warn 'Factory(:name) is deprecated; use FactoryGirl.create(:name) instead.', caller
|
124
124
|
FactoryGirl.create(name, attrs)
|
125
125
|
end
|
126
126
|
end
|
data/lib/factory_girl/trait.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
module FactoryGirl
|
2
|
+
# @api private
|
2
3
|
class Trait
|
3
|
-
attr_reader :name
|
4
|
+
attr_reader :name, :definition
|
4
5
|
|
5
|
-
def initialize(name, &block)
|
6
|
+
def initialize(name, &block)
|
6
7
|
@name = name
|
7
8
|
@block = block
|
8
|
-
@definition = Definition.new
|
9
|
+
@definition = Definition.new(@name)
|
9
10
|
|
10
11
|
proxy = FactoryGirl::DefinitionProxy.new(@definition)
|
11
12
|
proxy.instance_eval(&@block) if block_given?
|
12
13
|
end
|
13
14
|
|
14
|
-
delegate :add_callback, :declare_attribute, :to_create, :define_trait,
|
15
|
+
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
|
15
16
|
:callbacks, :attributes, to: :@definition
|
16
17
|
|
17
18
|
def names
|
data/lib/factory_girl/version.rb
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
+
unless ActiveSupport::Notifications.respond_to?(:subscribed)
|
4
|
+
module SubscribedBehavior
|
5
|
+
def subscribed(callback, *args, &block)
|
6
|
+
subscriber = subscribe(*args, &callback)
|
7
|
+
yield
|
8
|
+
ensure
|
9
|
+
unsubscribe(subscriber)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveSupport::Notifications.extend SubscribedBehavior
|
14
|
+
end
|
15
|
+
|
3
16
|
describe "using ActiveSupport::Instrumentation to track factory interaction" do
|
4
17
|
before do
|
5
18
|
define_model("User", email: :string)
|
@@ -8,7 +21,7 @@ describe "using ActiveSupport::Instrumentation to track factory interaction" do
|
|
8
21
|
email "john@example.com"
|
9
22
|
|
10
23
|
factory :slow_user do
|
11
|
-
|
24
|
+
after(:build) { Kernel.sleep(0.1) }
|
12
25
|
end
|
13
26
|
end
|
14
27
|
|
@@ -22,7 +35,7 @@ describe "using ActiveSupport::Instrumentation to track factory interaction" do
|
|
22
35
|
FactoryGirl.build(:slow_user)
|
23
36
|
end
|
24
37
|
|
25
|
-
time_to_execute.should
|
38
|
+
time_to_execute.should >= 0.1
|
26
39
|
end
|
27
40
|
|
28
41
|
it "builds the correct payload" do
|
@@ -6,35 +6,35 @@ describe "callbacks" do
|
|
6
6
|
|
7
7
|
FactoryGirl.define do
|
8
8
|
factory :user_with_callbacks, class: :user do
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
after(:stub) { |user| user.first_name = 'Stubby' }
|
10
|
+
after(:build) { |user| user.first_name = 'Buildy' }
|
11
|
+
after(:create) { |user| user.last_name = 'Createy' }
|
12
12
|
end
|
13
13
|
|
14
14
|
factory :user_with_inherited_callbacks, parent: :user_with_callbacks do
|
15
|
-
|
16
|
-
|
15
|
+
after(:stub) { |user| user.last_name = 'Double-Stubby' }
|
16
|
+
after(:build) { |user| user.first_name = 'Child-Buildy' }
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
it "runs the
|
21
|
+
it "runs the after(:stub) callback when stubbing" do
|
22
22
|
user = FactoryGirl.build_stubbed(:user_with_callbacks)
|
23
23
|
user.first_name.should == 'Stubby'
|
24
24
|
end
|
25
25
|
|
26
|
-
it "runs the
|
26
|
+
it "runs the after(:build) callback when building" do
|
27
27
|
user = FactoryGirl.build(:user_with_callbacks)
|
28
28
|
user.first_name.should == 'Buildy'
|
29
29
|
end
|
30
30
|
|
31
|
-
it "runs both the
|
31
|
+
it "runs both the after(:build) and after(:create) callbacks when creating" do
|
32
32
|
user = FactoryGirl.create(:user_with_callbacks)
|
33
33
|
user.first_name.should == 'Buildy'
|
34
34
|
user.last_name.should == 'Createy'
|
35
35
|
end
|
36
36
|
|
37
|
-
it "runs both the
|
37
|
+
it "runs both the after(:stub) callback on the factory and the inherited after(:stub) callback" do
|
38
38
|
user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
|
39
39
|
user.first_name.should == 'Stubby'
|
40
40
|
user.last_name.should == 'Double-Stubby'
|
@@ -45,3 +45,107 @@ describe "callbacks" do
|
|
45
45
|
user.first_name.should == 'Child-Buildy'
|
46
46
|
end
|
47
47
|
end
|
48
|
+
|
49
|
+
describe "callbacks using syntax methods without referencing FactoryGirl explicitly" do
|
50
|
+
before do
|
51
|
+
define_model("User", first_name: :string, last_name: :string)
|
52
|
+
|
53
|
+
FactoryGirl.define do
|
54
|
+
sequence(:sequence_1)
|
55
|
+
sequence(:sequence_2)
|
56
|
+
sequence(:sequence_3)
|
57
|
+
|
58
|
+
factory :user do
|
59
|
+
after(:stub) { generate(:sequence_3) }
|
60
|
+
after(:build) {|user| user.first_name = generate(:sequence_1) }
|
61
|
+
after(:create) {|user, evaluator| user.last_name = generate(:sequence_2) }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "works when the callback has no variables" do
|
67
|
+
FactoryGirl.build_stubbed(:user)
|
68
|
+
FactoryGirl.generate(:sequence_3).should == 2
|
69
|
+
end
|
70
|
+
|
71
|
+
it "works when the callback has one variable" do
|
72
|
+
FactoryGirl.build(:user).first_name.should == 1
|
73
|
+
end
|
74
|
+
|
75
|
+
it "works when the callback has two variables" do
|
76
|
+
FactoryGirl.create(:user).last_name.should == 1
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "custom callbacks" do
|
81
|
+
let(:custom_before) do
|
82
|
+
Class.new do
|
83
|
+
def result(evaluation)
|
84
|
+
evaluation.object.tap do |instance|
|
85
|
+
evaluation.notify(:before_custom, instance)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
let(:custom_after) do
|
92
|
+
Class.new do
|
93
|
+
def result(evaluation)
|
94
|
+
evaluation.object.tap do |instance|
|
95
|
+
evaluation.notify(:after_custom, instance)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
let(:totally_custom) do
|
102
|
+
Class.new do
|
103
|
+
def result(evaluation)
|
104
|
+
evaluation.object.tap do |instance|
|
105
|
+
evaluation.notify(:totally_custom, instance)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
before do
|
112
|
+
define_model("User", first_name: :string, last_name: :string) do
|
113
|
+
def name
|
114
|
+
[first_name, last_name].join(" ")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
FactoryGirl.register_strategy(:custom_before, custom_before)
|
119
|
+
FactoryGirl.register_strategy(:custom_after, custom_after)
|
120
|
+
FactoryGirl.register_strategy(:totally_custom, totally_custom)
|
121
|
+
|
122
|
+
FactoryGirl.define do
|
123
|
+
factory :user do
|
124
|
+
first_name "John"
|
125
|
+
last_name "Doe"
|
126
|
+
|
127
|
+
before(:custom) {|instance| instance.first_name = "Overridden First" }
|
128
|
+
after(:custom) {|instance| instance.last_name = "Overridden Last" }
|
129
|
+
callback(:totally_custom) do |instance|
|
130
|
+
instance.first_name = "Totally"
|
131
|
+
instance.last_name = "Custom"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
it "runs a custom before callback when the proper strategy executes" do
|
138
|
+
FactoryGirl.build(:user).name.should == "John Doe"
|
139
|
+
FactoryGirl.custom_before(:user).name.should == "Overridden First Doe"
|
140
|
+
end
|
141
|
+
|
142
|
+
it "runs a custom after callback when the proper strategy executes" do
|
143
|
+
FactoryGirl.build(:user).name.should == "John Doe"
|
144
|
+
FactoryGirl.custom_after(:user).name.should == "John Overridden Last"
|
145
|
+
end
|
146
|
+
|
147
|
+
it "runs a custom callback without prepending before or after when the proper strategy executes" do
|
148
|
+
FactoryGirl.build(:user).name.should == "John Doe"
|
149
|
+
FactoryGirl.totally_custom(:user).name.should == "Totally Custom"
|
150
|
+
end
|
151
|
+
end
|
@@ -64,7 +64,7 @@ describe "multiple creates and ignored attributes to dynamically build attribute
|
|
64
64
|
posts_count 5
|
65
65
|
end
|
66
66
|
|
67
|
-
|
67
|
+
after(:create) do |user, evaluator|
|
68
68
|
FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
|
69
69
|
end
|
70
70
|
end
|
@@ -0,0 +1,82 @@
|
|
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
|
+
FactoryGirl.build(:user).name.should == 'initialize_with'
|
48
|
+
FactoryGirl.build(:post).name.should == 'initialize_with'
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'handles child initialize_with' do
|
52
|
+
FactoryGirl.build(:child_user).name.should == 'initialize_with'
|
53
|
+
FactoryGirl.build(:child_post).name.should == 'initialize_with'
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'handles child initialize_with with trait' do
|
57
|
+
FactoryGirl.build(:child_user_with_trait).name.should == 'trait initialize_with'
|
58
|
+
FactoryGirl.build(:child_post_with_trait).name.should == 'trait initialize_with'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'handles inline trait override' do
|
62
|
+
FactoryGirl.build(:child_user, :with_initialize_with).name.should == 'trait initialize_with'
|
63
|
+
FactoryGirl.build(:child_post, :with_initialize_with).name.should == '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
|
+
FactoryGirl.build(:company).name.should == 'initialize_with'
|
80
|
+
FactoryGirl.build(:company, :with_initialize_with).name.should == 'trait initialize_with'
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,122 @@
|
|
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
|
+
FactoryGirl.create(:user).name.should == 'persisted!'
|
39
|
+
FactoryGirl.create(:post).name.should == 'persisted!'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'handles child to_create' do
|
43
|
+
FactoryGirl.create(:child_user).name.should == 'persisted!'
|
44
|
+
FactoryGirl.create(:child_post).name.should == 'persisted!'
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'handles child to_create with trait' do
|
48
|
+
FactoryGirl.create(:child_user_with_trait).name.should == 'override'
|
49
|
+
FactoryGirl.create(:child_post_with_trait).name.should == 'override'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'handles inline trait override' do
|
53
|
+
FactoryGirl.create(:child_user, :override_to_create).name.should == 'override'
|
54
|
+
FactoryGirl.create(:child_post, :override_to_create).name.should == '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
|
+
FactoryGirl.create(:company).name.should == 'persisted!'
|
65
|
+
FactoryGirl.create(:company, :override_to_create).name.should == '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
|
+
FactoryGirl.create(:user).should be_new_record
|
105
|
+
FactoryGirl.create(:post).should be_new_record
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'does not persist child records' do
|
109
|
+
FactoryGirl.create(:child_user).should be_new_record
|
110
|
+
FactoryGirl.create(:child_post).should be_new_record
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'honors overridden to_create' do
|
114
|
+
FactoryGirl.create(:child_user_with_trait).name.should == 'override'
|
115
|
+
FactoryGirl.create(:child_post_with_trait).name.should == 'override'
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'honors inline trait to_create' do
|
119
|
+
FactoryGirl.create(:child_user, :override_to_create).name.should == 'override'
|
120
|
+
FactoryGirl.create(:child_post, :override_to_create).name.should == 'override'
|
121
|
+
end
|
122
|
+
end
|