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,56 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "build 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.build_list(:post, 20) }
|
17
|
-
|
18
|
-
its(:length) { should eq 20 }
|
19
|
-
|
20
|
-
it "builds (but doesn't save) all the posts" do
|
21
|
-
subject.each do |record|
|
22
|
-
expect(record).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.build_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.build_list(:post, 20, title: "The Listing of the Block") do |post|
|
46
|
-
post.position = post.id
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
it "correctly uses the set value" 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
|
@@ -1,86 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "a built 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 { build(:post) }
|
23
|
-
|
24
|
-
it { should 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 built instance with 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 { build(:post) }
|
52
|
-
|
53
|
-
it { should be_new_record }
|
54
|
-
|
55
|
-
it "assigns but does not save associations" do
|
56
|
-
expect(subject.user).to be_kind_of(User)
|
57
|
-
expect(subject.user).to be_new_record
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe "calling `build` with a block" do
|
62
|
-
include FactoryGirl::Syntax::Methods
|
63
|
-
|
64
|
-
before do
|
65
|
-
define_model('Company', name: :string)
|
66
|
-
|
67
|
-
FactoryGirl.define do
|
68
|
-
factory :company
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
it "passes the built instance" do
|
73
|
-
build(:company, name: 'thoughtbot') do |company|
|
74
|
-
expect(company.name).to eq('thoughtbot')
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
it "returns the built instance" do
|
79
|
-
expected = nil
|
80
|
-
result = build(:company) do |company|
|
81
|
-
expected = company
|
82
|
-
"hello!"
|
83
|
-
end
|
84
|
-
expect(result).to eq expected
|
85
|
-
end
|
86
|
-
end
|
@@ -1,161 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "a generated stub instance" do
|
4
|
-
include FactoryGirl::Syntax::Methods
|
5
|
-
|
6
|
-
before do
|
7
|
-
define_model('User')
|
8
|
-
|
9
|
-
define_model('Post', title: :string,
|
10
|
-
body: :string,
|
11
|
-
age: :integer,
|
12
|
-
user_id: :integer) do
|
13
|
-
belongs_to :user
|
14
|
-
end
|
15
|
-
|
16
|
-
FactoryGirl.define do
|
17
|
-
factory :user
|
18
|
-
|
19
|
-
factory :post do
|
20
|
-
title { "default title" }
|
21
|
-
body { "default body" }
|
22
|
-
user
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
subject { build_stubbed(:post, title: 'overridden title') }
|
28
|
-
|
29
|
-
it "assigns a default attribute" do
|
30
|
-
expect(subject.body).to eq 'default body'
|
31
|
-
end
|
32
|
-
|
33
|
-
it "assigns an overridden attribute" do
|
34
|
-
expect(subject.title).to eq 'overridden title'
|
35
|
-
end
|
36
|
-
|
37
|
-
it "assigns associations" do
|
38
|
-
expect(subject.user).not_to be_nil
|
39
|
-
end
|
40
|
-
|
41
|
-
it "has an id" do
|
42
|
-
expect(subject.id).to be > 0
|
43
|
-
end
|
44
|
-
|
45
|
-
it "generates unique ids" do
|
46
|
-
other_stub = build_stubbed(:post)
|
47
|
-
expect(subject.id).not_to eq other_stub.id
|
48
|
-
end
|
49
|
-
|
50
|
-
it "isn't a new record" do
|
51
|
-
expect(subject).not_to be_new_record
|
52
|
-
end
|
53
|
-
|
54
|
-
it "disables connection" do
|
55
|
-
expect { subject.connection }.to raise_error(RuntimeError)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "disables update_attribute" do
|
59
|
-
expect { subject.update_attribute(:title, "value") }.to raise_error(RuntimeError)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "disables reload" do
|
63
|
-
expect { subject.reload }.to raise_error(RuntimeError)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "disables destroy" do
|
67
|
-
expect { subject.destroy }.to raise_error(RuntimeError)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "disables save" do
|
71
|
-
expect { subject.save }.to raise_error(RuntimeError)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "disables increment" do
|
75
|
-
expect { subject.increment!(:age) }.to raise_error(RuntimeError)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "calling `build_stubbed` with a block" do
|
80
|
-
include FactoryGirl::Syntax::Methods
|
81
|
-
|
82
|
-
before do
|
83
|
-
define_model('Company', name: :string)
|
84
|
-
|
85
|
-
FactoryGirl.define do
|
86
|
-
factory :company
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
it "passes the stub instance" do
|
91
|
-
build_stubbed(:company, name: 'thoughtbot') do |company|
|
92
|
-
expect(company.name).to eq('thoughtbot')
|
93
|
-
expect { company.save }.to raise_error(RuntimeError)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
it "returns the stub instance" do
|
98
|
-
expected = nil
|
99
|
-
result = build_stubbed(:company) do |company|
|
100
|
-
expected = company
|
101
|
-
"hello!"
|
102
|
-
end
|
103
|
-
expect(result).to eq expected
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
describe "defaulting `created_at`" do
|
108
|
-
include FactoryGirl::Syntax::Methods
|
109
|
-
|
110
|
-
before do
|
111
|
-
define_model('ThingWithTimestamp', created_at: :datetime)
|
112
|
-
define_model('ThingWithoutTimestamp')
|
113
|
-
|
114
|
-
FactoryGirl.define do
|
115
|
-
factory :thing_with_timestamp
|
116
|
-
factory :thing_without_timestamp
|
117
|
-
end
|
118
|
-
|
119
|
-
Timecop.freeze 2012, 1, 1
|
120
|
-
end
|
121
|
-
|
122
|
-
it "defaults created_at for objects with created_at" do
|
123
|
-
expect(build_stubbed(:thing_with_timestamp).created_at).to eq Time.now
|
124
|
-
end
|
125
|
-
|
126
|
-
it "defaults created_at for objects with created_at to the correct time with zone" do
|
127
|
-
original_timezone = ENV['TZ']
|
128
|
-
ENV['TZ'] = 'UTC'
|
129
|
-
Time.zone = 'Eastern Time (US & Canada)'
|
130
|
-
|
131
|
-
expect(build_stubbed(:thing_with_timestamp).created_at.zone).to eq 'EST'
|
132
|
-
|
133
|
-
ENV['TZ'] = original_timezone
|
134
|
-
end
|
135
|
-
|
136
|
-
it "adds created_at to objects who don't have the method" do
|
137
|
-
expect(build_stubbed(:thing_without_timestamp)).to respond_to(:created_at)
|
138
|
-
end
|
139
|
-
|
140
|
-
it "allows overriding created_at for objects with created_at" do
|
141
|
-
expect(build_stubbed(:thing_with_timestamp, created_at: 3.days.ago).created_at).to eq 3.days.ago
|
142
|
-
end
|
143
|
-
|
144
|
-
it "doesn't allow setting created_at on an object that doesn't define it" do
|
145
|
-
expect { build_stubbed(:thing_without_timestamp, :created_at => Time.now) }.to raise_error(NoMethodError, /created_at=/)
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
describe 'defaulting `id`' do
|
150
|
-
before do
|
151
|
-
define_model('Post')
|
152
|
-
|
153
|
-
FactoryGirl.define do
|
154
|
-
factory :post
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
it 'allows overriding id' do
|
159
|
-
expect(FactoryGirl.build_stubbed(:post, id: 12).id).to eq 12
|
160
|
-
end
|
161
|
-
end
|
@@ -1,253 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "callbacks" do
|
4
|
-
before do
|
5
|
-
define_model("User", first_name: :string, last_name: :string)
|
6
|
-
|
7
|
-
FactoryGirl.define do
|
8
|
-
factory :user_with_callbacks, class: :user do
|
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
|
-
end
|
13
|
-
|
14
|
-
factory :user_with_inherited_callbacks, parent: :user_with_callbacks do
|
15
|
-
after(:stub) { |user| user.last_name = 'Double-Stubby' }
|
16
|
-
after(:build) { |user| user.first_name = 'Child-Buildy' }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
it "runs the after(:stub) callback when stubbing" do
|
22
|
-
user = FactoryGirl.build_stubbed(:user_with_callbacks)
|
23
|
-
expect(user.first_name).to eq 'Stubby'
|
24
|
-
end
|
25
|
-
|
26
|
-
it "runs the after(:build) callback when building" do
|
27
|
-
user = FactoryGirl.build(:user_with_callbacks)
|
28
|
-
expect(user.first_name).to eq 'Buildy'
|
29
|
-
end
|
30
|
-
|
31
|
-
it "runs both the after(:build) and after(:create) callbacks when creating" do
|
32
|
-
user = FactoryGirl.create(:user_with_callbacks)
|
33
|
-
expect(user.first_name).to eq 'Buildy'
|
34
|
-
expect(user.last_name).to eq 'Createy'
|
35
|
-
end
|
36
|
-
|
37
|
-
it "runs both the after(:stub) callback on the factory and the inherited after(:stub) callback" do
|
38
|
-
user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
|
39
|
-
expect(user.first_name).to eq 'Stubby'
|
40
|
-
expect(user.last_name).to eq 'Double-Stubby'
|
41
|
-
end
|
42
|
-
|
43
|
-
it "runs child callback after parent callback" do
|
44
|
-
user = FactoryGirl.build(:user_with_inherited_callbacks)
|
45
|
-
expect(user.first_name).to eq 'Child-Buildy'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe 'callbacks using Symbol#to_proc' do
|
50
|
-
before do
|
51
|
-
define_model('User') do
|
52
|
-
def confirmed?
|
53
|
-
!!@confirmed
|
54
|
-
end
|
55
|
-
|
56
|
-
def confirm!
|
57
|
-
@confirmed = true
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
FactoryGirl.define do
|
62
|
-
factory :user do
|
63
|
-
after :build, &:confirm!
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'runs the callback correctly' do
|
69
|
-
user = FactoryGirl.build(:user)
|
70
|
-
expect(user).to be_confirmed
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe "callbacks using syntax methods without referencing FactoryGirl explicitly" do
|
75
|
-
before do
|
76
|
-
define_model("User", first_name: :string, last_name: :string)
|
77
|
-
|
78
|
-
FactoryGirl.define do
|
79
|
-
sequence(:sequence_1)
|
80
|
-
sequence(:sequence_2)
|
81
|
-
sequence(:sequence_3)
|
82
|
-
|
83
|
-
factory :user do
|
84
|
-
after(:stub) { generate(:sequence_3) }
|
85
|
-
after(:build) { |user| user.first_name = generate(:sequence_1) }
|
86
|
-
after(:create) { |user, evaluator| user.last_name = generate(:sequence_2) }
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
it "works when the callback has no variables" do
|
92
|
-
FactoryGirl.build_stubbed(:user)
|
93
|
-
expect(FactoryGirl.generate(:sequence_3)).to eq 2
|
94
|
-
end
|
95
|
-
|
96
|
-
it "works when the callback has one variable" do
|
97
|
-
expect(FactoryGirl.build(:user).first_name).to eq 1
|
98
|
-
end
|
99
|
-
|
100
|
-
it "works when the callback has two variables" do
|
101
|
-
expect(FactoryGirl.create(:user).last_name).to eq 1
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe "custom callbacks" do
|
106
|
-
let(:custom_before) do
|
107
|
-
Class.new do
|
108
|
-
def result(evaluation)
|
109
|
-
evaluation.object.tap do |instance|
|
110
|
-
evaluation.notify(:before_custom, instance)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
let(:custom_after) do
|
117
|
-
Class.new do
|
118
|
-
def result(evaluation)
|
119
|
-
evaluation.object.tap do |instance|
|
120
|
-
evaluation.notify(:after_custom, instance)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
let(:totally_custom) do
|
127
|
-
Class.new do
|
128
|
-
def result(evaluation)
|
129
|
-
evaluation.object.tap do |instance|
|
130
|
-
evaluation.notify(:totally_custom, instance)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
before do
|
137
|
-
define_model("User", first_name: :string, last_name: :string) do
|
138
|
-
def name
|
139
|
-
[first_name, last_name].join(" ")
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
FactoryGirl.register_strategy(:custom_before, custom_before)
|
144
|
-
FactoryGirl.register_strategy(:custom_after, custom_after)
|
145
|
-
FactoryGirl.register_strategy(:totally_custom, totally_custom)
|
146
|
-
|
147
|
-
FactoryGirl.define do
|
148
|
-
factory :user do
|
149
|
-
first_name "John"
|
150
|
-
last_name "Doe"
|
151
|
-
|
152
|
-
before(:custom) { |instance| instance.first_name = "Overridden First" }
|
153
|
-
after(:custom) { |instance| instance.last_name = "Overridden Last" }
|
154
|
-
callback(:totally_custom) do |instance|
|
155
|
-
instance.first_name = "Totally"
|
156
|
-
instance.last_name = "Custom"
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
it "runs a custom before callback when the proper strategy executes" do
|
163
|
-
expect(FactoryGirl.build(:user).name).to eq "John Doe"
|
164
|
-
expect(FactoryGirl.custom_before(:user).name).to eq "Overridden First Doe"
|
165
|
-
end
|
166
|
-
|
167
|
-
it "runs a custom after callback when the proper strategy executes" do
|
168
|
-
expect(FactoryGirl.build(:user).name).to eq "John Doe"
|
169
|
-
expect(FactoryGirl.custom_after(:user).name).to eq "John Overridden Last"
|
170
|
-
end
|
171
|
-
|
172
|
-
it "runs a custom callback without prepending before or after when the proper strategy executes" do
|
173
|
-
expect(FactoryGirl.build(:user).name).to eq "John Doe"
|
174
|
-
expect(FactoryGirl.totally_custom(:user).name).to eq "Totally Custom"
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
describe 'binding a callback to multiple callbacks' do
|
179
|
-
before do
|
180
|
-
define_model('User', name: :string)
|
181
|
-
|
182
|
-
FactoryGirl.define do
|
183
|
-
factory :user do
|
184
|
-
callback(:before_create, :after_stub) do |instance|
|
185
|
-
instance.name = instance.name.upcase
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
it 'binds the callback to creation' do
|
192
|
-
expect(FactoryGirl.create(:user, name: 'John Doe').name).to eq 'JOHN DOE'
|
193
|
-
end
|
194
|
-
|
195
|
-
it 'does not bind the callback to building' do
|
196
|
-
expect(FactoryGirl.build(:user, name: 'John Doe').name).to eq 'John Doe'
|
197
|
-
end
|
198
|
-
|
199
|
-
it 'binds the callback to stubbing' do
|
200
|
-
expect(FactoryGirl.build_stubbed(:user, name: 'John Doe').name).to eq 'JOHN DOE'
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
describe 'global callbacks' do
|
205
|
-
include FactoryGirl::Syntax::Methods
|
206
|
-
|
207
|
-
before do
|
208
|
-
define_model('User', name: :string)
|
209
|
-
define_model('Company', name: :string)
|
210
|
-
|
211
|
-
FactoryGirl.define do
|
212
|
-
after :build do |object|
|
213
|
-
object.name = case object.class.to_s
|
214
|
-
when 'User' then 'John Doe'
|
215
|
-
when 'Company' then 'Acme Suppliers'
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
after :create do |object|
|
220
|
-
object.name = "#{object.name}!!!"
|
221
|
-
end
|
222
|
-
|
223
|
-
trait :awesome do
|
224
|
-
after :build do |object|
|
225
|
-
object.name = "___#{object.name}___"
|
226
|
-
end
|
227
|
-
|
228
|
-
after :create do |object|
|
229
|
-
object.name = "A#{object.name}Z"
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
factory :user do
|
234
|
-
after :build do |user|
|
235
|
-
user.name = user.name.downcase
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
factory :company do
|
240
|
-
after :build do |company|
|
241
|
-
company.name = company.name.upcase
|
242
|
-
end
|
243
|
-
end
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
it 'triggers after build callbacks for all factories' do
|
248
|
-
expect(build(:user).name).to eq 'john doe'
|
249
|
-
expect(create(:user).name).to eq 'john doe!!!'
|
250
|
-
expect(create(:user, :awesome).name).to eq 'A___john doe___!!!Z'
|
251
|
-
expect(build(:company).name).to eq 'ACME SUPPLIERS'
|
252
|
-
end
|
253
|
-
end
|