dm-factory_girl 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +286 -0
  5. data/Rakefile +81 -0
  6. data/lib/factory_girl.rb +35 -0
  7. data/lib/factory_girl/aliases.rb +50 -0
  8. data/lib/factory_girl/attribute.rb +29 -0
  9. data/lib/factory_girl/attribute/association.rb +20 -0
  10. data/lib/factory_girl/attribute/callback.rb +16 -0
  11. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  12. data/lib/factory_girl/attribute/static.rb +17 -0
  13. data/lib/factory_girl/factory.rb +395 -0
  14. data/lib/factory_girl/proxy.rb +79 -0
  15. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  16. data/lib/factory_girl/proxy/build.rb +30 -0
  17. data/lib/factory_girl/proxy/create.rb +18 -0
  18. data/lib/factory_girl/proxy/stub.rb +50 -0
  19. data/lib/factory_girl/sequence.rb +63 -0
  20. data/lib/factory_girl/step_definitions.rb +54 -0
  21. data/lib/factory_girl/syntax.rb +12 -0
  22. data/lib/factory_girl/syntax/blueprint.rb +45 -0
  23. data/lib/factory_girl/syntax/generate.rb +73 -0
  24. data/lib/factory_girl/syntax/make.rb +43 -0
  25. data/lib/factory_girl/syntax/sham.rb +42 -0
  26. data/spec/factory_girl/aliases_spec.rb +29 -0
  27. data/spec/factory_girl/attribute/association_spec.rb +29 -0
  28. data/spec/factory_girl/attribute/callback_spec.rb +23 -0
  29. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  30. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  31. data/spec/factory_girl/attribute_spec.rb +30 -0
  32. data/spec/factory_girl/factory_spec.rb +571 -0
  33. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  34. data/spec/factory_girl/proxy/build_spec.rb +81 -0
  35. data/spec/factory_girl/proxy/create_spec.rb +94 -0
  36. data/spec/factory_girl/proxy/stub_spec.rb +79 -0
  37. data/spec/factory_girl/proxy_spec.rb +84 -0
  38. data/spec/factory_girl/sequence_spec.rb +66 -0
  39. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  40. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  41. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  42. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  43. data/spec/integration_spec.rb +305 -0
  44. data/spec/models.rb +43 -0
  45. data/spec/spec_helper.rb +30 -0
  46. metadata +123 -0
