gabrielg-factory_girl 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -148,6 +148,15 @@ Factory.next :email
148
148
  Factory.next :email
149
149
  # => "person2@example.com"</code></pre>
150
150
 
151
+ You can also define an anonymous sequence within a Factory.define block:
152
+
153
+ <pre><code># Defines a sequence for name
154
+ Factory.define :person do |p|
155
+ p.sequence :name do |n|
156
+ "Johnny #{n}"
157
+ end
158
+ end</code></pre>
159
+
151
160
  h2. Callbacks
152
161
 
153
162
  Callbacks can be specified after build and after create. If the block arity is
@@ -79,6 +79,22 @@ class Factory
79
79
  @attributes << attribute
80
80
  end
81
81
 
82
+ # Adds an anonymous sequence for an attribute:
83
+ #
84
+ # Factory.define :user do |f|
85
+ # f.sequence do |n|
86
+ # "Johnny #{n}"
87
+ # end
88
+ # end
89
+ #
90
+ # When generating an instance, the next value in the sequence will be used.
91
+ def sequence (name, &proc)
92
+ sequence = Sequence.new(&proc)
93
+ add_attribute(name) do
94
+ sequence.next
95
+ end
96
+ end
97
+
82
98
  # Calls add_attribute using the missing method name as the name of the
83
99
  # attribute, so that:
84
100
  #
@@ -252,7 +268,7 @@ class Factory
252
268
  vals[attribute.name] = attribute.value(proxy)
253
269
  vals
254
270
  end
255
- inherited_factory ? inherited_factory.attributes_for({}, strategy).merge(attrs) : attrs
271
+ inherited_factory ? inherited_factory.attributes_for(attrs, strategy) : attrs
256
272
  end
257
273
 
258
274
  def build_instance (override, strategy)
@@ -270,7 +286,7 @@ class Factory
270
286
 
271
287
  def class_for (class_or_to_s)
272
288
  if class_or_to_s.respond_to?(:to_sym)
273
- class_or_to_s.to_s.classify.constantize
289
+ class_or_to_s.to_s.pluralize.classify.constantize
274
290
  else
275
291
  class_or_to_s
276
292
  end
data/test/factory_test.rb CHANGED
@@ -213,6 +213,26 @@ class FactoryTest < Test::Unit::TestCase
213
213
 
214
214
  end
215
215
 
216
+ context "when adding a sequence attribute" do
217
+
218
+ setup do
219
+ @attr = :name
220
+ end
221
+
222
+ should "not evaluate the block when the sequence is added" do
223
+ @factory.sequence(@attr){ flunk }
224
+ end
225
+
226
+ should "call next in the sequence when attributes are generated" do
227
+ @factory.sequence(@attr) do |n|
228
+ "sequence #{n}"
229
+ end
230
+ assert_equal "sequence 1", @factory.attributes_for[@attr]
231
+ assert_equal "sequence 2", @factory.attributes_for[@attr]
232
+ end
233
+
234
+ end
235
+
216
236
  context "when adding an attribute with a block" do
217
237
 
218
238
  setup do
metadata CHANGED
@@ -1,20 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gabrielg-factory_girl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Ferris
8
+ - Alex Sharp
9
+ - Paul Battley
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
13
 
12
- date: 2008-09-12 00:00:00 -07:00
14
+ date: 2008-11-20 00:00:00 -08:00
13
15
  default_executable:
14
16
  dependencies: []
15
17
 
16
18
  description: factory_girl provides a framework and DSL for defining and using factories - less error-prone, more explicit, and all-around easier to work with than fixtures.
17
- email: jferris@thoughtbot.com
19
+ email: pbattley@gmail.com
18
20
  executables: []
19
21
 
20
22
  extensions: []