factory_girl 3.3.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/GETTING_STARTED.md +28 -0
- data/Gemfile.lock +1 -1
- data/NEWS +6 -0
- data/gemfiles/3.0.gemfile.lock +1 -1
- data/gemfiles/3.1.gemfile.lock +1 -1
- data/gemfiles/3.2.gemfile.lock +1 -1
- data/lib/factory_girl.rb +8 -2
- data/lib/factory_girl/attribute_assigner.rb +22 -2
- data/lib/factory_girl/configuration.rb +7 -3
- data/lib/factory_girl/decorator.rb +21 -0
- data/lib/factory_girl/decorator/attribute_hash.rb +17 -0
- data/lib/factory_girl/decorator/class_key_hash.rb +27 -0
- data/lib/factory_girl/decorator/disallows_duplicates_registry.rb +13 -0
- data/lib/factory_girl/decorator/invocation_ignorer.rb +9 -0
- data/lib/factory_girl/decorator/invocation_tracker.rb +19 -0
- data/lib/factory_girl/evaluator.rb +3 -7
- data/lib/factory_girl/registry.rb +4 -4
- data/lib/factory_girl/sequence.rb +22 -2
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/global_initialize_with_spec.rb +2 -0
- data/spec/acceptance/initialize_with_spec.rb +88 -0
- data/spec/acceptance/keyed_by_class_spec.rb +22 -0
- data/spec/acceptance/traits_spec.rb +2 -0
- data/spec/factory_girl/disallows_duplicates_registry_spec.rb +4 -29
- data/spec/factory_girl/registry_spec.rb +8 -0
- data/spec/factory_girl/sequence_spec.rb +10 -0
- metadata +10 -3
- data/lib/factory_girl/disallows_duplicates_registry.rb +0 -17
data/GETTING_STARTED.md
CHANGED
@@ -762,6 +762,25 @@ factory :user do
|
|
762
762
|
end
|
763
763
|
```
|
764
764
|
|
765
|
+
You can also access all public attributes within the `initialize_with` block
|
766
|
+
by calling `attributes`:
|
767
|
+
|
768
|
+
```ruby
|
769
|
+
factory :user do
|
770
|
+
ignore do
|
771
|
+
comments_count 5
|
772
|
+
end
|
773
|
+
|
774
|
+
name { Faker::Name.name }
|
775
|
+
|
776
|
+
initialize_with { new(attributes) }
|
777
|
+
end
|
778
|
+
```
|
779
|
+
|
780
|
+
This will build a hash of all attributes to be passed to `new`. It won't
|
781
|
+
include ignored attributes, but everything else defined in the factory will be
|
782
|
+
passed (associations, evalued sequences, etc.)
|
783
|
+
|
765
784
|
You can define `initialize_with` for all factories by including it in the
|
766
785
|
`FactoryGirl.define` block:
|
767
786
|
|
@@ -771,6 +790,15 @@ FactoryGirl.define do
|
|
771
790
|
end
|
772
791
|
```
|
773
792
|
|
793
|
+
When using `initialize_with`, attributes accessed from the `initialize_with`
|
794
|
+
block are assigned a second time to the instance. Duplicate assignment can be
|
795
|
+
disabled by configuring FactoryGirl as such:
|
796
|
+
|
797
|
+
FactoryGirl.duplicate_attribute_assignment_from_initialize_with = false
|
798
|
+
|
799
|
+
This would allow for attributes declared as ignored to not be ignored, since
|
800
|
+
it won't assign them for a second time.
|
801
|
+
|
774
802
|
Custom Strategies
|
775
803
|
-----------------
|
776
804
|
|
data/Gemfile.lock
CHANGED
data/NEWS
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
3.4.0 (June 11, 2012)
|
2
|
+
Sequences support Enumerators
|
3
|
+
Optionally disable duplicate assignment of attributes in initialize_with
|
4
|
+
Make hash of public attributes available in initialize_with
|
5
|
+
Support referring to a factory based on class name
|
6
|
+
|
1
7
|
3.3.0 (May 13, 2012)
|
2
8
|
Allow to_create, skip_create, and initialize_with to be defined globally
|
3
9
|
Allow to_create, skip_create, and initialize_with to be defined within traits
|
data/gemfiles/3.0.gemfile.lock
CHANGED
data/gemfiles/3.1.gemfile.lock
CHANGED
data/gemfiles/3.2.gemfile.lock
CHANGED
data/lib/factory_girl.rb
CHANGED
@@ -12,7 +12,6 @@ require 'factory_girl/strategy/create'
|
|
12
12
|
require 'factory_girl/strategy/attributes_for'
|
13
13
|
require 'factory_girl/strategy/stub'
|
14
14
|
require 'factory_girl/strategy/null'
|
15
|
-
require 'factory_girl/disallows_duplicates_registry'
|
16
15
|
require 'factory_girl/registry'
|
17
16
|
require 'factory_girl/null_factory'
|
18
17
|
require 'factory_girl/null_object'
|
@@ -37,6 +36,12 @@ require 'factory_girl/syntax'
|
|
37
36
|
require 'factory_girl/syntax_runner'
|
38
37
|
require 'factory_girl/find_definitions'
|
39
38
|
require 'factory_girl/reload'
|
39
|
+
require 'factory_girl/decorator'
|
40
|
+
require 'factory_girl/decorator/attribute_hash'
|
41
|
+
require 'factory_girl/decorator/class_key_hash'
|
42
|
+
require 'factory_girl/decorator/disallows_duplicates_registry'
|
43
|
+
require 'factory_girl/decorator/invocation_tracker'
|
44
|
+
require 'factory_girl/decorator/invocation_ignorer'
|
40
45
|
require 'factory_girl/version'
|
41
46
|
|
42
47
|
module FactoryGirl
|
@@ -50,7 +55,8 @@ module FactoryGirl
|
|
50
55
|
|
51
56
|
class << self
|
52
57
|
delegate :factories, :sequences, :traits, :strategies, :callback_names,
|
53
|
-
:to_create, :skip_create, :initialize_with, :constructor,
|
58
|
+
:to_create, :skip_create, :initialize_with, :constructor, :duplicate_attribute_assignment_from_initialize_with,
|
59
|
+
:duplicate_attribute_assignment_from_initialize_with=, to: :configuration
|
54
60
|
end
|
55
61
|
|
56
62
|
def self.register_factory(factory)
|
@@ -30,8 +30,28 @@ module FactoryGirl
|
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
+
def method_tracking_evaluator
|
34
|
+
@method_tracking_evaluator ||= Decorator::AttributeHash.new(invocation_decorator.new(@evaluator), attribute_names_to_assign)
|
35
|
+
end
|
36
|
+
|
37
|
+
def invocation_decorator
|
38
|
+
if FactoryGirl.duplicate_attribute_assignment_from_initialize_with
|
39
|
+
Decorator::InvocationIgnorer
|
40
|
+
else
|
41
|
+
Decorator::InvocationTracker
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def methods_invoked_on_evaluator
|
46
|
+
method_tracking_evaluator.__invoked_methods__
|
47
|
+
end
|
48
|
+
|
33
49
|
def build_class_instance
|
34
|
-
@build_class_instance ||=
|
50
|
+
@build_class_instance ||= method_tracking_evaluator.instance_exec(&@instance_builder).tap do
|
51
|
+
if @instance_builder != FactoryGirl.constructor && FactoryGirl.duplicate_attribute_assignment_from_initialize_with
|
52
|
+
ActiveSupport::Deprecation.warn 'Accessing attributes from initialize_with when duplicate assignment is enabled is deprecated; use FactoryGirl.duplicate_attribute_assignment_from_initialize_with = false.', caller
|
53
|
+
end
|
54
|
+
end
|
35
55
|
end
|
36
56
|
|
37
57
|
def build_hash
|
@@ -43,7 +63,7 @@ module FactoryGirl
|
|
43
63
|
end
|
44
64
|
|
45
65
|
def attributes_to_set_on_instance
|
46
|
-
(attribute_names_to_assign - @attribute_names_assigned).uniq
|
66
|
+
(attribute_names_to_assign - @attribute_names_assigned - methods_invoked_on_evaluator).uniq
|
47
67
|
end
|
48
68
|
|
49
69
|
def attributes_to_set_on_hash
|
@@ -3,14 +3,18 @@ module FactoryGirl
|
|
3
3
|
class Configuration
|
4
4
|
attr_reader :factories, :sequences, :traits, :strategies, :callback_names
|
5
5
|
|
6
|
+
attr_accessor :duplicate_attribute_assignment_from_initialize_with
|
7
|
+
|
6
8
|
def initialize
|
7
|
-
@factories = DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
|
8
|
-
@sequences = DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
|
9
|
-
@traits = DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
|
9
|
+
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
|
10
|
+
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
|
11
|
+
@traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
|
10
12
|
@strategies = Registry.new('Strategy')
|
11
13
|
@callback_names = Set.new
|
12
14
|
@definition = Definition.new
|
13
15
|
|
16
|
+
@duplicate_attribute_assignment_from_initialize_with = true
|
17
|
+
|
14
18
|
to_create {|instance| instance.save! }
|
15
19
|
initialize_with { new }
|
16
20
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Decorator < BasicObject
|
3
|
+
undef_method :==
|
4
|
+
|
5
|
+
def initialize(component)
|
6
|
+
@component = component
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(name, *args, &block)
|
10
|
+
@component.send(name, *args, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def send(symbol, *args)
|
14
|
+
__send__(symbol, *args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.const_missing(name)
|
18
|
+
::Object.const_get(name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Decorator
|
3
|
+
class AttributeHash < Decorator
|
4
|
+
def initialize(component, attributes = [])
|
5
|
+
super(component)
|
6
|
+
@attributes = attributes
|
7
|
+
end
|
8
|
+
|
9
|
+
def attributes
|
10
|
+
@attributes.inject({}) do |result, attribute_name|
|
11
|
+
result[attribute_name] = send(attribute_name)
|
12
|
+
result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Decorator
|
3
|
+
class ClassKeyHash < Decorator
|
4
|
+
def [](key)
|
5
|
+
@component[symbolized_key key]
|
6
|
+
end
|
7
|
+
|
8
|
+
def []=(key, value)
|
9
|
+
@component[symbolized_key key] = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def key?(key)
|
13
|
+
@component.key? symbolized_key(key)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def symbolized_key(key)
|
19
|
+
if key.respond_to?(:to_sym)
|
20
|
+
key.to_sym
|
21
|
+
else
|
22
|
+
key.to_s.underscore.to_sym
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Decorator
|
3
|
+
class DisallowsDuplicatesRegistry < Decorator
|
4
|
+
def register(name, item)
|
5
|
+
if registered?(name)
|
6
|
+
raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}"
|
7
|
+
else
|
8
|
+
@component.register(name, item)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class Decorator
|
3
|
+
class InvocationTracker < Decorator
|
4
|
+
def initialize(component)
|
5
|
+
super
|
6
|
+
@invoked_methods = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(name, *args, &block)
|
10
|
+
@invoked_methods << name
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def __invoked_methods__
|
15
|
+
@invoked_methods.uniq
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -35,14 +35,10 @@ module FactoryGirl
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def method_missing(method_name, *args, &block)
|
38
|
-
if @
|
39
|
-
@
|
38
|
+
if @instance.respond_to?(method_name)
|
39
|
+
@instance.send(method_name, *args, &block)
|
40
40
|
else
|
41
|
-
|
42
|
-
@instance.send(method_name, *args, &block)
|
43
|
-
else
|
44
|
-
SyntaxRunner.new.send(method_name, *args, &block)
|
45
|
-
end
|
41
|
+
SyntaxRunner.new.send(method_name, *args, &block)
|
46
42
|
end
|
47
43
|
end
|
48
44
|
|
@@ -6,7 +6,7 @@ module FactoryGirl
|
|
6
6
|
|
7
7
|
def initialize(name)
|
8
8
|
@name = name
|
9
|
-
@items = {}
|
9
|
+
@items = Decorator::ClassKeyHash.new({})
|
10
10
|
end
|
11
11
|
|
12
12
|
def clear
|
@@ -19,7 +19,7 @@ module FactoryGirl
|
|
19
19
|
|
20
20
|
def find(name)
|
21
21
|
if registered?(name)
|
22
|
-
@items[name
|
22
|
+
@items[name]
|
23
23
|
else
|
24
24
|
raise ArgumentError, "#{@name} not registered: #{name}"
|
25
25
|
end
|
@@ -28,11 +28,11 @@ module FactoryGirl
|
|
28
28
|
alias :[] :find
|
29
29
|
|
30
30
|
def register(name, item)
|
31
|
-
@items[name
|
31
|
+
@items[name] = item
|
32
32
|
end
|
33
33
|
|
34
34
|
def registered?(name)
|
35
|
-
@items.key?(name
|
35
|
+
@items.key?(name)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
@@ -13,16 +13,36 @@ module FactoryGirl
|
|
13
13
|
options = args.extract_options!
|
14
14
|
@value = args.first || 1
|
15
15
|
@aliases = options.fetch(:aliases) { [] }
|
16
|
+
|
17
|
+
if !@value.respond_to?(:peek)
|
18
|
+
@value = EnumeratorAdapter.new(@value)
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
def next
|
19
|
-
@proc ? @proc.call(@value) : @value
|
23
|
+
@proc ? @proc.call(@value.peek) : @value.peek
|
20
24
|
ensure
|
21
|
-
@value
|
25
|
+
@value.next
|
22
26
|
end
|
23
27
|
|
24
28
|
def names
|
25
29
|
[@name] + @aliases
|
26
30
|
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
class EnumeratorAdapter
|
35
|
+
def initialize(value)
|
36
|
+
@value = value
|
37
|
+
end
|
38
|
+
|
39
|
+
def peek
|
40
|
+
@value
|
41
|
+
end
|
42
|
+
|
43
|
+
def next
|
44
|
+
@value = @value.next
|
45
|
+
end
|
46
|
+
end
|
27
47
|
end
|
28
48
|
end
|
data/lib/factory_girl/version.rb
CHANGED
@@ -4,6 +4,8 @@ describe "initialize_with with non-FG attributes" do
|
|
4
4
|
include FactoryGirl::Syntax::Methods
|
5
5
|
|
6
6
|
before do
|
7
|
+
ActiveSupport::Deprecation.silenced = true
|
8
|
+
|
7
9
|
define_model("User", name: :string, age: :integer) do
|
8
10
|
def self.construct(name, age)
|
9
11
|
new(name: name, age: age)
|
@@ -26,6 +28,8 @@ describe "initialize_with with FG attributes that are ignored" do
|
|
26
28
|
include FactoryGirl::Syntax::Methods
|
27
29
|
|
28
30
|
before do
|
31
|
+
ActiveSupport::Deprecation.silenced = true
|
32
|
+
|
29
33
|
define_model("User", name: :string) do
|
30
34
|
def self.construct(name)
|
31
35
|
new(name: "#{name} from .construct")
|
@@ -51,6 +55,8 @@ describe "initialize_with with FG attributes that are not ignored" do
|
|
51
55
|
include FactoryGirl::Syntax::Methods
|
52
56
|
|
53
57
|
before do
|
58
|
+
ActiveSupport::Deprecation.silenced = true
|
59
|
+
|
54
60
|
define_model("User", name: :string) do
|
55
61
|
def self.construct(name)
|
56
62
|
new(name: "#{name} from .construct")
|
@@ -75,6 +81,8 @@ describe "initialize_with non-ORM-backed objects" do
|
|
75
81
|
include FactoryGirl::Syntax::Methods
|
76
82
|
|
77
83
|
before do
|
84
|
+
ActiveSupport::Deprecation.silenced = true
|
85
|
+
|
78
86
|
define_class("ReportGenerator") do
|
79
87
|
attr_reader :name, :data
|
80
88
|
|
@@ -108,6 +116,8 @@ end
|
|
108
116
|
|
109
117
|
describe "initialize_with parent and child factories" do
|
110
118
|
before do
|
119
|
+
ActiveSupport::Deprecation.silenced = true
|
120
|
+
|
111
121
|
define_class("Awesome") do
|
112
122
|
attr_reader :name
|
113
123
|
|
@@ -148,6 +158,8 @@ end
|
|
148
158
|
|
149
159
|
describe "initialize_with implicit constructor" do
|
150
160
|
before do
|
161
|
+
ActiveSupport::Deprecation.silenced = true
|
162
|
+
|
151
163
|
define_class("Awesome") do
|
152
164
|
attr_reader :name
|
153
165
|
|
@@ -171,3 +183,79 @@ describe "initialize_with implicit constructor" do
|
|
171
183
|
FactoryGirl.build(:awesome, name: "Awesome name").name.should == "Awesome name"
|
172
184
|
end
|
173
185
|
end
|
186
|
+
|
187
|
+
describe "initialize_with doesn't duplicate assignment on attributes accessed from initialize_with" do
|
188
|
+
before do
|
189
|
+
ActiveSupport::Deprecation.silenced = true
|
190
|
+
|
191
|
+
define_class("User") do
|
192
|
+
attr_reader :name
|
193
|
+
attr_accessor :email
|
194
|
+
|
195
|
+
def initialize(name)
|
196
|
+
@name = name
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
FactoryGirl.define do
|
201
|
+
sequence(:email) {|n| "person#{n}@example.com" }
|
202
|
+
|
203
|
+
factory :user do
|
204
|
+
email
|
205
|
+
|
206
|
+
name { email.gsub(/\@.+/, "") }
|
207
|
+
|
208
|
+
initialize_with { new(name) }
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
it "instantiates the correct object" do
|
214
|
+
FactoryGirl.duplicate_attribute_assignment_from_initialize_with = false
|
215
|
+
|
216
|
+
built_user = FactoryGirl.build(:user)
|
217
|
+
built_user.name.should == "person1"
|
218
|
+
built_user.email.should == "person1@example.com"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "initialize_with has access to all attributes for construction" do
|
223
|
+
before do
|
224
|
+
ActiveSupport::Deprecation.silenced = true
|
225
|
+
|
226
|
+
define_class("User") do
|
227
|
+
attr_reader :name, :email, :ignored
|
228
|
+
|
229
|
+
def initialize(attributes = {})
|
230
|
+
@name = attributes[:name]
|
231
|
+
@email = attributes[:email]
|
232
|
+
@ignored = attributes[:ignored]
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
FactoryGirl.define do
|
237
|
+
sequence(:email) {|n| "person#{n}@example.com" }
|
238
|
+
|
239
|
+
factory :user do
|
240
|
+
ignore do
|
241
|
+
ignored "of course!"
|
242
|
+
end
|
243
|
+
|
244
|
+
email
|
245
|
+
|
246
|
+
name { email.gsub(/\@.+/, "") }
|
247
|
+
|
248
|
+
initialize_with { new(attributes) }
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
it "assigns attributes correctly" do
|
254
|
+
FactoryGirl.duplicate_attribute_assignment_from_initialize_with = false
|
255
|
+
|
256
|
+
user_with_attributes = FactoryGirl.build(:user)
|
257
|
+
user_with_attributes.email.should == "person1@example.com"
|
258
|
+
user_with_attributes.name.should == "person1"
|
259
|
+
user_with_attributes.ignored.should be_nil
|
260
|
+
end
|
261
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'finding factories keyed by class instead of symbol' do
|
4
|
+
before do
|
5
|
+
define_model("User") do
|
6
|
+
attr_accessor :name, :email
|
7
|
+
end
|
8
|
+
|
9
|
+
FactoryGirl.define do
|
10
|
+
factory :user do
|
11
|
+
name 'John Doe'
|
12
|
+
sequence(:email) {|n| "person#{n}@example.com" }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'allows interaction based on class name' do
|
18
|
+
user = FactoryGirl.create User, email: 'person@example.com'
|
19
|
+
user.email.should == 'person@example.com'
|
20
|
+
user.name.should == 'John Doe'
|
21
|
+
end
|
22
|
+
end
|
@@ -1,35 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe FactoryGirl::DisallowsDuplicatesRegistry do
|
4
|
-
let(:registry) { stub("registry", name:
|
3
|
+
describe FactoryGirl::Decorator::DisallowsDuplicatesRegistry do
|
4
|
+
let(:registry) { stub("registry", name: 'Great thing', register: true) }
|
5
5
|
|
6
|
-
subject {
|
7
|
-
|
8
|
-
it "delegates #each to the registry" do
|
9
|
-
block = -> {}
|
10
|
-
subject.each(block)
|
11
|
-
registry.should have_received(:each).with(block)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "delegates #registered? to the registry" do
|
15
|
-
subject.registered?(:great_name)
|
16
|
-
registry.should have_received(:registered?).with(:great_name)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "delegates #clear to the registry" do
|
20
|
-
subject.clear
|
21
|
-
registry.should have_received(:clear)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "delegates #find to the registry" do
|
25
|
-
subject.find(:awesome)
|
26
|
-
registry.should have_received(:find).with(:awesome)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "delegates #[] to the registry" do
|
30
|
-
subject[:awesome]
|
31
|
-
registry.should have_received(:[]).with(:awesome)
|
32
|
-
end
|
6
|
+
subject { described_class.new(registry) }
|
33
7
|
|
34
8
|
it "delegates #register to the registry when not registered" do
|
35
9
|
registry.stubs(registered?: false)
|
@@ -38,6 +12,7 @@ describe FactoryGirl::DisallowsDuplicatesRegistry do
|
|
38
12
|
end
|
39
13
|
|
40
14
|
it "raises when attempting to #register a previously registered strategy" do
|
15
|
+
registry.stubs(registered?: true)
|
41
16
|
expect { subject.register(:same_name, {}) }.
|
42
17
|
to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
|
43
18
|
end
|
@@ -57,4 +57,12 @@ describe FactoryGirl::Registry do
|
|
57
57
|
subject.clear
|
58
58
|
subject.count.should be_zero
|
59
59
|
end
|
60
|
+
|
61
|
+
it "registers classes" do
|
62
|
+
define_class("User")
|
63
|
+
subject.register(User, registered_object)
|
64
|
+
subject.to_a.should == [registered_object]
|
65
|
+
subject.find(:user).should == registered_object
|
66
|
+
subject.find(User).should == registered_object
|
67
|
+
end
|
60
68
|
end
|
@@ -65,4 +65,14 @@ describe FactoryGirl::Sequence do
|
|
65
65
|
its(:next) { should == "B" }
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
describe "iterating over items in an enumerator" do
|
70
|
+
subject { FactoryGirl::Sequence.new(:name, %w[foo bar].to_enum) {|n| "=#{n}" } }
|
71
|
+
|
72
|
+
it "navigates to the next items until no items remain" do
|
73
|
+
subject.next.should == "=foo"
|
74
|
+
subject.next.should == "=bar"
|
75
|
+
expect { subject.next }.to raise_error(StopIteration)
|
76
|
+
end
|
77
|
+
end
|
68
78
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_girl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-06-11 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -261,10 +261,15 @@ files:
|
|
261
261
|
- lib/factory_girl/declaration/implicit.rb
|
262
262
|
- lib/factory_girl/declaration/static.rb
|
263
263
|
- lib/factory_girl/declaration_list.rb
|
264
|
+
- lib/factory_girl/decorator.rb
|
265
|
+
- lib/factory_girl/decorator/attribute_hash.rb
|
266
|
+
- lib/factory_girl/decorator/class_key_hash.rb
|
267
|
+
- lib/factory_girl/decorator/disallows_duplicates_registry.rb
|
268
|
+
- lib/factory_girl/decorator/invocation_ignorer.rb
|
269
|
+
- lib/factory_girl/decorator/invocation_tracker.rb
|
264
270
|
- lib/factory_girl/definition.rb
|
265
271
|
- lib/factory_girl/definition_list.rb
|
266
272
|
- lib/factory_girl/definition_proxy.rb
|
267
|
-
- lib/factory_girl/disallows_duplicates_registry.rb
|
268
273
|
- lib/factory_girl/errors.rb
|
269
274
|
- lib/factory_girl/evaluation.rb
|
270
275
|
- lib/factory_girl/evaluator.rb
|
@@ -315,6 +320,7 @@ files:
|
|
315
320
|
- spec/acceptance/global_initialize_with_spec.rb
|
316
321
|
- spec/acceptance/global_to_create_spec.rb
|
317
322
|
- spec/acceptance/initialize_with_spec.rb
|
323
|
+
- spec/acceptance/keyed_by_class_spec.rb
|
318
324
|
- spec/acceptance/modify_factories_spec.rb
|
319
325
|
- spec/acceptance/modify_inherited_spec.rb
|
320
326
|
- spec/acceptance/nested_attributes_spec.rb
|
@@ -423,6 +429,7 @@ test_files:
|
|
423
429
|
- spec/acceptance/global_initialize_with_spec.rb
|
424
430
|
- spec/acceptance/global_to_create_spec.rb
|
425
431
|
- spec/acceptance/initialize_with_spec.rb
|
432
|
+
- spec/acceptance/keyed_by_class_spec.rb
|
426
433
|
- spec/acceptance/modify_factories_spec.rb
|
427
434
|
- spec/acceptance/modify_inherited_spec.rb
|
428
435
|
- spec/acceptance/nested_attributes_spec.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module FactoryGirl
|
2
|
-
class DisallowsDuplicatesRegistry
|
3
|
-
def initialize(component)
|
4
|
-
@component = component
|
5
|
-
end
|
6
|
-
|
7
|
-
delegate :clear, :each, :find, :[], :registered?, to: :@component
|
8
|
-
|
9
|
-
def register(name, item)
|
10
|
-
if registered?(name)
|
11
|
-
raise DuplicateDefinitionError, "#{@component.name} already registered: #{name}"
|
12
|
-
else
|
13
|
-
@component.register(name, item)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|