factory_girl 4.2.0 → 4.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +29 -7
- data/.yardopts +1 -1
- data/Appraisals +12 -8
- data/CONTRIBUTING.md +60 -0
- data/GETTING_STARTED.md +473 -191
- data/Gemfile +6 -3
- data/Gemfile.lock +93 -65
- data/LICENSE +1 -1
- data/NAME.md +11 -0
- data/NEWS +43 -1
- data/README.md +52 -28
- data/Rakefile +1 -3
- data/factory_girl.gemspec +9 -17
- data/gemfiles/3.2.gemfile +6 -3
- data/gemfiles/3.2.gemfile.lock +85 -62
- data/gemfiles/4.0.gemfile +6 -3
- data/gemfiles/4.0.gemfile.lock +84 -74
- data/gemfiles/4.1.gemfile +10 -0
- data/gemfiles/4.1.gemfile.lock +104 -0
- data/gemfiles/4.2.gemfile +10 -0
- data/gemfiles/4.2.gemfile.lock +104 -0
- data/gemfiles/5.0.gemfile +10 -0
- data/gemfiles/5.0.gemfile.lock +103 -0
- data/lib/factory_girl/attribute/dynamic.rb +4 -1
- data/lib/factory_girl/attribute_assigner.rb +4 -4
- data/lib/factory_girl/attribute_list.rb +1 -1
- data/lib/factory_girl/callback.rb +1 -1
- data/lib/factory_girl/callbacks_observer.rb +1 -1
- data/lib/factory_girl/configuration.rb +7 -2
- data/lib/factory_girl/declaration_list.rb +2 -2
- data/lib/factory_girl/decorator/attribute_hash.rb +1 -2
- data/lib/factory_girl/decorator/class_key_hash.rb +2 -1
- data/lib/factory_girl/decorator.rb +2 -2
- data/lib/factory_girl/definition.rb +25 -15
- data/lib/factory_girl/definition_hierarchy.rb +1 -1
- data/lib/factory_girl/definition_proxy.rb +15 -17
- data/lib/factory_girl/errors.rb +6 -0
- data/lib/factory_girl/evaluator.rb +7 -1
- data/lib/factory_girl/factory.rb +3 -3
- data/lib/factory_girl/factory_runner.rb +7 -1
- data/lib/factory_girl/find_definitions.rb +3 -3
- data/lib/factory_girl/linter.rb +97 -0
- data/lib/factory_girl/strategy/stub.rb +58 -20
- data/lib/factory_girl/strategy_syntax_method_registrar.rb +17 -0
- data/lib/factory_girl/syntax/default.rb +8 -0
- data/lib/factory_girl/syntax/methods.rb +18 -2
- data/lib/factory_girl/version.rb +1 -1
- data/lib/factory_girl.rb +30 -3
- metadata +65 -249
- data/CONTRIBUTION_GUIDELINES.md +0 -10
- 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.0.gemfile +0 -7
- data/gemfiles/3.0.gemfile.lock +0 -81
- data/gemfiles/3.1.gemfile +0 -7
- data/gemfiles/3.1.gemfile.lock +0 -82
- 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 -177
- data/spec/acceptance/create_list_spec.rb +0 -97
- data/spec/acceptance/create_spec.rb +0 -116
- data/spec/acceptance/define_child_before_parent_spec.rb +0 -21
- 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/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/register_strategies_spec.rb +0 -128
- 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 -41
- data/spec/acceptance/traits_spec.rb +0 -727
- data/spec/acceptance/transient_attributes_spec.rb +0 -124
- 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 -58
- 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 -9
- data/spec/support/shared_examples/strategy.rb +0 -90
@@ -1,215 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::DefinitionProxy, "#add_attribute" do
|
4
|
-
subject { FactoryGirl::Definition.new }
|
5
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
6
|
-
|
7
|
-
it "raises if both a block and value are given" do
|
8
|
-
expect {
|
9
|
-
proxy.add_attribute(:something, "great") { "will raise!" }
|
10
|
-
}.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
|
11
|
-
end
|
12
|
-
|
13
|
-
it "declares a static attribute on the factory" do
|
14
|
-
proxy.add_attribute(:attribute_name, "attribute value")
|
15
|
-
expect(subject).to have_static_declaration(:attribute_name).with_value("attribute value")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "declares a dynamic attribute on the factory" do
|
19
|
-
attribute_value = -> { "dynamic attribute" }
|
20
|
-
proxy.add_attribute(:attribute_name, &attribute_value)
|
21
|
-
expect(subject).to have_dynamic_declaration(:attribute_name).with_value(attribute_value)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe FactoryGirl::DefinitionProxy, "#add_attribute when the proxy ignores attributes" do
|
26
|
-
subject { FactoryGirl::Definition.new }
|
27
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject, true) }
|
28
|
-
|
29
|
-
it "raises if both a block and value are given" do
|
30
|
-
expect {
|
31
|
-
proxy.add_attribute(:something, "great") { "will raise!" }
|
32
|
-
}.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
|
33
|
-
end
|
34
|
-
|
35
|
-
it "declares a static attribute on the factory" do
|
36
|
-
proxy.add_attribute(:attribute_name, "attribute value")
|
37
|
-
expect(subject).to have_static_declaration(:attribute_name).ignored.with_value("attribute value")
|
38
|
-
end
|
39
|
-
|
40
|
-
it "declares a dynamic attribute on the factory" do
|
41
|
-
attribute_value = -> { "dynamic attribute" }
|
42
|
-
proxy.add_attribute(:attribute_name, &attribute_value)
|
43
|
-
expect(subject).to have_dynamic_declaration(:attribute_name).ignored.with_value(attribute_value)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe FactoryGirl::DefinitionProxy, "#ignore" do
|
48
|
-
subject { FactoryGirl::Definition.new }
|
49
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
50
|
-
|
51
|
-
it "makes all attributes added ignored" do
|
52
|
-
proxy.ignore do
|
53
|
-
add_attribute(:attribute_name, "attribute value")
|
54
|
-
end
|
55
|
-
|
56
|
-
expect(subject).to have_static_declaration(:attribute_name).ignored.with_value("attribute value")
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe FactoryGirl::DefinitionProxy, "#method_missing" do
|
61
|
-
subject { FactoryGirl::Definition.new }
|
62
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
63
|
-
|
64
|
-
it "declares an implicit declaration without args or a block" do
|
65
|
-
proxy.bogus
|
66
|
-
expect(subject).to have_implicit_declaration(:bogus).with_factory(subject)
|
67
|
-
end
|
68
|
-
|
69
|
-
it "declares an association when :factory is passed" do
|
70
|
-
proxy.author factory: :user
|
71
|
-
expect(subject).to have_association_declaration(:author).with_options(factory: :user)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "declares a static attribute" do
|
75
|
-
proxy.attribute_name "attribute value"
|
76
|
-
expect(subject).to have_static_declaration(:attribute_name).with_value("attribute value")
|
77
|
-
end
|
78
|
-
|
79
|
-
it "declares a dynamic attribute" do
|
80
|
-
attribute_value = -> { "dynamic attribute" }
|
81
|
-
proxy.attribute_name(&attribute_value)
|
82
|
-
expect(subject).to have_dynamic_declaration(:attribute_name).with_value(attribute_value)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe FactoryGirl::DefinitionProxy, "#sequence" do
|
87
|
-
subject { FactoryGirl::Definition.new }
|
88
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
89
|
-
|
90
|
-
before { FactoryGirl::Sequence.stubs(:new) }
|
91
|
-
|
92
|
-
it "creates a new sequence starting at 1" do
|
93
|
-
proxy.sequence(:great)
|
94
|
-
expect(FactoryGirl::Sequence).to have_received(:new).with(:great)
|
95
|
-
end
|
96
|
-
|
97
|
-
it "creates a new sequence with an overridden starting vaue" do
|
98
|
-
proxy.sequence(:great, "C")
|
99
|
-
expect(FactoryGirl::Sequence).to have_received(:new).with(:great, "C")
|
100
|
-
end
|
101
|
-
|
102
|
-
it "creates a new sequence with a block" do
|
103
|
-
sequence_block = Proc.new {|n| "user+#{n}@example.com" }
|
104
|
-
proxy.sequence(:great, 1, &sequence_block)
|
105
|
-
expect(FactoryGirl::Sequence).to have_received(:new).with(:great, 1, &sequence_block)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
describe FactoryGirl::DefinitionProxy, "#association" do
|
110
|
-
subject { FactoryGirl::Definition.new }
|
111
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
112
|
-
|
113
|
-
it "declares an association" do
|
114
|
-
proxy.association(:association_name)
|
115
|
-
expect(subject).to have_association_declaration(:association_name)
|
116
|
-
end
|
117
|
-
|
118
|
-
it "declares an association with options" do
|
119
|
-
proxy.association(:association_name, { name: "Awesome" })
|
120
|
-
expect(subject).to have_association_declaration(:association_name).with_options(name: "Awesome")
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
describe FactoryGirl::DefinitionProxy, "adding callbacks" do
|
125
|
-
subject { FactoryGirl::Definition.new }
|
126
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
127
|
-
let(:callback) { -> { "my awesome callback!" } }
|
128
|
-
|
129
|
-
context "#after(:build)" do
|
130
|
-
before { proxy.after(:build, &callback) }
|
131
|
-
it { should have_callback(:after_build).with_block(callback) }
|
132
|
-
end
|
133
|
-
|
134
|
-
context "#after(:create)" do
|
135
|
-
before { proxy.after(:create, &callback) }
|
136
|
-
it { should have_callback(:after_create).with_block(callback) }
|
137
|
-
end
|
138
|
-
|
139
|
-
context "#after(:stub)" do
|
140
|
-
before { proxy.after(:stub, &callback) }
|
141
|
-
it { should have_callback(:after_stub).with_block(callback) }
|
142
|
-
end
|
143
|
-
|
144
|
-
context "#after(:stub, :create)" do
|
145
|
-
before { proxy.after(:stub, :create, &callback) }
|
146
|
-
it { should have_callback(:after_stub).with_block(callback) }
|
147
|
-
it { should have_callback(:after_create).with_block(callback) }
|
148
|
-
end
|
149
|
-
|
150
|
-
context "#before(:stub, :create)" do
|
151
|
-
before { proxy.before(:stub, :create, &callback) }
|
152
|
-
it { should have_callback(:before_stub).with_block(callback) }
|
153
|
-
it { should have_callback(:before_create).with_block(callback) }
|
154
|
-
end
|
155
|
-
|
156
|
-
context "#callback(:after_stub, :before_create)" do
|
157
|
-
before { proxy.callback(:after_stub, :before_create, &callback) }
|
158
|
-
it { should have_callback(:after_stub).with_block(callback) }
|
159
|
-
it { should have_callback(:before_create).with_block(callback) }
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
describe FactoryGirl::DefinitionProxy, "#to_create" do
|
164
|
-
subject { FactoryGirl::Definition.new }
|
165
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
166
|
-
|
167
|
-
it "accepts a block to run in place of #save!" do
|
168
|
-
to_create_block = ->(record) { record.persist }
|
169
|
-
proxy.to_create(&to_create_block)
|
170
|
-
expect(subject.to_create).to eq to_create_block
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
describe FactoryGirl::DefinitionProxy, "#factory" do
|
175
|
-
subject { FactoryGirl::Definition.new }
|
176
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
177
|
-
|
178
|
-
it "without options" do
|
179
|
-
proxy.factory(:child)
|
180
|
-
expect(proxy.child_factories).to include([:child, {}, nil])
|
181
|
-
end
|
182
|
-
|
183
|
-
it "with options" do
|
184
|
-
proxy.factory(:child, { awesome: true })
|
185
|
-
expect(proxy.child_factories).to include([:child, { awesome: true }, nil])
|
186
|
-
end
|
187
|
-
|
188
|
-
it "with a block" do
|
189
|
-
child_block = -> { }
|
190
|
-
proxy.factory(:child, {}, &child_block)
|
191
|
-
expect(proxy.child_factories).to include([:child, {}, child_block])
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
describe FactoryGirl::DefinitionProxy, "#trait" do
|
196
|
-
subject { FactoryGirl::Definition.new }
|
197
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
198
|
-
|
199
|
-
it "declares a trait" do
|
200
|
-
male_trait = Proc.new { gender("Male") }
|
201
|
-
proxy.trait(:male, &male_trait)
|
202
|
-
expect(subject).to have_trait(:male).with_block(male_trait)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
describe FactoryGirl::DefinitionProxy, "#initialize_with" do
|
207
|
-
subject { FactoryGirl::Definition.new }
|
208
|
-
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
209
|
-
|
210
|
-
it "defines the constructor on the definition" do
|
211
|
-
constructor = Proc.new { Array.new }
|
212
|
-
proxy.initialize_with(&constructor)
|
213
|
-
expect(subject.constructor).to eq constructor
|
214
|
-
end
|
215
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe FactoryGirl::Definition do
|
4
|
-
it { should delegate(:declare_attribute).to(:declarations) }
|
5
|
-
end
|
6
|
-
|
7
|
-
describe FactoryGirl::Definition, "with a name" do
|
8
|
-
let(:name) { :"great name" }
|
9
|
-
subject { FactoryGirl::Definition.new(name) }
|
10
|
-
|
11
|
-
it "creates a new attribute list with the name passed" do
|
12
|
-
FactoryGirl::DeclarationList.stubs(:new)
|
13
|
-
subject
|
14
|
-
expect(FactoryGirl::DeclarationList).to have_received(:new).with(name)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe FactoryGirl::Definition, "#overridable" do
|
19
|
-
let(:list) { stub("declaration list", overridable: true) }
|
20
|
-
before { FactoryGirl::DeclarationList.stubs(new: list) }
|
21
|
-
|
22
|
-
it "sets the declaration list as overridable" do
|
23
|
-
expect(subject.overridable).to eq subject
|
24
|
-
expect(list).to have_received(:overridable).once
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe FactoryGirl::Definition, "defining traits" do
|
29
|
-
let(:trait_1) { stub("trait") }
|
30
|
-
let(:trait_2) { stub("trait") }
|
31
|
-
|
32
|
-
it "maintains a list of traits" do
|
33
|
-
subject.define_trait(trait_1)
|
34
|
-
subject.define_trait(trait_2)
|
35
|
-
expect(subject.defined_traits).to eq [trait_1, trait_2]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe FactoryGirl::Definition, "adding callbacks" do
|
40
|
-
let(:callback_1) { "callback1" }
|
41
|
-
let(:callback_2) { "callback2" }
|
42
|
-
|
43
|
-
it "maintains a list of callbacks" do
|
44
|
-
subject.add_callback(callback_1)
|
45
|
-
subject.add_callback(callback_2)
|
46
|
-
expect(subject.callbacks).to eq [callback_1, callback_2]
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe FactoryGirl::Definition, "#to_create" do
|
51
|
-
its(:to_create) { should be_nil }
|
52
|
-
|
53
|
-
it "returns the assigned value when given a block" do
|
54
|
-
block = proc { nil }
|
55
|
-
subject.to_create(&block)
|
56
|
-
expect(subject.to_create).to eq block
|
57
|
-
end
|
58
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe FactoryGirl::Decorator::DisallowsDuplicatesRegistry do
|
4
|
-
let(:registry) { stub("registry", name: 'Great thing', register: true) }
|
5
|
-
|
6
|
-
subject { described_class.new(registry) }
|
7
|
-
|
8
|
-
it "delegates #register to the registry when not registered" do
|
9
|
-
registry.stubs(registered?: false)
|
10
|
-
subject.register(:awesome, {})
|
11
|
-
expect(registry).to have_received(:register).with(:awesome, {})
|
12
|
-
end
|
13
|
-
|
14
|
-
it "raises when attempting to #register a previously registered strategy" do
|
15
|
-
registry.stubs(registered?: true)
|
16
|
-
expect { subject.register(:same_name, {}) }.
|
17
|
-
to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
|
18
|
-
end
|
19
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe FactoryGirl::EvaluatorClassDefiner do
|
4
|
-
it "returns an evaluator when accessing the evaluator class" do
|
5
|
-
evaluator = define_evaluator(parent_class: FactoryGirl::Evaluator)
|
6
|
-
|
7
|
-
expect(evaluator).to be_a(FactoryGirl::Evaluator)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "adds each attribute to the evaluator" do
|
11
|
-
attribute = stub_attribute(:attribute) { 1 }
|
12
|
-
evaluator = define_evaluator(attributes: [attribute])
|
13
|
-
|
14
|
-
expect(evaluator.attribute).to eq 1
|
15
|
-
end
|
16
|
-
|
17
|
-
it "evaluates the block in the context of the evaluator" do
|
18
|
-
dependency_attribute = stub("dependency", name: :dependency, to_proc: -> { 1 })
|
19
|
-
dependency_attribute = stub_attribute(:dependency) { 1 }
|
20
|
-
attribute = stub_attribute(:attribute) { dependency + 1 }
|
21
|
-
evaluator = define_evaluator(attributes: [dependency_attribute, attribute])
|
22
|
-
|
23
|
-
expect(evaluator.attribute).to eq 2
|
24
|
-
end
|
25
|
-
|
26
|
-
it "only instance_execs the block once even when returning nil" do
|
27
|
-
count = 0
|
28
|
-
attribute = stub_attribute :attribute do
|
29
|
-
count += 1
|
30
|
-
nil
|
31
|
-
end
|
32
|
-
evaluator = define_evaluator(attributes: [attribute])
|
33
|
-
|
34
|
-
2.times { evaluator.attribute }
|
35
|
-
|
36
|
-
expect(count).to eq 1
|
37
|
-
end
|
38
|
-
|
39
|
-
it "sets attributes on the evaluator class" do
|
40
|
-
attributes = [stub_attribute, stub_attribute]
|
41
|
-
evaluator = define_evaluator(attributes: attributes)
|
42
|
-
|
43
|
-
expect(evaluator.attribute_lists).to eq [attributes]
|
44
|
-
end
|
45
|
-
|
46
|
-
context "with a custom evaluator as a parent class" do
|
47
|
-
it "bases its attribute lists on itself and its parent evaluator" do
|
48
|
-
parent_attributes = [stub_attribute, stub_attribute]
|
49
|
-
parent_evaluator_class = define_evaluator_class(attributes: parent_attributes)
|
50
|
-
child_attributes = [stub_attribute, stub_attribute]
|
51
|
-
child_evaluator = define_evaluator(
|
52
|
-
attributes: child_attributes,
|
53
|
-
parent_class: parent_evaluator_class
|
54
|
-
)
|
55
|
-
|
56
|
-
expect(child_evaluator.attribute_lists).to eq [parent_attributes, child_attributes]
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def define_evaluator(arguments = {})
|
61
|
-
evaluator_class = define_evaluator_class(arguments)
|
62
|
-
evaluator_class.new(FactoryGirl::Strategy::Null)
|
63
|
-
end
|
64
|
-
|
65
|
-
def define_evaluator_class(arguments = {})
|
66
|
-
evaluator_class_definer = FactoryGirl::EvaluatorClassDefiner.new(
|
67
|
-
arguments[:attributes] || [],
|
68
|
-
arguments[:parent_class] || FactoryGirl::Evaluator
|
69
|
-
)
|
70
|
-
evaluator_class_definer.evaluator_class
|
71
|
-
end
|
72
|
-
|
73
|
-
def stub_attribute(name = :attribute, &value)
|
74
|
-
value ||= -> {}
|
75
|
-
stub(name.to_s, name: name.to_sym, to_proc: value)
|
76
|
-
end
|
77
|
-
end
|
@@ -1,259 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe FactoryGirl::Factory do
|
4
|
-
before do
|
5
|
-
@name = :user
|
6
|
-
@class = define_class('User')
|
7
|
-
@factory = FactoryGirl::Factory.new(@name)
|
8
|
-
FactoryGirl.register_factory(@factory)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "has a factory name" do
|
12
|
-
expect(@factory.name).to eq @name
|
13
|
-
end
|
14
|
-
|
15
|
-
it "has a build class" do
|
16
|
-
expect(@factory.build_class).to eq @class
|
17
|
-
end
|
18
|
-
|
19
|
-
it "passes a custom creation block" do
|
20
|
-
strategy = stub("strategy", result: nil, add_observer: true)
|
21
|
-
FactoryGirl::Strategy::Build.stubs(new: strategy)
|
22
|
-
block = -> {}
|
23
|
-
factory = FactoryGirl::Factory.new(:object)
|
24
|
-
factory.to_create(&block)
|
25
|
-
|
26
|
-
factory.run(FactoryGirl::Strategy::Build, {})
|
27
|
-
|
28
|
-
expect(strategy).to have_received(:result).with(instance_of(FactoryGirl::Evaluation))
|
29
|
-
end
|
30
|
-
|
31
|
-
it "returns associations" do
|
32
|
-
factory = FactoryGirl::Factory.new(:post)
|
33
|
-
FactoryGirl.register_factory(FactoryGirl::Factory.new(:admin))
|
34
|
-
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:author, {}))
|
35
|
-
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:editor, {}))
|
36
|
-
factory.declare_attribute(FactoryGirl::Declaration::Implicit.new(:admin, factory))
|
37
|
-
factory.associations.each do |association|
|
38
|
-
expect(association).to be_association
|
39
|
-
end
|
40
|
-
expect(factory.associations.to_a.length).to eq 3
|
41
|
-
end
|
42
|
-
|
43
|
-
it "includes associations from the parent factory" do
|
44
|
-
association_on_parent = FactoryGirl::Declaration::Association.new(:association_on_parent, {})
|
45
|
-
association_on_child = FactoryGirl::Declaration::Association.new(:association_on_child, {})
|
46
|
-
|
47
|
-
factory = FactoryGirl::Factory.new(:post)
|
48
|
-
factory.declare_attribute(association_on_parent)
|
49
|
-
FactoryGirl.register_factory(factory)
|
50
|
-
|
51
|
-
child_factory = FactoryGirl::Factory.new(:child_post, parent: :post)
|
52
|
-
child_factory.declare_attribute(association_on_child)
|
53
|
-
|
54
|
-
expect(child_factory.associations.map(&:name)).to eq [:association_on_parent, :association_on_child]
|
55
|
-
end
|
56
|
-
|
57
|
-
describe "when overriding generated attributes with a hash" do
|
58
|
-
before do
|
59
|
-
@name = :name
|
60
|
-
@value = 'The price is right!'
|
61
|
-
@hash = { @name => @value }
|
62
|
-
end
|
63
|
-
|
64
|
-
it "returns the overridden value in the generated attributes" do
|
65
|
-
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
66
|
-
@factory.declare_attribute(declaration)
|
67
|
-
result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
68
|
-
expect(result[@name]).to eq @value
|
69
|
-
end
|
70
|
-
|
71
|
-
it "does not call a lazy attribute block for an overridden attribute" do
|
72
|
-
declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, -> { flunk })
|
73
|
-
@factory.declare_attribute(declaration)
|
74
|
-
@factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "overrides a symbol parameter with a string parameter" do
|
78
|
-
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
79
|
-
@factory.declare_attribute(declaration)
|
80
|
-
@hash = { @name.to_s => @value }
|
81
|
-
result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
82
|
-
expect(result[@name]).to eq @value
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe "overriding an attribute with an alias" do
|
87
|
-
before do
|
88
|
-
@factory.declare_attribute(FactoryGirl::Declaration::Static.new(:test, 'original'))
|
89
|
-
FactoryGirl.aliases << [/(.*)_alias/, '\1']
|
90
|
-
@result = @factory.run(FactoryGirl::Strategy::AttributesFor,
|
91
|
-
test_alias: 'new')
|
92
|
-
end
|
93
|
-
|
94
|
-
it "uses the passed in value for the alias" do
|
95
|
-
expect(@result[:test_alias]).to eq 'new'
|
96
|
-
end
|
97
|
-
|
98
|
-
it "discards the predefined value for the attribute" do
|
99
|
-
expect(@result[:test]).to be_nil
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
it "guesses the build class from the factory name" do
|
104
|
-
expect(@factory.build_class).to eq User
|
105
|
-
end
|
106
|
-
|
107
|
-
it "creates a new factory using the class of the parent" do
|
108
|
-
child = FactoryGirl::Factory.new(:child, parent: @factory.name)
|
109
|
-
child.compile
|
110
|
-
expect(child.build_class).to eq @factory.build_class
|
111
|
-
end
|
112
|
-
|
113
|
-
it "creates a new factory while overriding the parent class" do
|
114
|
-
child = FactoryGirl::Factory.new(:child, class: String, parent: @factory.name)
|
115
|
-
child.compile
|
116
|
-
expect(child.build_class).to eq String
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
describe FactoryGirl::Factory, "when defined with a custom class" do
|
121
|
-
subject { FactoryGirl::Factory.new(:author, class: Float) }
|
122
|
-
its(:build_class) { should eq Float }
|
123
|
-
end
|
124
|
-
|
125
|
-
describe FactoryGirl::Factory, "when given a class that overrides #to_s" do
|
126
|
-
let(:overriding_class) { Overriding::Class }
|
127
|
-
|
128
|
-
before do
|
129
|
-
define_class("Overriding")
|
130
|
-
define_class("Overriding::Class") do
|
131
|
-
def self.to_s
|
132
|
-
"Overriding"
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
subject { FactoryGirl::Factory.new(:overriding_class, class: Overriding::Class) }
|
138
|
-
|
139
|
-
it "sets build_class correctly" do
|
140
|
-
expect(subject.build_class).to eq overriding_class
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
describe FactoryGirl::Factory, "when defined with a class instead of a name" do
|
145
|
-
let(:factory_class) { ArgumentError }
|
146
|
-
let(:name) { :argument_error }
|
147
|
-
|
148
|
-
subject { FactoryGirl::Factory.new(factory_class) }
|
149
|
-
|
150
|
-
its(:name) { should eq name }
|
151
|
-
its(:build_class) { should eq factory_class }
|
152
|
-
end
|
153
|
-
|
154
|
-
describe FactoryGirl::Factory, "when defined with a custom class name" do
|
155
|
-
subject { FactoryGirl::Factory.new(:author, class: :argument_error) }
|
156
|
-
its(:build_class) { should eq ArgumentError }
|
157
|
-
end
|
158
|
-
|
159
|
-
describe FactoryGirl::Factory, "with a name ending in s" do
|
160
|
-
let(:name) { :business }
|
161
|
-
let(:business_class) { Business }
|
162
|
-
|
163
|
-
before { define_class('Business') }
|
164
|
-
subject { FactoryGirl::Factory.new(name) }
|
165
|
-
|
166
|
-
its(:name) { should eq name }
|
167
|
-
its(:build_class) { should eq business_class }
|
168
|
-
end
|
169
|
-
|
170
|
-
describe FactoryGirl::Factory, "with a string for a name" do
|
171
|
-
let(:name) { :string }
|
172
|
-
subject { FactoryGirl::Factory.new(name.to_s) }
|
173
|
-
its(:name) { should eq name }
|
174
|
-
end
|
175
|
-
|
176
|
-
describe FactoryGirl::Factory, "for namespaced class" do
|
177
|
-
let(:name) { :settings }
|
178
|
-
let(:settings_class) { Admin::Settings }
|
179
|
-
|
180
|
-
before do
|
181
|
-
define_class("Admin")
|
182
|
-
define_class("Admin::Settings")
|
183
|
-
end
|
184
|
-
|
185
|
-
context "with a namespaced class with Namespace::Class syntax" do
|
186
|
-
subject { FactoryGirl::Factory.new(name, class: "Admin::Settings") }
|
187
|
-
|
188
|
-
it "sets build_class correctly" do
|
189
|
-
expect(subject.build_class).to eq settings_class
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
context "with a namespaced class with namespace/class syntax" do
|
194
|
-
subject { FactoryGirl::Factory.new(name, class: "admin/settings") }
|
195
|
-
|
196
|
-
it "sets build_class correctly" do
|
197
|
-
expect(subject.build_class).to eq settings_class
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
describe FactoryGirl::Factory, "human names" do
|
203
|
-
context "factory name without underscores" do
|
204
|
-
subject { FactoryGirl::Factory.new(:user) }
|
205
|
-
its(:names) { should eq [:user] }
|
206
|
-
its(:human_names) { should eq ["user"] }
|
207
|
-
end
|
208
|
-
|
209
|
-
context "factory name with underscores" do
|
210
|
-
subject { FactoryGirl::Factory.new(:happy_user) }
|
211
|
-
its(:names) { should eq [:happy_user] }
|
212
|
-
its(:human_names) { should eq ["happy user"] }
|
213
|
-
end
|
214
|
-
|
215
|
-
context "factory name with big letters" do
|
216
|
-
subject { FactoryGirl::Factory.new(:LoL) }
|
217
|
-
its(:names) { should eq [:LoL] }
|
218
|
-
its(:human_names) { should eq ["lol"] }
|
219
|
-
end
|
220
|
-
|
221
|
-
context "factory name with aliases" do
|
222
|
-
subject { FactoryGirl::Factory.new(:happy_user, aliases: [:gleeful_user, :person]) }
|
223
|
-
its(:names) { should eq [:happy_user, :gleeful_user, :person] }
|
224
|
-
its(:human_names) { should eq ["happy user", "gleeful user", "person"] }
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
describe FactoryGirl::Factory, "running a factory" do
|
229
|
-
subject { FactoryGirl::Factory.new(:user) }
|
230
|
-
let(:attribute) { FactoryGirl::Attribute::Static.new(:name, "value", false) }
|
231
|
-
let(:declaration) { FactoryGirl::Declaration::Static.new(:name, "value", false) }
|
232
|
-
let(:strategy) { stub("strategy", result: "result", add_observer: true) }
|
233
|
-
let(:attributes) { [attribute] }
|
234
|
-
let(:attribute_list) { stub('attribute-list', declarations: [declaration], to_a: attributes) }
|
235
|
-
|
236
|
-
before do
|
237
|
-
define_model("User", name: :string)
|
238
|
-
FactoryGirl::Declaration::Static.stubs(new: declaration)
|
239
|
-
declaration.stubs(to_attributes: attributes)
|
240
|
-
FactoryGirl::Strategy::Build.stubs(new: strategy)
|
241
|
-
subject.declare_attribute(declaration)
|
242
|
-
end
|
243
|
-
|
244
|
-
it "creates the right strategy using the build class when running" do
|
245
|
-
subject.run(FactoryGirl::Strategy::Build, {})
|
246
|
-
expect(FactoryGirl::Strategy::Build).to have_received(:new).once
|
247
|
-
end
|
248
|
-
|
249
|
-
it "returns the result from the strategy when running" do
|
250
|
-
expect(subject.run(FactoryGirl::Strategy::Build, {})).to eq "result"
|
251
|
-
end
|
252
|
-
|
253
|
-
it "calls the block and returns the result" do
|
254
|
-
block_run = nil
|
255
|
-
block = ->(result) { block_run = "changed" }
|
256
|
-
subject.run(FactoryGirl::Strategy::Build, { }, &block)
|
257
|
-
expect(block_run).to eq "changed"
|
258
|
-
end
|
259
|
-
end
|