factory_girl 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +59 -13
- data/Rakefile +29 -31
- data/VERSION +1 -0
- data/lib/factory_girl/factory.rb +7 -0
- data/lib/factory_girl/step_definitions.rb +1 -0
- data/spec/factory_girl/factory_spec.rb +13 -1
- metadata +94 -15
data/README.rdoc
CHANGED
@@ -7,14 +7,12 @@ factory_girl is a fixtures replacement with a straightforward definition syntax,
|
|
7
7
|
Github: http://github.com/thoughtbot/factory_girl/tree/master
|
8
8
|
|
9
9
|
Gem:
|
10
|
-
gem install
|
10
|
+
gem install factory_girl --source http://gemcutter.org
|
11
11
|
|
12
|
-
Note: if you install factory_girl using the gem from
|
12
|
+
Note: if you install factory_girl using the gem from Gemcutter, you'll need this
|
13
13
|
in your environment.rb if you want to use Rails 2.1+'s dependency manager:
|
14
14
|
|
15
|
-
config.gem "
|
16
|
-
:lib => "factory_girl",
|
17
|
-
:source => "http://gems.github.com"
|
15
|
+
config.gem "factory_girl", :source => "http://gemcutter.org"
|
18
16
|
|
19
17
|
== Defining factories
|
20
18
|
|
@@ -43,6 +41,8 @@ Each factory has a name and a set of attributes. The name is used to guess the c
|
|
43
41
|
|
44
42
|
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.
|
45
43
|
|
44
|
+
Attempting to define multiple factories with the same name will raise an error.
|
45
|
+
|
46
46
|
Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:
|
47
47
|
|
48
48
|
test/factories.rb
|
@@ -80,7 +80,7 @@ The default strategy can be overriden:
|
|
80
80
|
|
81
81
|
user = Factory(:user)
|
82
82
|
|
83
|
-
No matter which
|
83
|
+
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
|
84
84
|
|
85
85
|
# Build a User instance and override the first_name property
|
86
86
|
user = Factory.build(:user, :first_name => 'Joe')
|
@@ -191,19 +191,64 @@ a particular factory:
|
|
191
191
|
f.sequence(:email) {|n| "person#{n}@example.com" }
|
192
192
|
end
|
193
193
|
|
194
|
+
== Callbacks
|
195
|
+
|
196
|
+
Factory_girl makes available three callbacks for injecting some code:
|
197
|
+
|
198
|
+
* after_build - called after a factory is built (via Factory.build)
|
199
|
+
* after_create - called after a factory is saved (via Factory.create)
|
200
|
+
* after_stub - called after a factory is stubbed (via Factory.stub)
|
201
|
+
|
202
|
+
Examples:
|
203
|
+
|
204
|
+
# Define a factory that calls the generate_hashed_password method after it is built
|
205
|
+
Factory.define :user do |u|
|
206
|
+
u.after_build { |user| do_something_to(user) }
|
207
|
+
end
|
208
|
+
|
209
|
+
Note that you'll have an instance of the user in the block. This can be useful.
|
210
|
+
|
211
|
+
You can also define multiple types of callbacks on the same factory:
|
212
|
+
|
213
|
+
Factory.define :user do |u|
|
214
|
+
u.after_build { |user| do_something_to(user) }
|
215
|
+
u.after_create { |user| do_something_else_to(user) }
|
216
|
+
end
|
217
|
+
|
218
|
+
Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
|
219
|
+
|
220
|
+
Factory.define :user do |u|
|
221
|
+
u.after_create { this_runs_first }
|
222
|
+
u.after_create { then_this }
|
223
|
+
end
|
224
|
+
|
225
|
+
Calling Factory.create will invoke both after_build and after_create callbacks.
|
226
|
+
|
227
|
+
Also, like standard attributes, child factories will inherit (and can define additional) callbacks from their parent factory.
|
228
|
+
|
194
229
|
== Alternate Syntaxes
|
195
230
|
|
196
|
-
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.
|
231
|
+
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:
|
197
232
|
|
198
|
-
|
233
|
+
require 'factory_girl/syntax/blueprint'
|
234
|
+
require 'factory_girl/syntax/make'
|
235
|
+
require 'factory_girl/syntax/sham'
|
199
236
|
|
200
|
-
|
237
|
+
Sham.email {|n| "#{n}@example.com" }
|
201
238
|
|
202
|
-
|
239
|
+
User.blueprint do
|
240
|
+
name { 'Billy Bob' }
|
241
|
+
email { Sham.email }
|
242
|
+
end
|
203
243
|
|
204
|
-
|
244
|
+
User.make(:name => 'Johnny')
|
245
|
+
|
246
|
+
== More Information
|
205
247
|
|
206
|
-
|
248
|
+
* RDoc[http://rdoc.info/projects/thoughtbot/factory_girl]
|
249
|
+
* Mailing list[http://groups.google.com/group/factory_girl]
|
250
|
+
* Issues[http://github.com/thoughtbot/factory_girl/issues]
|
251
|
+
* GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS[http://giantrobots.thoughtbot.com]
|
207
252
|
|
208
253
|
== Contributing
|
209
254
|
|
@@ -217,6 +262,7 @@ factory_girl was written by Joe Ferris with contributions from several authors,
|
|
217
262
|
* Jon Yurek
|
218
263
|
* Josh Nichols
|
219
264
|
* Josh Owens
|
265
|
+
* Nate Sutton
|
220
266
|
|
221
267
|
The syntax layers are derived from software written by the following authors:
|
222
268
|
* Pete Yandell
|
@@ -225,4 +271,4 @@ The syntax layers are derived from software written by the following authors:
|
|
225
271
|
|
226
272
|
Thanks to all members of thoughtbot for inspiration, ideas, and funding.
|
227
273
|
|
228
|
-
Copyright 2008-
|
274
|
+
Copyright 2008-2010 Joe Ferris and thoughtbot[http://www.thoughtbot.com], inc.
|
data/Rakefile
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
require 'rake/rdoctask'
|
4
|
-
require 'rake/gempackagetask'
|
5
4
|
require 'rcov/rcovtask'
|
6
5
|
require 'date'
|
7
6
|
|
@@ -36,44 +35,43 @@ task :sync_docs => 'rdoc' do
|
|
36
35
|
`rsync -ave ssh rdoc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/factory_girl`
|
37
36
|
end
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
38
|
+
begin
|
39
|
+
require 'jeweler'
|
40
|
+
Jeweler::Tasks.new do |s|
|
41
|
+
s.name = %q{factory_girl}
|
42
|
+
s.summary = %q{factory_girl provides a framework and DSL for defining and
|
43
|
+
using model instance factories.}
|
44
|
+
s.description = %q{factory_girl provides a framework and DSL for defining and
|
45
|
+
using factories - less error-prone, more explicit, and
|
46
|
+
all-around easier to work with than fixtures.}
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'spec/**/*.rb']
|
49
|
+
s.require_path = 'lib'
|
50
|
+
s.test_files = Dir[*['spec/**/*_spec.rb']]
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
s.has_rdoc = true
|
53
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
54
|
+
s.rdoc_options = ['--line-numbers', "--main", "README.rdoc"]
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
s.authors = ["Joe Ferris"]
|
57
|
+
s.email = %q{jferris@thoughtbot.com}
|
58
|
+
s.homepage = "http://thoughtbot.com/projects/factory_girl"
|
59
59
|
|
60
|
-
|
61
|
-
|
60
|
+
s.add_development_dependency('rcov')
|
61
|
+
s.add_development_dependency('rspec')
|
62
|
+
s.add_development_dependency('cucumber')
|
63
|
+
s.add_development_dependency('activerecord')
|
64
|
+
s.add_development_dependency('rr')
|
65
|
+
s.add_development_dependency('sqlite3')
|
62
66
|
|
63
|
-
|
64
|
-
|
65
|
-
|
67
|
+
s.platform = Gem::Platform::RUBY
|
68
|
+
end
|
69
|
+
rescue LoadError
|
70
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
66
71
|
end
|
67
72
|
|
68
73
|
desc "Clean files generated by rake tasks"
|
69
|
-
task :clobber => [:clobber_rdoc, :
|
70
|
-
|
71
|
-
desc "Generate a gemspec file"
|
72
|
-
task :gemspec do
|
73
|
-
File.open("#{spec.name}.gemspec", 'w') do |f|
|
74
|
-
f.write spec.to_ruby
|
75
|
-
end
|
76
|
-
end
|
74
|
+
task :clobber => [:clobber_rdoc, :clobber_rcov]
|
77
75
|
|
78
76
|
Cucumber::Rake::Task.new(:features) do |t|
|
79
77
|
t.fork = true
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.4
|
data/lib/factory_girl/factory.rb
CHANGED
@@ -7,6 +7,10 @@ class Factory
|
|
7
7
|
# Raised when a callback is defined that has an invalid name
|
8
8
|
class InvalidCallbackNameError < RuntimeError
|
9
9
|
end
|
10
|
+
|
11
|
+
# Raised when a factory is defined with the same name as a previously-defined factory.
|
12
|
+
class DuplicateDefinitionError < RuntimeError
|
13
|
+
end
|
10
14
|
|
11
15
|
class << self
|
12
16
|
attr_accessor :factories #:nodoc:
|
@@ -51,6 +55,9 @@ class Factory
|
|
51
55
|
if parent = options.delete(:parent)
|
52
56
|
instance.inherit_from(Factory.factory_by_name(parent))
|
53
57
|
end
|
58
|
+
if self.factories[instance.factory_name]
|
59
|
+
raise DuplicateDefinitionError, "Factory already defined: #{name}"
|
60
|
+
end
|
54
61
|
self.factories[instance.factory_name] = instance
|
55
62
|
end
|
56
63
|
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module FactoryGirlStepHelpers
|
2
2
|
def convert_association_string_to_instance(factory_name, assignment)
|
3
3
|
attribute, value = assignment.split(':', 2)
|
4
|
+
return if value.blank?
|
4
5
|
attributes = convert_human_hash_to_attribute_hash(attribute => value.strip)
|
5
6
|
factory = Factory.factory_by_name(factory_name)
|
6
7
|
model_class = factory.build_class
|
@@ -10,6 +10,8 @@ describe Factory do
|
|
10
10
|
stub(Factory).new { @factory }
|
11
11
|
end
|
12
12
|
|
13
|
+
after { Factory.factories.clear }
|
14
|
+
|
13
15
|
it "should create a new factory using the specified name and options" do
|
14
16
|
mock(Factory).new(@name, @options) { @factory }
|
15
17
|
Factory.define(@name, @options) {|f| }
|
@@ -28,9 +30,17 @@ describe Factory do
|
|
28
30
|
@factory.should == Factory.factories[@name]
|
29
31
|
end
|
30
32
|
|
31
|
-
it "should allow
|
33
|
+
it "should allow a factory to be found by name" do
|
34
|
+
Factory.define(@name) {|f| }
|
32
35
|
Factory.factory_by_name(@name).should == @factory
|
33
36
|
end
|
37
|
+
|
38
|
+
it "should not allow a duplicate factory definition" do
|
39
|
+
lambda {
|
40
|
+
2.times { Factory.define(@name) {|f| } }
|
41
|
+
}.should raise_error(Factory::DuplicateDefinitionError)
|
42
|
+
end
|
43
|
+
|
34
44
|
end
|
35
45
|
|
36
46
|
describe "a factory" do
|
@@ -420,6 +430,8 @@ describe Factory do
|
|
420
430
|
end
|
421
431
|
end
|
422
432
|
|
433
|
+
after { Factory.factories.clear }
|
434
|
+
|
423
435
|
it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
|
424
436
|
lambda {
|
425
437
|
Factory.define(:child, :parent => :nonexsitent) {}
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_girl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 2
|
8
|
+
- 4
|
9
|
+
version: 1.2.4
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Joe Ferris
|
@@ -9,14 +14,85 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-03-30 00:00:00 -04:00
|
13
18
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rcov
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rspec
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :development
|
43
|
+
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: cucumber
|
46
|
+
prerelease: false
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
type: :development
|
55
|
+
version_requirements: *id003
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: activerecord
|
58
|
+
prerelease: false
|
59
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id004
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rr
|
70
|
+
prerelease: false
|
71
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id005
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: sqlite3
|
82
|
+
prerelease: false
|
83
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
type: :development
|
91
|
+
version_requirements: *id006
|
16
92
|
description: |-
|
17
93
|
factory_girl provides a framework and DSL for defining and
|
18
|
-
|
19
|
-
|
94
|
+
using factories - less error-prone, more explicit, and
|
95
|
+
all-around easier to work with than fixtures.
|
20
96
|
email: jferris@thoughtbot.com
|
21
97
|
executables: []
|
22
98
|
|
@@ -25,31 +101,32 @@ extensions: []
|
|
25
101
|
extra_rdoc_files:
|
26
102
|
- README.rdoc
|
27
103
|
files:
|
28
|
-
- Changelog
|
29
104
|
- CONTRIBUTION_GUIDELINES.rdoc
|
105
|
+
- Changelog
|
30
106
|
- LICENSE
|
31
|
-
- Rakefile
|
32
107
|
- README.rdoc
|
108
|
+
- Rakefile
|
109
|
+
- VERSION
|
110
|
+
- lib/factory_girl.rb
|
33
111
|
- lib/factory_girl/aliases.rb
|
112
|
+
- lib/factory_girl/attribute.rb
|
34
113
|
- lib/factory_girl/attribute/association.rb
|
35
114
|
- lib/factory_girl/attribute/callback.rb
|
36
115
|
- lib/factory_girl/attribute/dynamic.rb
|
37
116
|
- lib/factory_girl/attribute/static.rb
|
38
|
-
- lib/factory_girl/attribute.rb
|
39
117
|
- lib/factory_girl/factory.rb
|
118
|
+
- lib/factory_girl/proxy.rb
|
40
119
|
- lib/factory_girl/proxy/attributes_for.rb
|
41
120
|
- lib/factory_girl/proxy/build.rb
|
42
121
|
- lib/factory_girl/proxy/create.rb
|
43
122
|
- lib/factory_girl/proxy/stub.rb
|
44
|
-
- lib/factory_girl/proxy.rb
|
45
123
|
- lib/factory_girl/sequence.rb
|
46
124
|
- lib/factory_girl/step_definitions.rb
|
125
|
+
- lib/factory_girl/syntax.rb
|
47
126
|
- lib/factory_girl/syntax/blueprint.rb
|
48
127
|
- lib/factory_girl/syntax/generate.rb
|
49
128
|
- lib/factory_girl/syntax/make.rb
|
50
129
|
- lib/factory_girl/syntax/sham.rb
|
51
|
-
- lib/factory_girl/syntax.rb
|
52
|
-
- lib/factory_girl.rb
|
53
130
|
- spec/factory_girl/aliases_spec.rb
|
54
131
|
- spec/factory_girl/attribute/association_spec.rb
|
55
132
|
- spec/factory_girl/attribute/callback_spec.rb
|
@@ -85,18 +162,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
162
|
requirements:
|
86
163
|
- - ">="
|
87
164
|
- !ruby/object:Gem::Version
|
165
|
+
segments:
|
166
|
+
- 0
|
88
167
|
version: "0"
|
89
|
-
version:
|
90
168
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
169
|
requirements:
|
92
170
|
- - ">="
|
93
171
|
- !ruby/object:Gem::Version
|
172
|
+
segments:
|
173
|
+
- 0
|
94
174
|
version: "0"
|
95
|
-
version:
|
96
175
|
requirements: []
|
97
176
|
|
98
177
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.3.
|
178
|
+
rubygems_version: 1.3.6
|
100
179
|
signing_key:
|
101
180
|
specification_version: 3
|
102
181
|
summary: factory_girl provides a framework and DSL for defining and using model instance factories.
|