threedaymonk-factory_girl 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +19 -0
- data/LICENSE +19 -0
- data/README.textile +155 -0
- data/Rakefile +69 -0
- data/lib/factory_girl.rb +16 -0
- data/lib/factory_girl/aliases.rb +37 -0
- data/lib/factory_girl/attribute.rb +38 -0
- data/lib/factory_girl/attribute_proxy.rb +92 -0
- data/lib/factory_girl/factory.rb +260 -0
- data/lib/factory_girl/sequence.rb +56 -0
- data/test/aliases_test.rb +29 -0
- data/test/attribute_proxy_test.rb +101 -0
- data/test/attribute_test.rb +70 -0
- data/test/factory_test.rb +454 -0
- data/test/integration_test.rb +147 -0
- data/test/models.rb +32 -0
- data/test/sequence_test.rb +76 -0
- data/test/test_helper.rb +10 -0
- metadata +88 -0
@@ -0,0 +1,260 @@
|
|
1
|
+
class Factory
|
2
|
+
|
3
|
+
cattr_accessor :factories #:nodoc:
|
4
|
+
self.factories = {}
|
5
|
+
|
6
|
+
# An Array of strings specifying locations that should be searched for
|
7
|
+
# factory definitions. By default, factory_girl will attempt to require
|
8
|
+
# "factories," "test/factories," and "spec/factories." Only the first
|
9
|
+
# existing file will be loaded.
|
10
|
+
cattr_accessor :definition_file_paths
|
11
|
+
self.definition_file_paths = %w(factories test/factories spec/factories)
|
12
|
+
|
13
|
+
attr_reader :factory_name
|
14
|
+
|
15
|
+
# Defines a new factory that can be used by the build strategies (create and
|
16
|
+
# build) to build new objects.
|
17
|
+
#
|
18
|
+
# Arguments:
|
19
|
+
# name: (Symbol)
|
20
|
+
# A unique name used to identify this factory.
|
21
|
+
# options: (Hash)
|
22
|
+
# class: the class that will be used when generating instances for this
|
23
|
+
# factory. If not specified, the class will be guessed from the
|
24
|
+
# factory name.
|
25
|
+
#
|
26
|
+
# Yields:
|
27
|
+
# The newly created factory (Factory)
|
28
|
+
def self.define (name, options = {})
|
29
|
+
instance = Factory.new(name, options)
|
30
|
+
yield(instance)
|
31
|
+
self.factories[instance.factory_name] = instance
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_class #:nodoc:
|
35
|
+
@build_class ||= class_for(@options[:class] || factory_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize (name, options = {}) #:nodoc:
|
39
|
+
options.assert_valid_keys(:class)
|
40
|
+
@factory_name = factory_name_for(name)
|
41
|
+
@options = options
|
42
|
+
@attributes = []
|
43
|
+
end
|
44
|
+
|
45
|
+
# Adds an attribute that should be assigned on generated instances for this
|
46
|
+
# factory.
|
47
|
+
#
|
48
|
+
# This method should be called with either a value or block, but not both. If
|
49
|
+
# called with a block, the attribute will be generated "lazily," whenever an
|
50
|
+
# instance is generated. Lazy attribute blocks will not be called if that
|
51
|
+
# attribute is overriden for a specific instance.
|
52
|
+
#
|
53
|
+
# When defining lazy attributes, an instance of Factory::AttributeProxy will
|
54
|
+
# be yielded, allowing associations to be built using the correct build
|
55
|
+
# strategy.
|
56
|
+
#
|
57
|
+
# Arguments:
|
58
|
+
# name: (Symbol)
|
59
|
+
# The name of this attribute. This will be assigned using :"#{name}=" for
|
60
|
+
# generated instances.
|
61
|
+
# value: (Object)
|
62
|
+
# If no block is given, this value will be used for this attribute.
|
63
|
+
def add_attribute (name, value = nil, &block)
|
64
|
+
attribute = Attribute.new(name, value, block)
|
65
|
+
|
66
|
+
if attribute_defined?(attribute.name)
|
67
|
+
raise AttributeDefinitionError, "Attribute already defined: #{name}"
|
68
|
+
end
|
69
|
+
|
70
|
+
@attributes << attribute
|
71
|
+
end
|
72
|
+
|
73
|
+
# Adds an anonymous sequence for an attribute:
|
74
|
+
#
|
75
|
+
# Factory.define :user do |f|
|
76
|
+
# f.sequence do |n|
|
77
|
+
# "Johnny #{n}"
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# When generating an instance, the next value in the sequence will be used.
|
82
|
+
def sequence (name, &proc)
|
83
|
+
sequence = Sequence.new(&proc)
|
84
|
+
add_attribute(name) do
|
85
|
+
sequence.next
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Calls add_attribute using the missing method name as the name of the
|
90
|
+
# attribute, so that:
|
91
|
+
#
|
92
|
+
# Factory.define :user do |f|
|
93
|
+
# f.name 'Billy Idol'
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# and:
|
97
|
+
#
|
98
|
+
# Factory.define :user do |f|
|
99
|
+
# f.add_attribute :name, 'Billy Idol'
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# are equivilent.
|
103
|
+
def method_missing (name, *args, &block)
|
104
|
+
add_attribute(name, *args, &block)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Adds an attribute that builds an association. The associated instance will
|
108
|
+
# be built using the same build strategy as the parent instance.
|
109
|
+
#
|
110
|
+
# Example:
|
111
|
+
# Factory.define :user do |f|
|
112
|
+
# f.name 'Joey'
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
# Factory.define :post do |f|
|
116
|
+
# f.association :author, :factory => :user
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# Arguments:
|
120
|
+
# name: (Symbol)
|
121
|
+
# The name of this attribute.
|
122
|
+
# options: (Hash)
|
123
|
+
# factory: (Symbol)
|
124
|
+
# The name of the factory to use when building the associated instance.
|
125
|
+
# If no name is given, the name of the attribute is assumed to be the
|
126
|
+
# name of the factory. For example, a "user" association will by
|
127
|
+
# default use the "user" factory.
|
128
|
+
def association (name, options = {})
|
129
|
+
name = name.to_sym
|
130
|
+
options = options.symbolize_keys
|
131
|
+
association_factory = options[:factory] || name
|
132
|
+
|
133
|
+
add_attribute(name) {|a| a.association(association_factory) }
|
134
|
+
end
|
135
|
+
|
136
|
+
def attributes_for (attrs = {}) #:nodoc:
|
137
|
+
build_attributes_hash(attrs, :attributes_for)
|
138
|
+
end
|
139
|
+
|
140
|
+
def build (attrs = {}) #:nodoc:
|
141
|
+
build_instance(attrs, :build)
|
142
|
+
end
|
143
|
+
|
144
|
+
def create (attrs = {}) #:nodoc:
|
145
|
+
instance = build_instance(attrs, :create)
|
146
|
+
instance.save!
|
147
|
+
instance
|
148
|
+
end
|
149
|
+
|
150
|
+
class << self
|
151
|
+
|
152
|
+
# Generates and returns a Hash of attributes from this factory. Attributes
|
153
|
+
# can be individually overridden by passing in a Hash of attribute => value
|
154
|
+
# pairs.
|
155
|
+
#
|
156
|
+
# Arguments:
|
157
|
+
# attrs: (Hash)
|
158
|
+
# Attributes to overwrite for this set.
|
159
|
+
#
|
160
|
+
# Returns:
|
161
|
+
# A set of attributes that can be used to build an instance of the class
|
162
|
+
# this factory generates. (Hash)
|
163
|
+
def attributes_for (name, attrs = {})
|
164
|
+
factory_by_name(name).attributes_for(attrs)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Generates and returns an instance from this factory. Attributes can be
|
168
|
+
# individually overridden by passing in a Hash of attribute => value pairs.
|
169
|
+
#
|
170
|
+
# Arguments:
|
171
|
+
# attrs: (Hash)
|
172
|
+
# See attributes_for
|
173
|
+
#
|
174
|
+
# Returns:
|
175
|
+
# An instance of the class this factory generates, with generated
|
176
|
+
# attributes assigned.
|
177
|
+
def build (name, attrs = {})
|
178
|
+
factory_by_name(name).build(attrs)
|
179
|
+
end
|
180
|
+
|
181
|
+
# Generates, saves, and returns an instance from this factory. Attributes can
|
182
|
+
# be individually overridden by passing in a Hash of attribute => value
|
183
|
+
# pairs.
|
184
|
+
#
|
185
|
+
# If the instance is not valid, an ActiveRecord::Invalid exception will be
|
186
|
+
# raised.
|
187
|
+
#
|
188
|
+
# Arguments:
|
189
|
+
# attrs: (Hash)
|
190
|
+
# See attributes_for
|
191
|
+
#
|
192
|
+
# Returns:
|
193
|
+
# A saved instance of the class this factory generates, with generated
|
194
|
+
# attributes assigned.
|
195
|
+
def create (name, attrs = {})
|
196
|
+
factory_by_name(name).create(attrs)
|
197
|
+
end
|
198
|
+
|
199
|
+
def find_definitions #:nodoc:
|
200
|
+
definition_file_paths.each do |path|
|
201
|
+
begin
|
202
|
+
require(path)
|
203
|
+
break
|
204
|
+
rescue LoadError
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
private
|
210
|
+
|
211
|
+
def factory_by_name (name)
|
212
|
+
factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
private
|
218
|
+
|
219
|
+
def build_attributes_hash (values, strategy)
|
220
|
+
values = values.symbolize_keys
|
221
|
+
passed_keys = values.keys.collect {|key| Factory.aliases_for(key) }.flatten
|
222
|
+
@attributes.each do |attribute|
|
223
|
+
unless passed_keys.include?(attribute.name)
|
224
|
+
proxy = AttributeProxy.new(self, attribute.name, strategy, values)
|
225
|
+
values[attribute.name] = attribute.value(proxy)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
values
|
229
|
+
end
|
230
|
+
|
231
|
+
def build_instance (override, strategy)
|
232
|
+
instance = build_class.new
|
233
|
+
attrs = build_attributes_hash(override, strategy)
|
234
|
+
attrs.each do |attr, value|
|
235
|
+
instance.send(:"#{attr}=", value)
|
236
|
+
end
|
237
|
+
instance
|
238
|
+
end
|
239
|
+
|
240
|
+
def class_for (class_or_to_s)
|
241
|
+
if class_or_to_s.respond_to?(:to_sym)
|
242
|
+
class_or_to_s.to_s.pluralize.classify.constantize
|
243
|
+
else
|
244
|
+
class_or_to_s
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def factory_name_for (class_or_to_s)
|
249
|
+
if class_or_to_s.respond_to?(:to_sym)
|
250
|
+
class_or_to_s.to_sym
|
251
|
+
else
|
252
|
+
class_or_to_s.to_s.underscore.to_sym
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def attribute_defined? (name)
|
257
|
+
!@attributes.detect {|attr| attr.name == name }.nil?
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class Factory
|
2
|
+
|
3
|
+
class Sequence
|
4
|
+
|
5
|
+
def initialize (&proc) #:nodoc:
|
6
|
+
@proc = proc
|
7
|
+
@value = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns the next value for this sequence
|
11
|
+
def next
|
12
|
+
@value += 1
|
13
|
+
@proc.call(@value)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
cattr_accessor :sequences #:nodoc:
|
19
|
+
self.sequences = {}
|
20
|
+
|
21
|
+
# Defines a new sequence that can be used to generate unique values in a specific format.
|
22
|
+
#
|
23
|
+
# Arguments:
|
24
|
+
# name: (Symbol)
|
25
|
+
# A unique name for this sequence. This name will be referenced when
|
26
|
+
# calling next to generate new values from this sequence.
|
27
|
+
# block: (Proc)
|
28
|
+
# The code to generate each value in the sequence. This block will be
|
29
|
+
# called with a unique number each time a value in the sequence is to be
|
30
|
+
# generated. The block should return the generated value for the
|
31
|
+
# sequence.
|
32
|
+
#
|
33
|
+
# Example:
|
34
|
+
#
|
35
|
+
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
|
36
|
+
def self.sequence (name, &block)
|
37
|
+
self.sequences[name] = Sequence.new(&block)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Generates and returns the next value in a sequence.
|
41
|
+
#
|
42
|
+
# Arguments:
|
43
|
+
# name: (Symbol)
|
44
|
+
# The name of the sequence that a value should be generated for.
|
45
|
+
#
|
46
|
+
# Returns:
|
47
|
+
# The next value in the sequence. (Object)
|
48
|
+
def self.next (sequence)
|
49
|
+
unless self.sequences.key?(sequence)
|
50
|
+
raise "No such sequence: #{sequence}"
|
51
|
+
end
|
52
|
+
|
53
|
+
self.sequences[sequence].next
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class AliasesTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "include an attribute as an alias for itself by default" do
|
6
|
+
assert Factory.aliases_for(:test).include?(:test)
|
7
|
+
end
|
8
|
+
|
9
|
+
should "include the root of a foreign key as an alias by default" do
|
10
|
+
assert Factory.aliases_for(:test_id).include?(:test)
|
11
|
+
end
|
12
|
+
|
13
|
+
should "include an attribute's foreign key as an alias by default" do
|
14
|
+
assert Factory.aliases_for(:test).include?(:test_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
context "after adding an alias" do
|
18
|
+
|
19
|
+
setup do
|
20
|
+
Factory.alias(/(.*)_suffix/, '\1')
|
21
|
+
end
|
22
|
+
|
23
|
+
should "return the alias in the aliases list" do
|
24
|
+
assert Factory.aliases_for(:test_suffix).include?(:test)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class AttributeProxyTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "an association proxy" do
|
6
|
+
|
7
|
+
setup do
|
8
|
+
@factory = mock('factory')
|
9
|
+
@attr = :user
|
10
|
+
@attrs = { :first_name => 'John' }
|
11
|
+
@strategy = :create
|
12
|
+
@proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
|
13
|
+
end
|
14
|
+
|
15
|
+
should "have a factory" do
|
16
|
+
assert_equal @factory, @proxy.factory
|
17
|
+
end
|
18
|
+
|
19
|
+
should "have an attribute name" do
|
20
|
+
assert_equal @attr, @proxy.attribute_name
|
21
|
+
end
|
22
|
+
|
23
|
+
should "have a build strategy" do
|
24
|
+
assert_equal @strategy, @proxy.strategy
|
25
|
+
end
|
26
|
+
|
27
|
+
should "have attributes" do
|
28
|
+
assert_equal @attrs, @proxy.current_values
|
29
|
+
end
|
30
|
+
|
31
|
+
context "building an association" do
|
32
|
+
|
33
|
+
setup do
|
34
|
+
@association = mock('built-user')
|
35
|
+
@name = :user
|
36
|
+
@attribs = { :first_name => 'Billy' }
|
37
|
+
|
38
|
+
Factory.stubs(@strategy).returns(@association)
|
39
|
+
end
|
40
|
+
|
41
|
+
should "delegate to the appropriate method on Factory" do
|
42
|
+
Factory.expects(@strategy).with(@name, @attribs).returns(@association)
|
43
|
+
@proxy.association(@name, @attribs)
|
44
|
+
end
|
45
|
+
|
46
|
+
should "return the built association" do
|
47
|
+
assert_equal @association, @proxy.association(@name)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
context "building an association using the attributes_for strategy" do
|
53
|
+
|
54
|
+
setup do
|
55
|
+
@strategy = :attributes_for
|
56
|
+
@proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
|
57
|
+
end
|
58
|
+
|
59
|
+
should "not build the association" do
|
60
|
+
Factory.expects(@strategy).never
|
61
|
+
@proxy.association(:user)
|
62
|
+
end
|
63
|
+
|
64
|
+
should "return nil for the association" do
|
65
|
+
Factory.stubs(@strategy).returns(:user)
|
66
|
+
assert_nil @proxy.association(:user)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
context "fetching the value of an attribute" do
|
72
|
+
|
73
|
+
setup do
|
74
|
+
@attr = :first_name
|
75
|
+
end
|
76
|
+
|
77
|
+
should "return the correct value" do
|
78
|
+
assert_equal @attrs[@attr], @proxy.value_for(@attr)
|
79
|
+
end
|
80
|
+
|
81
|
+
should "call value_for for undefined methods" do
|
82
|
+
assert_equal @attrs[@attr], @proxy.send(@attr)
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
context "fetching the value of an undefined attribute" do
|
88
|
+
|
89
|
+
setup do
|
90
|
+
@attr = :beachball
|
91
|
+
end
|
92
|
+
|
93
|
+
should "raise an ArgumentError" do
|
94
|
+
assert_raise(ArgumentError) { @proxy.value_for(@attr) }
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class AttributeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@proxy = mock('attribute-proxy')
|
7
|
+
end
|
8
|
+
|
9
|
+
context "an attribute" do
|
10
|
+
|
11
|
+
setup do
|
12
|
+
@name = :user
|
13
|
+
@attr = Factory::Attribute.new(@name, 'test', nil)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have a name" do
|
17
|
+
assert_equal @name, @attr.name
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
context "an attribute with a static value" do
|
23
|
+
|
24
|
+
setup do
|
25
|
+
@value = 'test'
|
26
|
+
@attr = Factory::Attribute.new(:user, @value, nil)
|
27
|
+
end
|
28
|
+
|
29
|
+
should "return the value" do
|
30
|
+
assert_equal @value, @attr.value(@proxy)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
context "an attribute with a lazy value" do
|
36
|
+
|
37
|
+
setup do
|
38
|
+
@block = lambda { 'value' }
|
39
|
+
@attr = Factory::Attribute.new(:user, nil, @block)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "call the block to return a value" do
|
43
|
+
assert_equal 'value', @attr.value(@proxy)
|
44
|
+
end
|
45
|
+
|
46
|
+
should "yield the attribute proxy to the block" do
|
47
|
+
@block = lambda {|a| a }
|
48
|
+
@attr = Factory::Attribute.new(:user, nil, @block)
|
49
|
+
assert_equal @proxy, @attr.value(@proxy)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
should "raise an error when defining an attribute writer" do
|
55
|
+
assert_raise Factory::AttributeDefinitionError do
|
56
|
+
Factory::Attribute.new('test=', nil, nil)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
should "not allow attributes to be added with both a value parameter and a block" do
|
61
|
+
assert_raise(Factory::AttributeDefinitionError) do
|
62
|
+
Factory::Attribute.new(:name, 'value', lambda {})
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
should "convert names to symbols" do
|
67
|
+
assert_equal :name, Factory::Attribute.new('name', nil, nil).name
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|