vitalish-factory_girl 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +282 -0
  5. data/Rakefile +66 -0
  6. data/lib/factory_girl.rb +24 -0
  7. data/lib/factory_girl/aliases.rb +21 -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 +215 -0
  14. data/lib/factory_girl/proxy.rb +77 -0
  15. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  16. data/lib/factory_girl/proxy/build.rb +32 -0
  17. data/lib/factory_girl/proxy/create.rb +12 -0
  18. data/lib/factory_girl/proxy/stub.rb +64 -0
  19. data/lib/factory_girl/sequence.rb +28 -0
  20. data/lib/factory_girl/step_definitions.rb +60 -0
  21. data/lib/factory_girl/syntax.rb +12 -0
  22. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  23. data/lib/factory_girl/syntax/generate.rb +73 -0
  24. data/lib/factory_girl/syntax/make.rb +41 -0
  25. data/lib/factory_girl/syntax/sham.rb +45 -0
  26. data/spec/factory_girl/aliases_spec.rb +33 -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 +60 -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 +411 -0
  33. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  34. data/spec/factory_girl/proxy/build_spec.rb +86 -0
  35. data/spec/factory_girl/proxy/create_spec.rb +99 -0
  36. data/spec/factory_girl/proxy/stub_spec.rb +80 -0
  37. data/spec/factory_girl/proxy_spec.rb +84 -0
  38. data/spec/factory_girl/sequence_spec.rb +43 -0
  39. data/spec/spec_helper.rb +93 -0
  40. metadata +119 -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.
data/Changelog ADDED
@@ -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.
data/README.rdoc ADDED
@@ -0,0 +1,282 @@
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 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.
data/Rakefile ADDED
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rake'
4
+ require 'rake/rdoctask'
5
+ require 'rcov/rcovtask'
6
+ require 'date'
7
+ require 'rake/gempackagetask'
8
+ require 'rspec/core/rake_task'
9
+ require 'cucumber/rake/task'
10
+ require 'appraisal'
11
+
12
+ desc 'Default: run the specs and features.'
13
+ task :default => 'spec:unit' do
14
+ system("rake -s appraisal spec:acceptance features;")
15
+ end
16
+
17
+ namespace :spec do
18
+ desc "Run unit specs"
19
+ RSpec::Core::RakeTask.new('unit') do |t|
20
+ t.pattern = 'spec/factory_girl/**/*_spec.rb'
21
+ end
22
+
23
+ desc "Run acceptance specs"
24
+ RSpec::Core::RakeTask.new('acceptance') do |t|
25
+ t.pattern = 'spec/acceptance/**/*_spec.rb'
26
+ end
27
+ end
28
+
29
+ desc "Run the unit and acceptance specs"
30
+ task :spec => ['spec:unit', 'spec:acceptance']
31
+
32
+ desc 'Performs code coverage on the factory_girl plugin.'
33
+ Rcov::RcovTask.new do |t|
34
+ t.test_files = FileList['spec/*_spec.rb']
35
+ t.verbose = true
36
+ end
37
+
38
+ desc 'Generate documentation for the factory_girl plugin.'
39
+ Rake::RDocTask.new(:rdoc) do |rdoc|
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = 'Factory Girl'
42
+ rdoc.options << '--line-numbers' << "--main" << "README.rdoc"
43
+ rdoc.rdoc_files.include('README.rdoc')
44
+ rdoc.rdoc_files.include('CONTRIBUTION_GUIDELINES.rdoc')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
47
+
48
+ desc 'Update documentation on website'
49
+ task :sync_docs => 'rdoc' do
50
+ `rsync -ave ssh rdoc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/factory_girl`
51
+ end
52
+
53
+ desc "Clean files generated by rake tasks"
54
+ task :clobber => [:clobber_rdoc, :clobber_rcov]
55
+
56
+ Cucumber::Rake::Task.new(:features) do |t|
57
+ t.fork = true
58
+ t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
59
+ end
60
+
61
+ eval("$specification = begin; #{IO.read('factory_girl.gemspec')}; end")
62
+ Rake::GemPackageTask.new($specification) do |package|
63
+ package.need_zip = true
64
+ package.need_tar = true
65
+ end
66
+
@@ -0,0 +1,24 @@
1
+ require 'factory_girl/proxy'
2
+ require 'factory_girl/proxy/build'
3
+ require 'factory_girl/proxy/create'
4
+ require 'factory_girl/proxy/attributes_for'
5
+ require 'factory_girl/proxy/stub'
6
+ require 'factory_girl/factory'
7
+ require 'factory_girl/attribute'
8
+ require 'factory_girl/attribute/static'
9
+ require 'factory_girl/attribute/dynamic'
10
+ require 'factory_girl/attribute/association'
11
+ require 'factory_girl/attribute/callback'
12
+ require 'factory_girl/sequence'
13
+ require 'factory_girl/aliases'
14
+ require 'factory_girl/definition_proxy'
15
+ require 'factory_girl/syntax/default'
16
+ require 'factory_girl/syntax/vintage'
17
+ require 'factory_girl/find_definitions'
18
+ require 'factory_girl/deprecated'
19
+ require 'factory_girl/version'
20
+
21
+ if defined?(Rails) && Rails::VERSION::MAJOR == 2
22
+ require 'factory_girl/rails2'
23
+ end
24
+
@@ -0,0 +1,21 @@
1
+ module FactoryGirl
2
+
3
+ class << self
4
+ attr_accessor :aliases #:nodoc:
5
+ end
6
+ self.aliases = [
7
+ [/(.+)_id/, '\1'],
8
+ [/(.*)/, '\1_id']
9
+ ]
10
+
11
+ def self.aliases_for(attribute) #:nodoc:
12
+ aliases.collect do |params|
13
+ pattern, replace = *params
14
+ if pattern.match(attribute.to_s)
15
+ attribute.to_s.sub(pattern, replace).to_sym
16
+ else
17
+ nil
18
+ end
19
+ end.compact << attribute
20
+ end
21
+ end