@@ -0,0 +1,9 @@
1
+ We're using GitHub[http://github.com/thoughtbot/factory_girl/tree/master], and we've been getting any combination of github pull requests, patches, emails, etc. We need to normalize this workflow to make sure we don't miss any fixes.
2
+
3
+ * Make sure you're accessing the source from the {official repository}[http://github.com/thoughtbot/factory_girl/tree/master].
4
+ * Please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier.
5
+ * If you're submitting patches, please cut each fix or feature into a separate patch.
6
+ * There should be an issue[http://github.com/thoughtbot/factory_girl/issues] for any submission. If you've found a bug and want to fix it, open a new ticket at the same time.
7
+ * Please <b>don't send pull requests</b> Just update the issue with the url for your fix when it's ready. The github pull requests pretty much get dropped on the floor until someone with commit rights notices them in the mailbox.
8
+ * Contributions without tests won't be accepted.
9
+ * Please don't submit patches or branches that update the Gem version.
@@ -0,0 +1,29 @@
1
+ 1.1.4 (November 28, 2008)
2
+ Factory.build now uses Factory.create for associations of the built object
3
+ Factory definitions are now detected in subdirectories, such as
4
+ factories/person_factory.rb (thanks to Josh Nichols)
5
+ Factory definitions are now loaded after the environment in a Rails project
6
+ (fixes some issues with dependencies being loaded too early) (thanks to
7
+ Josh Nichols)
8
+ Factory names ending in 's' no longer cause problems (thanks to Alex Sharp
9
+ and Josh Owens)
10
+
11
+ 1.1.3 (September 12, 2008)
12
+ Automatically pull in definitions from factories.rb, test/factories.rb, or
13
+ spec/factories.rb
14
+ 1.1.2 (July 30, 2008)
15
+ Improved error handling for invalid and undefined factories/attributes
16
+ Improved handling of strings vs symbols vs classes
17
+ Added a prettier syntax for handling associations
18
+ Updated documentation and fixed compatibility with Rails 2.1
19
+
20
+ 1.1.1 (June 23, 2008)
21
+ The attribute "name" no longer requires using #add_attribute
22
+
23
+ 1.1.0 (June 3, 2008)
24
+ Added support for dependent attributes
25
+ Fixed the attributes_for build strategy to not build associations
26
+ Added support for sequences
27
+
28
+ 1.0.0 (May 31, 208)
29
+ First version
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008 Joe Ferris and thoughtbot, inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,286 @@
1
+ = factory_girl
2
+
3
+ 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.
4
+
5
+ == This Version
6
+
7
+ This is not the official branch of DataMapper
8
+
9
+ This version of factory_girl maintains compatibility with Datamapper. By using the interface common to both ActiveRecord and DataMapper this gem can be used in both places. This version is maintained by John F. Miller sence Sept. 25, 2009.
10
+
11
+ Github: http://github.com/startrader/factory_girl/tree/master
12
+
13
+ To run tests with DataMapper us `rake DATAMAPPER=true`
14
+
15
+ == Download
16
+
17
+ Github: http://github.com/thoughtbot/factory_girl/tree/master
18
+
19
+ Gem:
20
+ gem install thoughtbot-factory_girl --source http://gems.github.com
21
+
22
+ Note: if you install factory_girl using the gem from Github, you'll need this
23
+ in your environment.rb if you want to use Rails 2.1+'s dependency manager:
24
+
25
+ config.gem "thoughtbot-factory_girl",
26
+ :lib => "factory_girl",
27
+ :source => "http://gems.github.com"
28
+
29
+ == Defining factories
30
+
31
+ 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:
32
+
33
+ # This will guess the User class
34
+ Factory.define :user do |u|
35
+ u.first_name 'John'
36
+ u.last_name 'Doe'
37
+ u.admin false
38
+ end
39
+
40
+ # This will use the User class (Admin would have been guessed)
41
+ Factory.define :admin, :class => User do |u|
42
+ u.first_name 'Admin'
43
+ u.last_name 'User'
44
+ u.admin true
45
+ end
46
+
47
+ # The same, but using a string instead of class constant
48
+ Factory.define :admin, :class => 'user' do |u|
49
+ u.first_name 'Admin'
50
+ u.last_name 'User'
51
+ u.admin true
52
+ end
53
+
54
+ 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.
55
+
56
+ Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:
57
+
58
+ test/factories.rb
59
+ spec/factories.rb
60
+ test/factories/*.rb
61
+ spec/factories/*.rb
62
+
63
+ == Using factories
64
+
65
+ factory_girl supports several different build strategies: build, create, attributes_for and stub:
66
+
67
+ # Returns a User instance that's not saved
68
+ user = Factory.build(:user)
69
+
70
+ # Returns a saved User instance
71
+ user = Factory.create(:user)
72
+
73
+ # Returns a hash of attributes that can be used to build a User instance:
74
+ attrs = Factory.attributes_for(:user)
75
+
76
+ # Returns an object with all defined attributes stubbed out:
77
+ stub = Factory.stub(:user)
78
+
79
+ You can use the Factory method as a shortcut for the default build strategy:
80
+
81
+ # Same as Factory.create :user:
82
+ user = Factory(:user)
83
+
84
+ The default strategy can be overriden:
85
+
86
+ # Now same as Factory.build(:user)
87
+ Factory.define :user, :default_strategy => :build do |u|
88
+ ...
89
+ end
90
+
91
+ user = Factory(:user)
92
+
93
+ No matter which startegy is used, it's possible to override the defined attributes by passing a hash:
94
+
95
+ # Build a User instance and override the first_name property
96
+ user = Factory.build(:user, :first_name => 'Joe')
97
+ user.first_name
98
+ # => "Joe"
99
+
100
+ == Lazy Attributes
101
+
102
+ 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:
103
+
104
+ Factory.define :user do |u|
105
+ # ...
106
+ u.activation_code { User.generate_activation_code }
107
+ end
108
+
109
+ == Dependent Attributes
110
+
111
+ Attributes can be based on the values of other attributes using the proxy that is yieled to lazy attribute blocks:
112
+
113
+ Factory.define :user do |u|
114
+ u.first_name 'Joe'
115
+ u.last_name 'Blow'
116
+ u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
117
+ end
118
+
119
+ Factory(:user, :last_name => 'Doe').email
120
+ # => "joe.doe@example.com"
121
+
122
+ == Associations
123
+
124
+ Associated instances can be generated by using the association method when
125
+ defining a lazy attribute:
126
+
127
+ Factory.define :post do |p|
128
+ # ...
129
+ p.author {|author| author.association(:user, :last_name => 'Writely') }
130
+ end
131
+
132
+ The behavior of the association method varies depending on the build strategy used for the parent object.
133
+
134
+ # Builds and saves a User and a Post
135
+ post = Factory(:post)
136
+ post.new_record? # => false
137
+ post.author.new_record # => false
138
+
139
+ # Builds and saves a User, and then builds but does not save a Post
140
+ Factory.build(:post)
141
+ post.new_record? # => true
142
+ post.author.new_record # => false
143
+
144
+ Because this pattern is so common, a prettier syntax is available for defining
145
+ associations:
146
+
147
+ # The following definitions are equivilent:
148
+ Factory.define :post do |p|
149
+ p.author {|a| a.association(:user) }
150
+ end
151
+
152
+ Factory.define :post do |p|
153
+ p.association :author, :factory => :user
154
+ end
155
+
156
+ If the factory name is the same as the association name, the factory name can
157
+ be left out.
158
+
159
+ == Inheritance
160
+
161
+ You can easily create multiple factories for the same class without repeating common attributes by using inheritance:
162
+
163
+ Factory.define :post do |p|
164
+ # the 'title' attribute is required for all posts
165
+ p.title 'A title'
166
+ end
167
+
168
+ Factory.define :approved_post, :parent => :post do |p|
169
+ p.approved true
170
+ # the 'approver' association is required for an approved post
171
+ p.association :approver, :factory => :user
172
+ end
173
+
174
+ == Sequences
175
+
176
+ Unique values in a specific format (for example, e-mail addresses) can be
177
+ generated using sequences. Sequences are defined by calling Factory.sequence,
178
+ and values in a sequence are generated by calling Factory.next:
179
+
180
+ # Defines a new sequence
181
+ Factory.sequence :email do |n|
182
+ "person#{n}@example.com"
183
+ end
184
+
185
+ Factory.next :email
186
+ # => "person1@example.com"
187
+
188
+ Factory.next :email
189
+ # => "person2@example.com"
190
+
191
+ Sequences can be used in lazy attributes:
192
+
193
+ Factory.define :user do |f|
194
+ f.email { Factory.next(:email) }
195
+ end
196
+
197
+ And it's also possible to define an in-line sequence that is only used in
198
+ a particular factory:
199
+
200
+ Factory.define :user do |f|
201
+ f.sequence(:email) {|n| "person#{n}@example.com" }
202
+ end
203
+
204
+ == Callbacks
205
+
206
+ Factory_girl makes available three callbacks to which you can pass blocks to be executed:
207
+
208
+ * after_build - called after a factory is built (via Factory.build)
209
+ * after_create - called after a factory is saved (via Factory.create)
210
+ * after_stub - called after a factory is stubbed (via Factory.stub)
211
+
212
+ Examples:
213
+
214
+ # Define a factory that calls the generate_hashed_password method after it is built
215
+ Factory.define :user do |u|
216
+ u.after_build { generate_hashed_password }
217
+ end
218
+
219
+ # Define a factory that calls the send_welcome_email method after it is created
220
+ Factory.define :user do |u|
221
+ u.after_create { send_welcome_email }
222
+ end
223
+
224
+ You can also define multiple types of callbacks on the same factory:
225
+
226
+ # Define a factory that calls the generate_hashed_password method after it is built, and send_welcome_email
227
+ # after it is saved.
228
+ Factory.define :user do |u|
229
+ u.after_build { generate_hashed_password }
230
+ u.after_create { send_welcome_email }
231
+ end
232
+
233
+ Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
234
+
235
+ # Define a factory which calls send_welcome_email and then set_default_preferences after it is created
236
+ Factory.define :user do |u|
237
+ u.after_create { send_welcome_email }
238
+ u.after_create { set_default_preferences }
239
+ end
240
+
241
+ If you provide a block parameter, the instance of that factory will be passed in:
242
+
243
+ # Define a factory with a callback that uses an instance of that class
244
+ Factory.define :user_updated_yesterday do |u|
245
+ u.after_build { |user_instance| user_instance.updated_at = Date.today - 1.day }
246
+ end
247
+
248
+ Note: Factory.create will invoke both after_build and after_create callbacks.
249
+
250
+ Also, like standard attributes, child factories will inherit (and can define additional) callbacks from their parent factory.
251
+
252
+ == Alternate Syntaxes
253
+
254
+ 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.
255
+
256
+ == More Information
257
+
258
+ Our blog: http://giantrobots.thoughtbot.com
259
+
260
+ factory_girl rdoc: http://rdoc.info/projects/thoughtbot/factory_girl
261
+
262
+ Mailing list: http://groups.google.com/group/factory_girl
263
+
264
+ factory_girl issues: http://github.com/thoughtbot/factory_girl/issues
265
+
266
+ == Contributing
267
+
268
+ Please read the contribution guidelines before submitting patches or pull requests.
269
+
270
+ == Author
271
+
272
+ factory_girl was written by Joe Ferris with contributions from several authors, including:
273
+ * Alex Sharp
274
+ * Eugene Bolshakov
275
+ * Jon Yurek
276
+ * Josh Nichols
277
+ * Josh Owens
278
+
279
+ The syntax layers are derived from software written by the following authors:
280
+ * Pete Yandell
281
+ * Rick Bradley
282
+ * Yossef Mendelssohn
283
+
284
+ Thanks to all members of thoughtbot for inspiration, ideas, and funding.
285
+
286
+ Copyright 2008-2009 Joe Ferris and thoughtbot[http://www.thoughtbot.com], inc.
@@ -0,0 +1,81 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+ require 'rcov/rcovtask'
6
+ require 'date'
7
+
8
+ require 'spec/rake/spectask'
9
+ require 'cucumber/rake/task'
10
+
11
+ desc 'Default: run the specs and features.'
12
+ task :default => [:spec, :features]
13
+
14
+ Spec::Rake::SpecTask.new do |t|
15
+ t.spec_opts = ['--options', "spec/spec.opts"]
16
+ end
17
+
18
+ desc 'Performs code coverage on the factory_girl plugin.'
19
+ Rcov::RcovTask.new do |t|
20
+ t.test_files = FileList['spec/*_spec.rb']
21
+ t.verbose = true
22
+ end
23
+
24
+ desc 'Generate documentation for the factory_girl plugin.'
25
+ Rake::RDocTask.new(:rdoc) do |rdoc|
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = 'Factory Girl'
28
+ rdoc.options << '--line-numbers' << "--main" << "README.rdoc"
29
+ rdoc.rdoc_files.include('README.rdoc')
30
+ rdoc.rdoc_files.include('CONTRIBUTION_GUIDELINES.rdoc')
31
+ rdoc.rdoc_files.include('lib/**/*.rb')
32
+ end
33
+
34
+ desc 'Update documentation on website'
35
+ task :sync_docs => 'rdoc' do
36
+ `rsync -ave ssh rdoc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/factory_girl`
37
+ end
38
+
39
+ spec = Gem::Specification.new do |s|
40
+ s.name = %q{factory_girl}
41
+ s.version = "1.2.3"
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
+
48
+ s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'spec/**/*.rb']
49
+ s.require_path = 'lib'
50
+ s.test_files = Dir[*['spec/**/*_spec.rb']]
51
+
52
+ s.has_rdoc = true
53
+ s.extra_rdoc_files = ["README.rdoc"]
54
+ s.rdoc_options = ['--line-numbers', "--main", "README.rdoc"]
55
+
56
+ s.authors = ["Joe Ferris"]
57
+ s.email = %q{jferris@thoughtbot.com}
58
+ s.homepage = "http://thoughtbot.com/projects/factory_girl"
59
+
60
+ s.platform = Gem::Platform::RUBY
61
+ end
62
+
63
+ Rake::GemPackageTask.new spec do |pkg|
64
+ pkg.need_tar = true
65
+ pkg.need_zip = true
66
+ end
67
+
68
+ desc "Clean files generated by rake tasks"
69
+ task :clobber => [:clobber_rdoc, :clobber_package]
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
77
+
78
+ Cucumber::Rake::Task.new(:features) do |t|
79
+ t.fork = true
80
+ t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
81
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_support'
2
+ require 'factory_girl/proxy'
3
+ require 'factory_girl/proxy/build'
4
+ require 'factory_girl/proxy/create'
5
+ require 'factory_girl/proxy/attributes_for'
6
+ require 'factory_girl/proxy/stub'
7
+ require 'factory_girl/factory'
8
+ require 'factory_girl/attribute'
9
+ require 'factory_girl/attribute/static'
10
+ require 'factory_girl/attribute/dynamic'
11
+ require 'factory_girl/attribute/association'
12
+ require 'factory_girl/attribute/callback'
13
+ require 'factory_girl/sequence'
14
+ require 'factory_girl/aliases'
15
+
16
+ # Shortcut for Factory.default_strategy.
17
+ #
18
+ # Example:
19
+ # Factory(:user, :name => 'Joe')
20
+ def Factory (name, attrs = {})
21
+ Factory.default_strategy(name, attrs)
22
+ end
23
+
24
+ if defined? Rails.configuration
25
+ Rails.configuration.after_initialize do
26
+ Factory.definition_file_paths = [
27
+ File.join(RAILS_ROOT, 'test', 'factories'),
28
+ File.join(RAILS_ROOT, 'spec', 'factories')
29
+ ]
30
+ Factory.find_definitions
31
+ end
32
+ else
33
+ Factory.find_definitions
34
+ end
35
+