factory_girl 2.0.0.beta1 → 2.0.0.beta2
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/Appraisals +12 -0
- data/{CONTRIBUTION_GUIDELINES.rdoc → CONTRIBUTION_GUIDELINES.md} +3 -3
- data/GETTING_STARTED.md +246 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +62 -0
- data/README.md +64 -0
- data/Rakefile +15 -30
- data/features/factory_girl_steps.feature +26 -0
- data/features/step_definitions/database_steps.rb +2 -0
- data/features/support/env.rb +0 -9
- data/features/support/factories.rb +15 -0
- data/features/support/test.db +0 -0
- data/lib/factory_girl.rb +2 -0
- data/lib/factory_girl/definition_proxy.rb +10 -43
- data/lib/factory_girl/factory.rb +48 -17
- data/lib/factory_girl/proxy.rb +3 -3
- data/lib/factory_girl/proxy/attributes_for.rb +1 -1
- data/lib/factory_girl/proxy/build.rb +3 -3
- data/lib/factory_girl/proxy/create.rb +6 -2
- data/lib/factory_girl/proxy/stub.rb +3 -3
- data/lib/factory_girl/registry.rb +59 -0
- data/lib/factory_girl/sequence.rb +23 -9
- data/lib/factory_girl/step_definitions.rb +16 -9
- data/lib/factory_girl/syntax/blueprint.rb +2 -2
- data/lib/factory_girl/syntax/default.rb +5 -3
- data/lib/factory_girl/syntax/generate.rb +6 -6
- data/lib/factory_girl/syntax/make.rb +2 -2
- data/lib/factory_girl/syntax/methods.rb +75 -0
- data/lib/factory_girl/syntax/sham.rb +2 -2
- data/lib/factory_girl/syntax/vintage.rb +16 -79
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/acceptance_helper.rb +7 -12
- data/spec/acceptance/attribute_aliases_spec.rb +26 -0
- data/spec/acceptance/attributes_for_spec.rb +48 -0
- data/spec/acceptance/build_spec.rb +35 -0
- data/spec/acceptance/build_stubbed_spec.rb +79 -0
- data/spec/acceptance/callbacks_spec.rb +42 -0
- data/spec/acceptance/create_spec.rb +67 -0
- data/spec/acceptance/default_strategy_spec.rb +27 -0
- data/spec/acceptance/definition_spec.rb +28 -0
- data/spec/acceptance/parent_spec.rb +39 -0
- data/spec/acceptance/sequence_spec.rb +32 -0
- data/spec/acceptance/syntax/blueprint_spec.rb +7 -5
- data/spec/acceptance/syntax/generate_spec.rb +10 -4
- data/spec/acceptance/syntax/make_spec.rb +7 -4
- data/spec/acceptance/syntax/sham_spec.rb +15 -8
- data/spec/acceptance/syntax/vintage_spec.rb +57 -17
- data/spec/factory_girl/attribute/dynamic_spec.rb +1 -1
- data/spec/factory_girl/definition_proxy_spec.rb +13 -11
- data/spec/factory_girl/factory_spec.rb +29 -52
- data/spec/factory_girl/find_definitions_spec.rb +34 -23
- data/spec/factory_girl/proxy/attributes_for_spec.rb +6 -6
- data/spec/factory_girl/proxy/build_spec.rb +4 -4
- data/spec/factory_girl/proxy/create_spec.rb +11 -3
- data/spec/factory_girl/proxy/stub_spec.rb +6 -6
- data/spec/factory_girl/proxy_spec.rb +1 -1
- data/spec/factory_girl/registry_spec.rb +92 -0
- data/spec/factory_girl/sequence_spec.rb +65 -8
- data/spec/spec_helper.rb +75 -29
- metadata +66 -43
- data/README.rdoc +0 -282
- data/spec/acceptance/acceptance_spec.rb +0 -288
- data/spec/acceptance/models.rb +0 -48
data/spec/spec_helper.rb
CHANGED
@@ -2,45 +2,91 @@ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
|
2
2
|
$LOAD_PATH << File.join(File.dirname(__FILE__))
|
3
3
|
|
4
4
|
require 'rubygems'
|
5
|
-
require '
|
6
|
-
require '
|
5
|
+
require 'rspec'
|
6
|
+
require 'rspec/autorun'
|
7
7
|
require 'rr'
|
8
8
|
|
9
9
|
require 'factory_girl'
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
module RR
|
12
|
+
module Adapters
|
13
|
+
module Rspec
|
14
|
+
def self.included(mod)
|
15
|
+
RSpec.configuration.backtrace_clean_patterns.push(RR::Errors::BACKTRACE_IDENTIFIER)
|
16
|
+
end
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.mock_framework = :rr
|
23
|
+
RSpec::Core::ExampleGroup.send(:include, RR::Adapters::Rspec)
|
24
|
+
config.after do
|
25
|
+
FactoryGirl.registry = FactoryGirl::Registry.new
|
22
26
|
end
|
27
|
+
end
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
module DefinesConstants
|
30
|
+
def self.included(example_group)
|
31
|
+
example_group.class_eval do
|
32
|
+
before do
|
33
|
+
@defined_constants = []
|
34
|
+
@created_tables = []
|
35
|
+
end
|
30
36
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@defined_constants << path
|
37
|
-
klass
|
38
|
-
end
|
37
|
+
after do
|
38
|
+
@defined_constants.reverse.each do |path|
|
39
|
+
namespace, class_name = *constant_path(path)
|
40
|
+
namespace.send(:remove_const, class_name)
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
@created_tables.each do |table_name|
|
44
|
+
ActiveRecord::Base.
|
45
|
+
connection.
|
46
|
+
execute("DROP TABLE IF EXISTS #{table_name}")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def define_class(path, base = Object, &block)
|
51
|
+
namespace, class_name = *constant_path(path)
|
52
|
+
klass = Class.new(base)
|
53
|
+
namespace.const_set(class_name, klass)
|
54
|
+
klass.class_eval(&block) if block_given?
|
55
|
+
@defined_constants << path
|
56
|
+
klass
|
57
|
+
end
|
58
|
+
|
59
|
+
def define_model(name, columns = {}, &block)
|
60
|
+
model = define_class(name, ActiveRecord::Base, &block)
|
61
|
+
create_table(model.table_name) do |table|
|
62
|
+
columns.each do |name, type|
|
63
|
+
table.column name, type
|
64
|
+
end
|
65
|
+
end
|
66
|
+
model
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_table(table_name, &block)
|
70
|
+
connection = ActiveRecord::Base.connection
|
71
|
+
|
72
|
+
begin
|
73
|
+
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
74
|
+
connection.create_table(table_name, &block)
|
75
|
+
@created_tables << table_name
|
76
|
+
connection
|
77
|
+
rescue Exception => exception
|
78
|
+
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
79
|
+
raise exception
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def constant_path(constant_name)
|
84
|
+
names = constant_name.split('::')
|
85
|
+
class_name = names.pop
|
86
|
+
namespace = names.inject(Object) { |result, name| result.const_get(name) }
|
87
|
+
[namespace, class_name]
|
88
|
+
end
|
89
|
+
end
|
45
90
|
end
|
46
91
|
end
|
92
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_girl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 299253610
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 0
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
version: 2.0.0.
|
10
|
+
- beta2
|
11
|
+
version: 2.0.0.beta2
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Joe Ferris
|
@@ -16,13 +16,14 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date:
|
19
|
+
date: 2011-02-17 00:00:00 -05:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
23
|
-
|
23
|
+
type: :development
|
24
24
|
prerelease: false
|
25
|
-
|
25
|
+
name: rcov
|
26
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
27
|
none: false
|
27
28
|
requirements:
|
28
29
|
- - ">="
|
@@ -31,12 +32,12 @@ dependencies:
|
|
31
32
|
segments:
|
32
33
|
- 0
|
33
34
|
version: "0"
|
34
|
-
|
35
|
-
version_requirements: *id001
|
35
|
+
requirement: *id001
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
|
37
|
+
type: :development
|
38
38
|
prerelease: false
|
39
|
-
|
39
|
+
name: rspec
|
40
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
41
|
none: false
|
41
42
|
requirements:
|
42
43
|
- - ">="
|
@@ -45,12 +46,12 @@ dependencies:
|
|
45
46
|
segments:
|
46
47
|
- 0
|
47
48
|
version: "0"
|
48
|
-
|
49
|
-
version_requirements: *id002
|
49
|
+
requirement: *id002
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
|
-
|
51
|
+
type: :development
|
52
52
|
prerelease: false
|
53
|
-
|
53
|
+
name: cucumber
|
54
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
55
|
none: false
|
55
56
|
requirements:
|
56
57
|
- - ">="
|
@@ -59,12 +60,12 @@ dependencies:
|
|
59
60
|
segments:
|
60
61
|
- 0
|
61
62
|
version: "0"
|
62
|
-
|
63
|
-
version_requirements: *id003
|
63
|
+
requirement: *id003
|
64
64
|
- !ruby/object:Gem::Dependency
|
65
|
-
|
65
|
+
type: :development
|
66
66
|
prerelease: false
|
67
|
-
|
67
|
+
name: activerecord
|
68
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
68
69
|
none: false
|
69
70
|
requirements:
|
70
71
|
- - ~>
|
@@ -75,12 +76,12 @@ dependencies:
|
|
75
76
|
- 3
|
76
77
|
- 5
|
77
78
|
version: 2.3.5
|
78
|
-
|
79
|
-
version_requirements: *id004
|
79
|
+
requirement: *id004
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
|
-
|
81
|
+
type: :development
|
82
82
|
prerelease: false
|
83
|
-
|
83
|
+
name: activerecord
|
84
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
84
85
|
none: false
|
85
86
|
requirements:
|
86
87
|
- - ~>
|
@@ -92,12 +93,12 @@ dependencies:
|
|
92
93
|
- 0
|
93
94
|
- beta3
|
94
95
|
version: 3.0.0.beta3
|
95
|
-
|
96
|
-
version_requirements: *id005
|
96
|
+
requirement: *id005
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
|
98
|
+
type: :development
|
99
99
|
prerelease: false
|
100
|
-
|
100
|
+
name: rr
|
101
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
101
102
|
none: false
|
102
103
|
requirements:
|
103
104
|
- - ">="
|
@@ -106,12 +107,12 @@ dependencies:
|
|
106
107
|
segments:
|
107
108
|
- 0
|
108
109
|
version: "0"
|
109
|
-
|
110
|
-
version_requirements: *id006
|
110
|
+
requirement: *id006
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
|
-
|
112
|
+
type: :development
|
113
113
|
prerelease: false
|
114
|
-
|
114
|
+
name: sqlite3
|
115
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
115
116
|
none: false
|
116
117
|
requirements:
|
117
118
|
- - ">="
|
@@ -120,8 +121,7 @@ dependencies:
|
|
120
121
|
segments:
|
121
122
|
- 0
|
122
123
|
version: "0"
|
123
|
-
|
124
|
-
version_requirements: *id007
|
124
|
+
requirement: *id007
|
125
125
|
description: |-
|
126
126
|
factory_girl provides a framework and DSL for defining and
|
127
127
|
using factories - less error-prone, more explicit, and
|
@@ -131,14 +131,18 @@ executables: []
|
|
131
131
|
|
132
132
|
extensions: []
|
133
133
|
|
134
|
-
extra_rdoc_files:
|
135
|
-
|
134
|
+
extra_rdoc_files: []
|
135
|
+
|
136
136
|
files:
|
137
|
+
- Appraisals
|
137
138
|
- Changelog
|
138
|
-
- CONTRIBUTION_GUIDELINES.
|
139
|
+
- CONTRIBUTION_GUIDELINES.md
|
140
|
+
- Gemfile
|
141
|
+
- Gemfile.lock
|
142
|
+
- GETTING_STARTED.md
|
139
143
|
- LICENSE
|
140
144
|
- Rakefile
|
141
|
-
- README.
|
145
|
+
- README.md
|
142
146
|
- lib/factory_girl/aliases.rb
|
143
147
|
- lib/factory_girl/attribute/association.rb
|
144
148
|
- lib/factory_girl/attribute/callback.rb
|
@@ -155,20 +159,30 @@ files:
|
|
155
159
|
- lib/factory_girl/proxy/stub.rb
|
156
160
|
- lib/factory_girl/proxy.rb
|
157
161
|
- lib/factory_girl/rails2.rb
|
162
|
+
- lib/factory_girl/registry.rb
|
158
163
|
- lib/factory_girl/sequence.rb
|
159
164
|
- lib/factory_girl/step_definitions.rb
|
160
165
|
- lib/factory_girl/syntax/blueprint.rb
|
161
166
|
- lib/factory_girl/syntax/default.rb
|
162
167
|
- lib/factory_girl/syntax/generate.rb
|
163
168
|
- lib/factory_girl/syntax/make.rb
|
169
|
+
- lib/factory_girl/syntax/methods.rb
|
164
170
|
- lib/factory_girl/syntax/sham.rb
|
165
171
|
- lib/factory_girl/syntax/vintage.rb
|
166
172
|
- lib/factory_girl/syntax.rb
|
167
173
|
- lib/factory_girl/version.rb
|
168
174
|
- lib/factory_girl.rb
|
169
175
|
- spec/acceptance/acceptance_helper.rb
|
170
|
-
- spec/acceptance/
|
171
|
-
- spec/acceptance/
|
176
|
+
- spec/acceptance/attribute_aliases_spec.rb
|
177
|
+
- spec/acceptance/attributes_for_spec.rb
|
178
|
+
- spec/acceptance/build_spec.rb
|
179
|
+
- spec/acceptance/build_stubbed_spec.rb
|
180
|
+
- spec/acceptance/callbacks_spec.rb
|
181
|
+
- spec/acceptance/create_spec.rb
|
182
|
+
- spec/acceptance/default_strategy_spec.rb
|
183
|
+
- spec/acceptance/definition_spec.rb
|
184
|
+
- spec/acceptance/parent_spec.rb
|
185
|
+
- spec/acceptance/sequence_spec.rb
|
172
186
|
- spec/acceptance/syntax/blueprint_spec.rb
|
173
187
|
- spec/acceptance/syntax/generate_spec.rb
|
174
188
|
- spec/acceptance/syntax/make_spec.rb
|
@@ -189,6 +203,7 @@ files:
|
|
189
203
|
- spec/factory_girl/proxy/create_spec.rb
|
190
204
|
- spec/factory_girl/proxy/stub_spec.rb
|
191
205
|
- spec/factory_girl/proxy_spec.rb
|
206
|
+
- spec/factory_girl/registry_spec.rb
|
192
207
|
- spec/factory_girl/sequence_spec.rb
|
193
208
|
- spec/spec_helper.rb
|
194
209
|
- features/factory_girl_steps.feature
|
@@ -201,10 +216,8 @@ homepage: http://thoughtbot.com/projects/factory_girl
|
|
201
216
|
licenses: []
|
202
217
|
|
203
218
|
post_install_message:
|
204
|
-
rdoc_options:
|
205
|
-
|
206
|
-
- --main
|
207
|
-
- README.rdoc
|
219
|
+
rdoc_options: []
|
220
|
+
|
208
221
|
require_paths:
|
209
222
|
- lib
|
210
223
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -235,7 +248,16 @@ signing_key:
|
|
235
248
|
specification_version: 3
|
236
249
|
summary: factory_girl provides a framework and DSL for defining and using model instance factories.
|
237
250
|
test_files:
|
238
|
-
- spec/acceptance/
|
251
|
+
- spec/acceptance/attribute_aliases_spec.rb
|
252
|
+
- spec/acceptance/attributes_for_spec.rb
|
253
|
+
- spec/acceptance/build_spec.rb
|
254
|
+
- spec/acceptance/build_stubbed_spec.rb
|
255
|
+
- spec/acceptance/callbacks_spec.rb
|
256
|
+
- spec/acceptance/create_spec.rb
|
257
|
+
- spec/acceptance/default_strategy_spec.rb
|
258
|
+
- spec/acceptance/definition_spec.rb
|
259
|
+
- spec/acceptance/parent_spec.rb
|
260
|
+
- spec/acceptance/sequence_spec.rb
|
239
261
|
- spec/acceptance/syntax/blueprint_spec.rb
|
240
262
|
- spec/acceptance/syntax/generate_spec.rb
|
241
263
|
- spec/acceptance/syntax/make_spec.rb
|
@@ -256,6 +278,7 @@ test_files:
|
|
256
278
|
- spec/factory_girl/proxy/create_spec.rb
|
257
279
|
- spec/factory_girl/proxy/stub_spec.rb
|
258
280
|
- spec/factory_girl/proxy_spec.rb
|
281
|
+
- spec/factory_girl/registry_spec.rb
|
259
282
|
- spec/factory_girl/sequence_spec.rb
|
260
283
|
- features/factory_girl_steps.feature
|
261
284
|
- features/step_definitions/database_steps.rb
|
data/README.rdoc
DELETED
@@ -1,282 +0,0 @@
|
|
1
|
-
= NOTE: this documentation is for the beta version of factory_girl 2
|
2
|
-
|
3
|
-
Up-to-date documentation for the stable branch can be found here:
|
4
|
-
|
5
|
-
http://github.com/thoughtbot/factory_girl/tree/1.3.x
|
6
|
-
|
7
|
-
= factory_girl
|
8
|
-
|
9
|
-
factory_girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.
|
10
|
-
|
11
|
-
If you want to use factory_girl with Rails 3, see
|
12
|
-
http://github.com/thoughtbot/factory_girl_rails
|
13
|
-
|
14
|
-
== Download
|
15
|
-
|
16
|
-
Github: http://github.com/thoughtbot/factory_girl/tree/master
|
17
|
-
|
18
|
-
Gem:
|
19
|
-
gem install factory_girl
|
20
|
-
|
21
|
-
== Defining factories
|
22
|
-
|
23
|
-
Each factory has a name and a set of attributes. The name is used to guess the class of the object by default, but it's possible to explicitly specify it:
|
24
|
-
|
25
|
-
# This will guess the User class
|
26
|
-
FactoryGirl.define :user do
|
27
|
-
factory :user do
|
28
|
-
first_name 'John'
|
29
|
-
last_name 'Doe'
|
30
|
-
admin false
|
31
|
-
end
|
32
|
-
|
33
|
-
# This will use the User class (Admin would have been guessed)
|
34
|
-
factory :admin, :class => User do
|
35
|
-
first_name 'Admin'
|
36
|
-
last_name 'User'
|
37
|
-
admin true
|
38
|
-
end
|
39
|
-
|
40
|
-
# The same, but using a string instead of class constant
|
41
|
-
factory :admin, :class => 'user' do
|
42
|
-
first_name 'Admin'
|
43
|
-
last_name 'User'
|
44
|
-
admin true
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
It is highly recommended that you have one factory for each class that provides the simplest set of attributes necessary to create an instance of that class. If you're creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults. Other factories can be created through inheritance to cover common scenarios for each class.
|
49
|
-
|
50
|
-
Attempting to define multiple factories with the same name will raise an error.
|
51
|
-
|
52
|
-
Factories can be defined anywhere, but will be automatically loaded if they
|
53
|
-
are defined in files at the following locations:
|
54
|
-
|
55
|
-
test/factories.rb
|
56
|
-
spec/factories.rb
|
57
|
-
test/factories/*.rb
|
58
|
-
spec/factories/*.rb
|
59
|
-
|
60
|
-
== Using factories
|
61
|
-
|
62
|
-
factory_girl supports several different build strategies: build, create, attributes_for and stub:
|
63
|
-
|
64
|
-
# Returns a User instance that's not saved
|
65
|
-
user = Factory.build(:user)
|
66
|
-
|
67
|
-
# Returns a saved User instance
|
68
|
-
user = Factory.create(:user)
|
69
|
-
|
70
|
-
# Returns a hash of attributes that can be used to build a User instance:
|
71
|
-
attrs = Factory.attributes_for(:user)
|
72
|
-
|
73
|
-
# Returns an object with all defined attributes stubbed out:
|
74
|
-
stub = Factory.stub(:user)
|
75
|
-
|
76
|
-
You can use the Factory method as a shortcut for the default build strategy:
|
77
|
-
|
78
|
-
# Same as Factory.create :user:
|
79
|
-
user = Factory(:user)
|
80
|
-
|
81
|
-
The default strategy can be overriden:
|
82
|
-
|
83
|
-
# Now same as Factory.build(:user)
|
84
|
-
factory :user, :default_strategy => :build do
|
85
|
-
...
|
86
|
-
end
|
87
|
-
|
88
|
-
user = Factory(:user)
|
89
|
-
|
90
|
-
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
|
91
|
-
|
92
|
-
# Build a User instance and override the first_name property
|
93
|
-
user = Factory.build(:user, :first_name => 'Joe')
|
94
|
-
user.first_name
|
95
|
-
# => "Joe"
|
96
|
-
|
97
|
-
== Lazy Attributes
|
98
|
-
|
99
|
-
Most factory attributes can be added using static values that are evaluated when the factory is defined, but some attributes (such as associations and other attributes that must be dynamically generated) will need values assigned each time an instance is generated. These "lazy" attributes can be added by passing a block instead of a parameter:
|
100
|
-
|
101
|
-
factory :user do
|
102
|
-
# ...
|
103
|
-
activation_code { User.generate_activation_code }
|
104
|
-
end
|
105
|
-
|
106
|
-
== Dependent Attributes
|
107
|
-
|
108
|
-
Attributes can be based on the values of other attributes using the proxy that is yieled to lazy attribute blocks:
|
109
|
-
|
110
|
-
factory :user do
|
111
|
-
first_name 'Joe'
|
112
|
-
last_name 'Blow'
|
113
|
-
email { "#{first_name}.#{last_name}@example.com".downcase }
|
114
|
-
end
|
115
|
-
|
116
|
-
Factory(:user, :last_name => 'Doe').email
|
117
|
-
# => "joe.doe@example.com"
|
118
|
-
|
119
|
-
== Associations
|
120
|
-
|
121
|
-
Associated instances can be generated by using the association method when
|
122
|
-
defining a lazy attribute:
|
123
|
-
|
124
|
-
factory :post do
|
125
|
-
# ...
|
126
|
-
author
|
127
|
-
end
|
128
|
-
|
129
|
-
You can also specify a different factory or override attributes:
|
130
|
-
|
131
|
-
factory :post do
|
132
|
-
# ...
|
133
|
-
association :author, :factory => :user, :last_name => 'Writely'
|
134
|
-
end
|
135
|
-
|
136
|
-
The behavior of the association method varies depending on the build strategy used for the parent object.
|
137
|
-
|
138
|
-
# Builds and saves a User and a Post
|
139
|
-
post = Factory(:post)
|
140
|
-
post.new_record? # => false
|
141
|
-
post.author.new_record # => false
|
142
|
-
|
143
|
-
# Builds and saves a User, and then builds but does not save a Post
|
144
|
-
post = Factory.build(:post)
|
145
|
-
post.new_record? # => true
|
146
|
-
post.author.new_record # => false
|
147
|
-
|
148
|
-
If the factory name is the same as the association name, the factory name can
|
149
|
-
be left out.
|
150
|
-
|
151
|
-
== Inheritance
|
152
|
-
|
153
|
-
You can easily create multiple factories for the same class without repeating common attributes by using inheritance:
|
154
|
-
|
155
|
-
factory :post do
|
156
|
-
# the 'title' attribute is required for all posts
|
157
|
-
title 'A title'
|
158
|
-
end
|
159
|
-
|
160
|
-
factory :approved_post, :parent => :post do
|
161
|
-
approved true
|
162
|
-
# the 'approver' association is required for an approved post
|
163
|
-
association :approver, :factory => :user
|
164
|
-
end
|
165
|
-
|
166
|
-
== Sequences
|
167
|
-
|
168
|
-
Unique values in a specific format (for example, e-mail addresses) can be
|
169
|
-
generated using sequences. Sequences are defined by calling Factory.sequence,
|
170
|
-
and values in a sequence are generated by calling Factory.next:
|
171
|
-
|
172
|
-
# Defines a new sequence
|
173
|
-
FactoryGirl.sequence :email do |n|
|
174
|
-
"person#{n}@example.com"
|
175
|
-
end
|
176
|
-
|
177
|
-
Factory.next :email
|
178
|
-
# => "person1@example.com"
|
179
|
-
|
180
|
-
Factory.next :email
|
181
|
-
# => "person2@example.com"
|
182
|
-
|
183
|
-
Sequences can be used as attributes:
|
184
|
-
|
185
|
-
factory :user do
|
186
|
-
email
|
187
|
-
end
|
188
|
-
|
189
|
-
Or in lazy attributes:
|
190
|
-
|
191
|
-
factory :invite do
|
192
|
-
invitee { Factory.next(:email) }
|
193
|
-
end
|
194
|
-
|
195
|
-
And it's also possible to define an in-line sequence that is only used in
|
196
|
-
a particular factory:
|
197
|
-
|
198
|
-
factory :user do
|
199
|
-
f.sequence(:email) {|n| "person#{n}@example.com" }
|
200
|
-
end
|
201
|
-
|
202
|
-
== Callbacks
|
203
|
-
|
204
|
-
Factory_girl makes available three callbacks for injecting some code:
|
205
|
-
|
206
|
-
* after_build - called after a factory is built (via Factory.build)
|
207
|
-
* after_create - called after a factory is saved (via Factory.create)
|
208
|
-
* after_stub - called after a factory is stubbed (via Factory.stub)
|
209
|
-
|
210
|
-
Examples:
|
211
|
-
|
212
|
-
# Define a factory that calls the generate_hashed_password method after it is built
|
213
|
-
factory :user do
|
214
|
-
after_build { |user| do_something_to(user) }
|
215
|
-
end
|
216
|
-
|
217
|
-
Note that you'll have an instance of the user in the block. This can be useful.
|
218
|
-
|
219
|
-
You can also define multiple types of callbacks on the same factory:
|
220
|
-
|
221
|
-
factory :user do
|
222
|
-
after_build { |user| do_something_to(user) }
|
223
|
-
after_create { |user| do_something_else_to(user) }
|
224
|
-
end
|
225
|
-
|
226
|
-
Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
|
227
|
-
|
228
|
-
factory :user do
|
229
|
-
after_create { this_runs_first }
|
230
|
-
after_create { then_this }
|
231
|
-
end
|
232
|
-
|
233
|
-
Calling Factory.create will invoke both after_build and after_create callbacks.
|
234
|
-
|
235
|
-
Also, like standard attributes, child factories will inherit (and can define additional) callbacks from their parent factory.
|
236
|
-
|
237
|
-
== Alternate Syntaxes
|
238
|
-
|
239
|
-
Users' tastes for syntax vary dramatically, but most users are looking for a common feature set. Because of this, factory_girl supports "syntax layers" which provide alternate interfaces. See Factory::Syntax for information about the various layers available. For example, the Machinist-style syntax is popular:
|
240
|
-
|
241
|
-
require 'factory_girl/syntax/blueprint'
|
242
|
-
require 'factory_girl/syntax/make'
|
243
|
-
require 'factory_girl/syntax/sham'
|
244
|
-
|
245
|
-
Sham.email {|n| "#{n}@example.com" }
|
246
|
-
|
247
|
-
User.blueprint do
|
248
|
-
name { 'Billy Bob' }
|
249
|
-
email { Sham.email }
|
250
|
-
end
|
251
|
-
|
252
|
-
User.make(:name => 'Johnny')
|
253
|
-
|
254
|
-
== More Information
|
255
|
-
|
256
|
-
* RDoc[http://rdoc.info/projects/thoughtbot/factory_girl]
|
257
|
-
* Mailing list[http://groups.google.com/group/factory_girl]
|
258
|
-
* Issues[http://github.com/thoughtbot/factory_girl/issues]
|
259
|
-
* GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS[http://giantrobots.thoughtbot.com]
|
260
|
-
|
261
|
-
== Contributing
|
262
|
-
|
263
|
-
Please read the contribution guidelines before submitting patches or pull requests.
|
264
|
-
|
265
|
-
== Author
|
266
|
-
|
267
|
-
factory_girl was written by Joe Ferris with contributions from several authors, including:
|
268
|
-
* Alex Sharp
|
269
|
-
* Eugene Bolshakov
|
270
|
-
* Jon Yurek
|
271
|
-
* Josh Nichols
|
272
|
-
* Josh Owens
|
273
|
-
* Nate Sutton
|
274
|
-
|
275
|
-
The syntax layers are derived from software written by the following authors:
|
276
|
-
* Pete Yandell
|
277
|
-
* Rick Bradley
|
278
|
-
* Yossef Mendelssohn
|
279
|
-
|
280
|
-
Thanks to all members of thoughtbot for inspiration, ideas, and funding.
|
281
|
-
|
282
|
-
Copyright 2008-2010 Joe Ferris and thoughtbot[http://www.thoughtbot.com], inc.
|