factory_girl 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +9 -3
- data/lib/factory_girl.rb +1 -0
- data/lib/factory_girl/attribute/association.rb +2 -0
- data/lib/factory_girl/attribute/callback.rb +16 -0
- data/lib/factory_girl/factory.rb +39 -4
- data/lib/factory_girl/proxy.rb +17 -0
- data/lib/factory_girl/proxy/build.rb +1 -0
- data/lib/factory_girl/proxy/create.rb +2 -0
- data/lib/factory_girl/proxy/stub.rb +7 -6
- data/lib/factory_girl/step_definitions.rb +54 -0
- data/spec/factory_girl/attribute/association_spec.rb +4 -0
- data/spec/factory_girl/attribute/callback_spec.rb +23 -0
- data/spec/factory_girl/factory_spec.rb +81 -0
- data/spec/factory_girl/proxy/build_spec.rb +8 -0
- data/spec/factory_girl/proxy/create_spec.rb +11 -0
- data/spec/factory_girl/proxy/stub_spec.rb +12 -2
- data/spec/factory_girl/proxy_spec.rb +56 -0
- data/spec/integration_spec.rb +39 -0
- metadata +6 -2
data/Rakefile
CHANGED
@@ -6,9 +6,10 @@ require 'rcov/rcovtask'
|
|
6
6
|
require 'date'
|
7
7
|
|
8
8
|
require 'spec/rake/spectask'
|
9
|
+
require 'cucumber/rake/task'
|
9
10
|
|
10
|
-
desc 'Default: run the specs.'
|
11
|
-
task :default => :spec
|
11
|
+
desc 'Default: run the specs and features.'
|
12
|
+
task :default => [:spec, :features]
|
12
13
|
|
13
14
|
Spec::Rake::SpecTask.new do |t|
|
14
15
|
t.spec_opts = ['--options', "spec/spec.opts"]
|
@@ -37,7 +38,7 @@ end
|
|
37
38
|
|
38
39
|
spec = Gem::Specification.new do |s|
|
39
40
|
s.name = %q{factory_girl}
|
40
|
-
s.version = "1.2.
|
41
|
+
s.version = "1.2.3"
|
41
42
|
s.summary = %q{factory_girl provides a framework and DSL for defining and
|
42
43
|
using model instance factories.}
|
43
44
|
s.description = %q{factory_girl provides a framework and DSL for defining and
|
@@ -73,3 +74,8 @@ task :gemspec do
|
|
73
74
|
f.write spec.to_ruby
|
74
75
|
end
|
75
76
|
end
|
77
|
+
|
78
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
79
|
+
t.fork = true
|
80
|
+
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
|
81
|
+
end
|
data/lib/factory_girl.rb
CHANGED
@@ -9,6 +9,7 @@ require 'factory_girl/attribute'
|
|
9
9
|
require 'factory_girl/attribute/static'
|
10
10
|
require 'factory_girl/attribute/dynamic'
|
11
11
|
require 'factory_girl/attribute/association'
|
12
|
+
require 'factory_girl/attribute/callback'
|
12
13
|
require 'factory_girl/sequence'
|
13
14
|
require 'factory_girl/aliases'
|
14
15
|
|
data/lib/factory_girl/factory.rb
CHANGED
@@ -4,6 +4,10 @@ class Factory
|
|
4
4
|
class AssociationDefinitionError < RuntimeError
|
5
5
|
end
|
6
6
|
|
7
|
+
# Raised when a callback is defined that has an invalid name
|
8
|
+
class InvalidCallbackNameError < RuntimeError
|
9
|
+
end
|
10
|
+
|
7
11
|
class << self
|
8
12
|
attr_accessor :factories #:nodoc:
|
9
13
|
|
@@ -184,6 +188,25 @@ class Factory
|
|
184
188
|
add_attribute(name) { s.next }
|
185
189
|
end
|
186
190
|
|
191
|
+
def after_build(&block)
|
192
|
+
callback(:after_build, &block)
|
193
|
+
end
|
194
|
+
|
195
|
+
def after_create(&block)
|
196
|
+
callback(:after_create, &block)
|
197
|
+
end
|
198
|
+
|
199
|
+
def after_stub(&block)
|
200
|
+
callback(:after_stub, &block)
|
201
|
+
end
|
202
|
+
|
203
|
+
def callback(name, &block)
|
204
|
+
unless [:after_build, :after_create, :after_stub].include?(name.to_sym)
|
205
|
+
raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub"
|
206
|
+
end
|
207
|
+
@attributes << Attribute::Callback.new(name.to_sym, block)
|
208
|
+
end
|
209
|
+
|
187
210
|
# Generates and returns a Hash of attributes from this factory. Attributes
|
188
211
|
# can be individually overridden by passing in a Hash of attribute => value
|
189
212
|
# pairs.
|
@@ -293,12 +316,24 @@ class Factory
|
|
293
316
|
proxy.result
|
294
317
|
end
|
295
318
|
|
296
|
-
private
|
297
|
-
|
298
319
|
def self.factory_by_name (name)
|
299
320
|
factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
|
300
321
|
end
|
301
|
-
|
322
|
+
|
323
|
+
def human_name(*args, &block)
|
324
|
+
if args.size == 0 && block.nil?
|
325
|
+
factory_name.to_s.gsub('_', ' ')
|
326
|
+
else
|
327
|
+
add_attribute(:human_name, *args, &block)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
def associations
|
332
|
+
attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
|
333
|
+
end
|
334
|
+
|
335
|
+
private
|
336
|
+
|
302
337
|
def class_for (class_or_to_s)
|
303
338
|
if class_or_to_s.respond_to?(:to_sym)
|
304
339
|
Object.const_get(variable_name_to_class_name(class_or_to_s))
|
@@ -316,7 +351,7 @@ class Factory
|
|
316
351
|
end
|
317
352
|
|
318
353
|
def attribute_defined? (name)
|
319
|
-
!@attributes.detect {|attr| attr.name == name }.nil?
|
354
|
+
!@attributes.detect {|attr| attr.name == name && !attr.is_a?(Factory::Attribute::Callback) }.nil?
|
320
355
|
end
|
321
356
|
|
322
357
|
def assert_valid_options(options)
|
data/lib/factory_girl/proxy.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
class Factory
|
2
2
|
|
3
3
|
class Proxy #:nodoc:
|
4
|
+
|
5
|
+
attr_reader :callbacks
|
6
|
+
|
4
7
|
def initialize(klass)
|
5
8
|
end
|
6
9
|
|
@@ -14,6 +17,20 @@ class Factory
|
|
14
17
|
def associate(name, factory, attributes)
|
15
18
|
end
|
16
19
|
|
20
|
+
def add_callback(name, block)
|
21
|
+
@callbacks ||= {}
|
22
|
+
@callbacks[name] ||= []
|
23
|
+
@callbacks[name] << block
|
24
|
+
end
|
25
|
+
|
26
|
+
def run_callbacks(name)
|
27
|
+
if @callbacks && @callbacks[name]
|
28
|
+
@callbacks[name].each do |block|
|
29
|
+
block.arity.zero? ? block.call : block.call(@instance)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
17
34
|
# Generates an association using the current build strategy.
|
18
35
|
#
|
19
36
|
# Arguments:
|
@@ -4,9 +4,9 @@ class Factory
|
|
4
4
|
@@next_id = 1000
|
5
5
|
|
6
6
|
def initialize(klass)
|
7
|
-
@
|
8
|
-
@
|
9
|
-
@
|
7
|
+
@instance = klass.new
|
8
|
+
@instance.id = next_id
|
9
|
+
@instance.instance_eval do
|
10
10
|
def new_record?
|
11
11
|
id.nil?
|
12
12
|
end
|
@@ -26,11 +26,11 @@ class Factory
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def get(attribute)
|
29
|
-
@
|
29
|
+
@instance.send(attribute)
|
30
30
|
end
|
31
31
|
|
32
32
|
def set(attribute, value)
|
33
|
-
@
|
33
|
+
@instance.send(:"#{attribute}=", value)
|
34
34
|
end
|
35
35
|
|
36
36
|
def associate(name, factory, attributes)
|
@@ -42,7 +42,8 @@ class Factory
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def result
|
45
|
-
|
45
|
+
run_callbacks(:after_stub)
|
46
|
+
@instance
|
46
47
|
end
|
47
48
|
end
|
48
49
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module FactoryGirlStepHelpers
|
2
|
+
def convert_association_string_to_instance(factory_name, assignment)
|
3
|
+
attribute, value = assignment.split(':', 2)
|
4
|
+
attributes = convert_human_hash_to_attribute_hash(attribute => value.strip)
|
5
|
+
factory = Factory.factory_by_name(factory_name)
|
6
|
+
model_class = factory.build_class
|
7
|
+
model_class.find(:first, :conditions => attributes) or
|
8
|
+
Factory(factory_name, attributes)
|
9
|
+
end
|
10
|
+
|
11
|
+
def convert_human_hash_to_attribute_hash(human_hash, associations = [])
|
12
|
+
human_hash.inject({}) do |attribute_hash, (human_key, value)|
|
13
|
+
key = human_key.downcase.gsub(' ', '_').to_sym
|
14
|
+
if association = associations.detect {|association| association.name == key }
|
15
|
+
value = convert_association_string_to_instance(association.factory, value)
|
16
|
+
end
|
17
|
+
attribute_hash.merge(key => value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
World(FactoryGirlStepHelpers)
|
23
|
+
|
24
|
+
Factory.factories.values.each do |factory|
|
25
|
+
# TODO: support irregular pluralizations
|
26
|
+
Given /^the following #{factory.human_name}s? exists?:$/ do |table|
|
27
|
+
table.hashes.each do |human_hash|
|
28
|
+
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
|
29
|
+
Factory.create(factory.factory_name, attributes)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
Given /^an? #{factory.human_name} exists$/ do
|
34
|
+
Factory(factory.factory_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
Given /^(\d+) #{factory.human_name}s exist$/ do |count|
|
38
|
+
count.to_i.times { Factory(factory.human_name) }
|
39
|
+
end
|
40
|
+
|
41
|
+
if factory.build_class.respond_to?(:columns)
|
42
|
+
factory.build_class.columns.each do |column|
|
43
|
+
human_column_name = column.name.downcase.gsub('_', ' ')
|
44
|
+
Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
|
45
|
+
Factory(factory.factory_name, column.name => value)
|
46
|
+
end
|
47
|
+
|
48
|
+
Given /^(\d+) #{factory.human_name}s exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
|
49
|
+
count.to_i.times { Factory(factory.factory_name, column.name => value) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
@@ -12,6 +12,10 @@ describe Factory::Attribute::Association do
|
|
12
12
|
@attr.name.should == @name
|
13
13
|
end
|
14
14
|
|
15
|
+
it "should have a factory" do
|
16
|
+
@attr.factory.should == @factory
|
17
|
+
end
|
18
|
+
|
15
19
|
it "should tell the proxy to associate when being added to a proxy" do
|
16
20
|
proxy = "proxy"
|
17
21
|
stub(proxy).associate
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe Factory::Attribute::Callback do
|
4
|
+
before do
|
5
|
+
@name = :after_create
|
6
|
+
@block = proc{ 'block' }
|
7
|
+
@attr = Factory::Attribute::Callback.new(@name, @block)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should have a name" do
|
11
|
+
@attr.name.should == @name
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should set its callback on a proxy" do
|
15
|
+
@proxy = "proxy"
|
16
|
+
mock(@proxy).add_callback(@name, @block)
|
17
|
+
@attr.add_to(@proxy)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should convert names to symbols" do
|
21
|
+
Factory::Attribute::Callback.new('name', nil).name.should == :name
|
22
|
+
end
|
23
|
+
end
|
@@ -27,6 +27,10 @@ describe Factory do
|
|
27
27
|
Factory.define(@name) {|f| }
|
28
28
|
@factory.should == Factory.factories[@name]
|
29
29
|
end
|
30
|
+
|
31
|
+
it "should allow that factory to be found by name" do
|
32
|
+
Factory.factory_by_name(@name).should == @factory
|
33
|
+
end
|
30
34
|
end
|
31
35
|
|
32
36
|
describe "a factory" do
|
@@ -90,6 +94,38 @@ describe Factory do
|
|
90
94
|
end
|
91
95
|
end
|
92
96
|
|
97
|
+
describe "adding a callback" do
|
98
|
+
it "should add a callback attribute when the after_build attribute is defined" do
|
99
|
+
mock(Factory::Attribute::Callback).new(:after_build, is_a(Proc)) { 'after_build callback' }
|
100
|
+
@factory.after_build {}
|
101
|
+
@factory.attributes.should include('after_build callback')
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should add a callback attribute when the after_create attribute is defined" do
|
105
|
+
mock(Factory::Attribute::Callback).new(:after_create, is_a(Proc)) { 'after_create callback' }
|
106
|
+
@factory.after_create {}
|
107
|
+
@factory.attributes.should include('after_create callback')
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should add a callback attribute when the after_stub attribute is defined" do
|
111
|
+
mock(Factory::Attribute::Callback).new(:after_stub, is_a(Proc)) { 'after_stub callback' }
|
112
|
+
@factory.after_stub {}
|
113
|
+
@factory.attributes.should include('after_stub callback')
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should add a callback attribute when defining a callback" do
|
117
|
+
mock(Factory::Attribute::Callback).new(:after_create, is_a(Proc)) { 'after_create callback' }
|
118
|
+
@factory.callback(:after_create) {}
|
119
|
+
@factory.attributes.should include('after_create callback')
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should raise an InvalidCallbackNameError when defining a callback with an invalid name" do
|
123
|
+
lambda{
|
124
|
+
@factory.callback(:invalid_callback_name) {}
|
125
|
+
}.should raise_error(Factory::InvalidCallbackNameError)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
93
129
|
describe "after adding an attribute" do
|
94
130
|
before do
|
95
131
|
@attribute = "attribute"
|
@@ -130,6 +166,16 @@ describe Factory do
|
|
130
166
|
factory.attributes.should include(attr)
|
131
167
|
end
|
132
168
|
|
169
|
+
it "should return associations" do
|
170
|
+
factory = Factory.new(:post)
|
171
|
+
factory.association(:author)
|
172
|
+
factory.association(:editor)
|
173
|
+
factory.associations.each do |association|
|
174
|
+
association.should be_a(Factory::Attribute::Association)
|
175
|
+
end
|
176
|
+
factory.associations.size.should == 2
|
177
|
+
end
|
178
|
+
|
133
179
|
it "should add an association with overrides" do
|
134
180
|
factory = Factory.new(:post)
|
135
181
|
name = :user
|
@@ -172,6 +218,21 @@ describe Factory do
|
|
172
218
|
@factory.attributes.should include(attribute)
|
173
219
|
end
|
174
220
|
|
221
|
+
it "should allow human_name as a static attribute name" do
|
222
|
+
attribute = 'attribute'
|
223
|
+
stub(attribute).name { :name }
|
224
|
+
mock(Factory::Attribute::Static).new(:human_name, 'value') { attribute}
|
225
|
+
@factory.human_name 'value'
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should allow human_name as a dynamic attribute name" do
|
229
|
+
attribute = 'attribute'
|
230
|
+
stub(attribute).name { :name }
|
231
|
+
block = lambda {}
|
232
|
+
mock(Factory::Attribute::Dynamic).new(:human_name, block) { attribute }
|
233
|
+
@factory.human_name(&block)
|
234
|
+
end
|
235
|
+
|
175
236
|
describe "when overriding generated attributes with a hash" do
|
176
237
|
before do
|
177
238
|
@attr = :name
|
@@ -397,6 +458,20 @@ describe Factory do
|
|
397
458
|
child.attributes.size.should == 1
|
398
459
|
child.attributes.first.should be_kind_of(Factory::Attribute::Dynamic)
|
399
460
|
end
|
461
|
+
|
462
|
+
it "inherit all callbacks" do
|
463
|
+
Factory.define(:child, :parent => :object) do |f|
|
464
|
+
f.after_stub {|o| o.name = 'Stubby' }
|
465
|
+
end
|
466
|
+
|
467
|
+
grandchild = Factory.define(:grandchild, :parent => :child) do |f|
|
468
|
+
f.after_stub {|o| o.name = "#{o.name} McStubby" }
|
469
|
+
end
|
470
|
+
|
471
|
+
grandchild.attributes.size.should == 3
|
472
|
+
grandchild.attributes.first.should be_kind_of(Factory::Attribute::Callback)
|
473
|
+
grandchild.attributes[1].should be_kind_of(Factory::Attribute::Callback)
|
474
|
+
end
|
400
475
|
end
|
401
476
|
|
402
477
|
describe 'defining a factory with a default strategy parameter' do
|
@@ -487,4 +562,10 @@ describe Factory do
|
|
487
562
|
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
488
563
|
end
|
489
564
|
end
|
565
|
+
|
566
|
+
it "should return the factory name without underscores for the human name" do
|
567
|
+
factory = Factory.new(:name_with_underscores)
|
568
|
+
factory.human_name.should == 'name with underscores'
|
569
|
+
end
|
570
|
+
|
490
571
|
end
|
@@ -45,6 +45,14 @@ describe Factory::Proxy::Build do
|
|
45
45
|
@proxy.result.should == @instance
|
46
46
|
end
|
47
47
|
|
48
|
+
it "should run the :after_build callback when retrieving the result" do
|
49
|
+
spy = Object.new
|
50
|
+
stub(spy).foo
|
51
|
+
@proxy.add_callback(:after_build, proc{ spy.foo })
|
52
|
+
@proxy.result
|
53
|
+
spy.should have_received.foo
|
54
|
+
end
|
55
|
+
|
48
56
|
describe "when setting an attribute" do
|
49
57
|
before do
|
50
58
|
stub(@instance).attribute = 'value'
|
@@ -44,6 +44,12 @@ describe Factory::Proxy::Create do
|
|
44
44
|
|
45
45
|
describe "when asked for the result" do
|
46
46
|
before do
|
47
|
+
@build_spy = Object.new
|
48
|
+
@create_spy = Object.new
|
49
|
+
stub(@build_spy).foo
|
50
|
+
stub(@create_spy).foo
|
51
|
+
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
|
52
|
+
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
|
47
53
|
@result = @proxy.result
|
48
54
|
end
|
49
55
|
|
@@ -54,6 +60,11 @@ describe Factory::Proxy::Create do
|
|
54
60
|
it "should return the built instance" do
|
55
61
|
@result.should == @instance
|
56
62
|
end
|
63
|
+
|
64
|
+
it "should run both the build and the create callbacks" do
|
65
|
+
@build_spy.should have_received.foo
|
66
|
+
@create_spy.should have_received.foo
|
67
|
+
end
|
57
68
|
end
|
58
69
|
|
59
70
|
describe "when setting an attribute" do
|
@@ -45,8 +45,18 @@ describe Factory::Proxy::Stub do
|
|
45
45
|
@stub.association(:user).should == @user
|
46
46
|
end
|
47
47
|
|
48
|
-
|
49
|
-
|
48
|
+
describe "when asked for the result" do
|
49
|
+
it "should return the actual instance" do
|
50
|
+
@stub.result.should == @instance
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should run the :after_stub callback" do
|
54
|
+
@spy = Object.new
|
55
|
+
stub(@spy).foo
|
56
|
+
@stub.add_callback(:after_stub, proc{ @spy.foo })
|
57
|
+
@stub.result
|
58
|
+
@spy.should have_received.foo
|
59
|
+
end
|
50
60
|
end
|
51
61
|
end
|
52
62
|
|
@@ -25,4 +25,60 @@ describe Factory::Proxy do
|
|
25
25
|
it "should raise an error when asked for the result" do
|
26
26
|
lambda { @proxy.result }.should raise_error(NotImplementedError)
|
27
27
|
end
|
28
|
+
|
29
|
+
describe "when adding callbacks" do
|
30
|
+
before do
|
31
|
+
@first_block = proc{ 'block 1' }
|
32
|
+
@second_block = proc{ 'block 2' }
|
33
|
+
end
|
34
|
+
it "should add a callback" do
|
35
|
+
@proxy.add_callback(:after_create, @first_block)
|
36
|
+
@proxy.callbacks[:after_create].should be_eql([@first_block])
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should add multiple callbacks of the same name" do
|
40
|
+
@proxy.add_callback(:after_create, @first_block)
|
41
|
+
@proxy.add_callback(:after_create, @second_block)
|
42
|
+
@proxy.callbacks[:after_create].should be_eql([@first_block, @second_block])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should add multiple callbacks of different names" do
|
46
|
+
@proxy.add_callback(:after_create, @first_block)
|
47
|
+
@proxy.add_callback(:after_build, @second_block)
|
48
|
+
@proxy.callbacks[:after_create].should be_eql([@first_block])
|
49
|
+
@proxy.callbacks[:after_build].should be_eql([@second_block])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "when running callbacks" do
|
54
|
+
before do
|
55
|
+
@first_spy = Object.new
|
56
|
+
@second_spy = Object.new
|
57
|
+
stub(@first_spy).foo
|
58
|
+
stub(@second_spy).foo
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should run all callbacks with a given name" do
|
62
|
+
@proxy.add_callback(:after_create, proc{ @first_spy.foo })
|
63
|
+
@proxy.add_callback(:after_create, proc{ @second_spy.foo })
|
64
|
+
@proxy.run_callbacks(:after_create)
|
65
|
+
@first_spy.should have_received.foo
|
66
|
+
@second_spy.should have_received.foo
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should only run callbacks with a given name" do
|
70
|
+
@proxy.add_callback(:after_create, proc{ @first_spy.foo })
|
71
|
+
@proxy.add_callback(:after_build, proc{ @second_spy.foo })
|
72
|
+
@proxy.run_callbacks(:after_create)
|
73
|
+
@first_spy.should have_received.foo
|
74
|
+
@second_spy.should_not have_received.foo
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should pass in the instance if the block takes an argument" do
|
78
|
+
@proxy.instance_variable_set("@instance", @first_spy)
|
79
|
+
@proxy.add_callback(:after_create, proc{|spy| spy.foo })
|
80
|
+
@proxy.run_callbacks(:after_create)
|
81
|
+
@first_spy.should have_received.foo
|
82
|
+
end
|
83
|
+
end
|
28
84
|
end
|
data/spec/integration_spec.rb
CHANGED
@@ -31,6 +31,21 @@ describe "integration" do
|
|
31
31
|
f.username 'GuestUser'
|
32
32
|
end
|
33
33
|
|
34
|
+
Factory.define :user_with_callbacks, :parent => :user do |f|
|
35
|
+
f.after_stub {|u| u.first_name = 'Stubby' }
|
36
|
+
f.after_build {|u| u.first_name = 'Buildy' }
|
37
|
+
f.after_create {|u| u.last_name = 'Createy' }
|
38
|
+
end
|
39
|
+
|
40
|
+
Factory.define :user_with_inherited_callbacks, :parent => :user_with_callbacks do |f|
|
41
|
+
f.callback(:after_stub) {|u| u.last_name = 'Double-Stubby' }
|
42
|
+
end
|
43
|
+
|
44
|
+
Factory.define :business do |f|
|
45
|
+
f.name 'Supplier of Awesome'
|
46
|
+
f.association :owner, :factory => :user
|
47
|
+
end
|
48
|
+
|
34
49
|
Factory.sequence :email do |n|
|
35
50
|
"somebody#{n}@example.com"
|
36
51
|
end
|
@@ -262,4 +277,28 @@ describe "integration" do
|
|
262
277
|
Factory(:sequence_abuser)
|
263
278
|
}.should raise_error(Factory::SequenceAbuseError)
|
264
279
|
end
|
280
|
+
|
281
|
+
describe "an instance with callbacks" do
|
282
|
+
it "should run the after_stub callback when stubbing" do
|
283
|
+
@user = Factory.stub(:user_with_callbacks)
|
284
|
+
@user.first_name.should == 'Stubby'
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should run the after_build callback when building" do
|
288
|
+
@user = Factory.build(:user_with_callbacks)
|
289
|
+
@user.first_name.should == 'Buildy'
|
290
|
+
end
|
291
|
+
|
292
|
+
it "should run both the after_build and after_create callbacks when creating" do
|
293
|
+
@user = Factory(:user_with_callbacks)
|
294
|
+
@user.first_name.should == 'Buildy'
|
295
|
+
@user.last_name.should == 'Createy'
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should run both the after_stub callback on the factory and the inherited after_stub callback" do
|
299
|
+
@user = Factory.stub(:user_with_inherited_callbacks)
|
300
|
+
@user.first_name.should == 'Stubby'
|
301
|
+
@user.last_name.should == 'Double-Stubby'
|
302
|
+
end
|
303
|
+
end
|
265
304
|
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: 1.2.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Ferris
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-10-16 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- README.rdoc
|
33
33
|
- lib/factory_girl/aliases.rb
|
34
34
|
- lib/factory_girl/attribute/association.rb
|
35
|
+
- lib/factory_girl/attribute/callback.rb
|
35
36
|
- lib/factory_girl/attribute/dynamic.rb
|
36
37
|
- lib/factory_girl/attribute/static.rb
|
37
38
|
- lib/factory_girl/attribute.rb
|
@@ -42,6 +43,7 @@ files:
|
|
42
43
|
- lib/factory_girl/proxy/stub.rb
|
43
44
|
- lib/factory_girl/proxy.rb
|
44
45
|
- lib/factory_girl/sequence.rb
|
46
|
+
- lib/factory_girl/step_definitions.rb
|
45
47
|
- lib/factory_girl/syntax/blueprint.rb
|
46
48
|
- lib/factory_girl/syntax/generate.rb
|
47
49
|
- lib/factory_girl/syntax/make.rb
|
@@ -50,6 +52,7 @@ files:
|
|
50
52
|
- lib/factory_girl.rb
|
51
53
|
- spec/factory_girl/aliases_spec.rb
|
52
54
|
- spec/factory_girl/attribute/association_spec.rb
|
55
|
+
- spec/factory_girl/attribute/callback_spec.rb
|
53
56
|
- spec/factory_girl/attribute/dynamic_spec.rb
|
54
57
|
- spec/factory_girl/attribute/static_spec.rb
|
55
58
|
- spec/factory_girl/attribute_spec.rb
|
@@ -100,6 +103,7 @@ summary: factory_girl provides a framework and DSL for defining and using model
|
|
100
103
|
test_files:
|
101
104
|
- spec/factory_girl/aliases_spec.rb
|
102
105
|
- spec/factory_girl/attribute/association_spec.rb
|
106
|
+
- spec/factory_girl/attribute/callback_spec.rb
|
103
107
|
- spec/factory_girl/attribute/dynamic_spec.rb
|
104
108
|
- spec/factory_girl/attribute/static_spec.rb
|
105
109
|
- spec/factory_girl/attribute_spec.rb
|