jeffrafter-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 +265 -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 +430 -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 +12 -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 +42 -0
  23. data/lib/factory_girl/syntax/generate.rb +68 -0
  24. data/lib/factory_girl/syntax/make.rb +39 -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 +605 -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 +304 -0
  44. data/spec/models.rb +43 -0
  45. data/spec/spec_helper.rb +18 -0
  46. metadata +128 -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,265 @@
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
+ == Download
6
+
7
+ Github: http://github.com/thoughtbot/factory_girl/tree/master
8
+
9
+ Gem:
10
+ gem install factory_girl --source http://gemcutter.org
11
+
12
+ Note: if you install factory_girl using the gem from Gemcutter, you'll need this
13
+ in your environment.rb if you want to use Rails 2.1+'s dependency manager:
14
+
15
+ config.gem "factory_girl",
16
+ :source => "http://gemcutter.org"
17
+
18
+ == Defining factories
19
+
20
+ 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:
21
+
22
+ # This will guess the User class
23
+ Factory.define :user do |u|
24
+ u.first_name 'John'
25
+ u.last_name 'Doe'
26
+ u.admin false
27
+ end
28
+
29
+ # This will use the User class (Admin would have been guessed)
30
+ Factory.define :admin, :class => User do |u|
31
+ u.first_name 'Admin'
32
+ u.last_name 'User'
33
+ u.admin true
34
+ end
35
+
36
+ # The same, but using a string instead of class constant
37
+ Factory.define :admin, :class => 'user' do |u|
38
+ u.first_name 'Admin'
39
+ u.last_name 'User'
40
+ u.admin true
41
+ end
42
+
43
+ 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.
44
+
45
+ Attempting to define multiple factories with the same name will raise an error.
46
+
47
+ Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:
48
+
49
+ test/factories.rb
50
+ spec/factories.rb
51
+ test/factories/*.rb
52
+ spec/factories/*.rb
53
+
54
+ == Using factories
55
+
56
+ factory_girl supports several different build strategies: build, create, attributes_for and stub:
57
+
58
+ # Returns a User instance that's not saved
59
+ user = Factory.build(:user)
60
+
61
+ # Returns a saved User instance
62
+ user = Factory.create(:user)
63
+
64
+ # Returns a hash of attributes that can be used to build a User instance:
65
+ attrs = Factory.attributes_for(:user)
66
+
67
+ # Returns an object with all defined attributes stubbed out:
68
+ stub = Factory.stub(:user)
69
+
70
+ You can use the Factory method as a shortcut for the default build strategy:
71
+
72
+ # Same as Factory.create :user:
73
+ user = Factory(:user)
74
+
75
+ The default strategy can be overriden:
76
+
77
+ # Now same as Factory.build(:user)
78
+ Factory.define :user, :default_strategy => :build do |u|
79
+ ...
80
+ end
81
+
82
+ user = Factory(:user)
83
+
84
+ No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
85
+
86
+ # Build a User instance and override the first_name property
87
+ user = Factory.build(:user, :first_name => 'Joe')
88
+ user.first_name
89
+ # => "Joe"
90
+
91
+ == Lazy Attributes
92
+
93
+ 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:
94
+
95
+ Factory.define :user do |u|
96
+ # ...
97
+ u.activation_code { User.generate_activation_code }
98
+ end
99
+
100
+ == Dependent Attributes
101
+
102
+ Attributes can be based on the values of other attributes using the proxy that is yieled to lazy attribute blocks:
103
+
104
+ Factory.define :user do |u|
105
+ u.first_name 'Joe'
106
+ u.last_name 'Blow'
107
+ u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
108
+ end
109
+
110
+ Factory(:user, :last_name => 'Doe').email
111
+ # => "joe.doe@example.com"
112
+
113
+ == Associations
114
+
115
+ Associated instances can be generated by using the association method when
116
+ defining a lazy attribute:
117
+
118
+ Factory.define :post do |p|
119
+ # ...
120
+ p.author {|author| author.association(:user, :last_name => 'Writely') }
121
+ end
122
+
123
+ The behavior of the association method varies depending on the build strategy used for the parent object.
124
+
125
+ # Builds and saves a User and a Post
126
+ post = Factory(:post)
127
+ post.new_record? # => false
128
+ post.author.new_record # => false
129
+
130
+ # Builds and saves a User, and then builds but does not save a Post
131
+ Factory.build(:post)
132
+ post.new_record? # => true
133
+ post.author.new_record # => false
134
+
135
+ Because this pattern is so common, a prettier syntax is available for defining
136
+ associations:
137
+
138
+ # The following definitions are equivilent:
139
+ Factory.define :post do |p|
140
+ p.author {|a| a.association(:user) }
141
+ end
142
+
143
+ Factory.define :post do |p|
144
+ p.association :author, :factory => :user
145
+ end
146
+
147
+ If the factory name is the same as the association name, the factory name can
148
+ be left out.
149
+
150
+ == Inheritance
151
+
152
+ You can easily create multiple factories for the same class without repeating common attributes by using inheritance:
153
+
154
+ Factory.define :post do |p|
155
+ # the 'title' attribute is required for all posts
156
+ p.title 'A title'
157
+ end
158
+
159
+ Factory.define :approved_post, :parent => :post do |p|
160
+ p.approved true
161
+ # the 'approver' association is required for an approved post
162
+ p.association :approver, :factory => :user
163
+ end
164
+
165
+ == Sequences
166
+
167
+ Unique values in a specific format (for example, e-mail addresses) can be
168
+ generated using sequences. Sequences are defined by calling Factory.sequence,
169
+ and values in a sequence are generated by calling Factory.next:
170
+
171
+ # Defines a new sequence
172
+ Factory.sequence :email do |n|
173
+ "person#{n}@example.com"
174
+ end
175
+
176
+ Factory.next :email
177
+ # => "person1@example.com"
178
+
179
+ Factory.next :email
180
+ # => "person2@example.com"
181
+
182
+ Sequences can be used in lazy attributes:
183
+
184
+ Factory.define :user do |f|
185
+ f.email { Factory.next(:email) }
186
+ end
187
+
188
+ And it's also possible to define an in-line sequence that is only used in
189
+ a particular factory:
190
+
191
+ Factory.define :user do |f|
192
+ f.sequence(:email) {|n| "person#{n}@example.com" }
193
+ end
194
+
195
+ == Callbacks
196
+
197
+ Factory_girl makes available three callbacks for injecting some code:
198
+
199
+ * after_build - called after a factory is built (via Factory.build)
200
+ * after_create - called after a factory is saved (via Factory.create)
201
+ * after_stub - called after a factory is stubbed (via Factory.stub)
202
+
203
+ Examples:
204
+
205
+ # Define a factory that calls the generate_hashed_password method after it is built
206
+ Factory.define :user do |u|
207
+ u.after_build { |user| do_something_to(user) }
208
+ end
209
+
210
+ Note that you'll have an instance of the user in the block. This can be useful.
211
+
212
+ You can also define multiple types of callbacks on the same factory:
213
+
214
+ Factory.define :user do |u|
215
+ u.after_build { |user| do_something_to(user) }
216
+ u.after_create { |user| do_something_else_to(user) }
217
+ end
218
+
219
+ Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
220
+
221
+ Factory.define :user do |u|
222
+ u.after_create { this_runs_first }
223
+ u.after_create { then_this }
224
+ end
225
+
226
+ Calling Factory.create will invoke both after_build and after_create callbacks.
227
+
228
+ Also, like standard attributes, child factories will inherit (and can define additional) callbacks from their parent factory.
229
+
230
+ == Alternate Syntaxes
231
+
232
+ 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.
233
+
234
+ == More Information
235
+
236
+ Our blog: http://giantrobots.thoughtbot.com
237
+
238
+ factory_girl rdoc: http://rdoc.info/projects/thoughtbot/factory_girl
239
+
240
+ Mailing list: http://groups.google.com/group/factory_girl
241
+
242
+ factory_girl issues: http://github.com/thoughtbot/factory_girl/issues
243
+
244
+ == Contributing
245
+
246
+ Please read the contribution guidelines before submitting patches or pull requests.
247
+
248
+ == Author
249
+
250
+ factory_girl was written by Joe Ferris with contributions from several authors, including:
251
+ * Alex Sharp
252
+ * Eugene Bolshakov
253
+ * Jon Yurek
254
+ * Josh Nichols
255
+ * Josh Owens
256
+ * Nate Sutton
257
+
258
+ The syntax layers are derived from software written by the following authors:
259
+ * Pete Yandell
260
+ * Rick Bradley
261
+ * Yossef Mendelssohn
262
+
263
+ Thanks to all members of thoughtbot for inspiration, ideas, and funding.
264
+
265
+ Copyright 2008-2009 Joe Ferris and thoughtbot[http://www.thoughtbot.com], inc.
data/Rakefile ADDED
@@ -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{jeffrafter-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
+
@@ -0,0 +1,50 @@
1
+ class Factory
2
+
3
+ class << self
4
+ attr_accessor :aliases #:nodoc:
5
+ end
6
+ self.aliases = [
7
+ [/(.*)_id/, '\1'],
8
+ [/(.*)/, '\1_id']
9
+ ]
10
+
11
+ # Defines a new alias for attributes.
12
+ #
13
+ # Arguments:
14
+ # * pattern: +Regexp+
15
+ # A pattern that will be matched against attributes when looking for
16
+ # aliases. Contents captured in the pattern can be used in the alias.
17
+ # * replace: +String+
18
+ # The alias that results from the matched pattern. Captured strings can
19
+ # be substituded like with +String#sub+.
20
+ #
21
+ # Example:
22
+ #
23
+ # Factory.alias /(.*)_confirmation/, '\1'
24
+ #
25
+ # factory_girl starts with aliases for foreign keys, so that a :user
26
+ # association can be overridden by a :user_id parameter:
27
+ #
28
+ # Factory.define :post do |p|
29
+ # p.association :user
30
+ # end
31
+ #
32
+ # # The user association will not be built in this example. The user_id
33
+ # # will be used instead.
34
+ # Factory(:post, :user_id => 1)
35
+ def self.alias (pattern, replace)
36
+ self.aliases << [pattern, replace]
37
+ end
38
+
39
+ def self.aliases_for (attribute) #:nodoc:
40
+ aliases.collect do |params|
41
+ pattern, replace = *params
42
+ if pattern.match(attribute.to_s)
43
+ attribute.to_s.sub(pattern, replace).to_sym
44
+ else
45
+ nil
46
+ end
47
+ end.compact << attribute
48
+ end
49
+
50
+ end