freegenie-factory_girl 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +236 -0
  5. data/Rakefile +81 -0
  6. data/lib/factory_girl/aliases.rb +50 -0
  7. data/lib/factory_girl/attribute/association.rb +20 -0
  8. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  9. data/lib/factory_girl/attribute/static.rb +17 -0
  10. data/lib/factory_girl/attribute.rb +29 -0
  11. data/lib/factory_girl/factory.rb +412 -0
  12. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  13. data/lib/factory_girl/proxy/build.rb +29 -0
  14. data/lib/factory_girl/proxy/create.rb +10 -0
  15. data/lib/factory_girl/proxy/stub.rb +49 -0
  16. data/lib/factory_girl/proxy.rb +62 -0
  17. data/lib/factory_girl/sequence.rb +63 -0
  18. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  19. data/lib/factory_girl/syntax/generate.rb +68 -0
  20. data/lib/factory_girl/syntax/make.rb +39 -0
  21. data/lib/factory_girl/syntax/sham.rb +42 -0
  22. data/lib/factory_girl/syntax.rb +12 -0
  23. data/lib/factory_girl.rb +39 -0
  24. data/spec/factory_girl/aliases_spec.rb +29 -0
  25. data/spec/factory_girl/attribute/association_spec.rb +29 -0
  26. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  27. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  28. data/spec/factory_girl/attribute_spec.rb +30 -0
  29. data/spec/factory_girl/factory_spec.rb +525 -0
  30. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  31. data/spec/factory_girl/proxy/build_spec.rb +73 -0
  32. data/spec/factory_girl/proxy/create_spec.rb +83 -0
  33. data/spec/factory_girl/proxy/stub_spec.rb +69 -0
  34. data/spec/factory_girl/proxy_spec.rb +28 -0
  35. data/spec/factory_girl/sequence_spec.rb +66 -0
  36. data/spec/factory_girl/syntax/blueprint_spec.rb +35 -0
  37. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  38. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  39. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  40. data/spec/integration_spec.rb +265 -0
  41. data/spec/models.rb +43 -0
  42. data/spec/spec_helper.rb +18 -0
  43. data/test/factory_girl_test.rb +28 -0
  44. metadata +118 -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,236 @@
1
+ = About this branch
2
+
3
+ This is just a test branch to try to solve a problem on existing recrods created by
4
+ factory_girl.
5
+ You can now specify :find_or_create as a strategy, to prevent records to be recreated each time.
6
+ Use with care, it's highly experimental.
7
+
8
+
9
+ = factory_girl
10
+
11
+ 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.
12
+
13
+ == Download
14
+
15
+ Github: http://github.com/thoughtbot/factory_girl/tree/master
16
+
17
+ Gem:
18
+ gem install thoughtbot-factory_girl --source http://gems.github.com
19
+
20
+ Note: if you install factory_girl using the gem from Github, you'll need this
21
+ in your environment.rb if you want to use Rails 2.1+'s dependency manager:
22
+
23
+ config.gem "thoughtbot-factory_girl",
24
+ :lib => "factory_girl",
25
+ :source => "http://gems.github.com"
26
+
27
+ == Defining factories
28
+
29
+ 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:
30
+
31
+ # This will guess the User class
32
+ Factory.define :user do |u|
33
+ u.first_name 'John'
34
+ u.last_name 'Doe'
35
+ u.admin false
36
+ end
37
+
38
+ # This will use the User class (Admin would have been guessed)
39
+ Factory.define :admin, :class => User do |u|
40
+ u.first_name 'Admin'
41
+ u.last_name 'User'
42
+ u.admin true
43
+ end
44
+
45
+ # The same, but using a string instead of class constant
46
+ Factory.define :admin, :class => 'user' do |u|
47
+ u.first_name 'Admin'
48
+ u.last_name 'User'
49
+ u.admin true
50
+ end
51
+
52
+ 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.
53
+
54
+ Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:
55
+
56
+ test/factories.rb
57
+ spec/factories.rb
58
+ test/factories/*.rb
59
+ spec/factories/*.rb
60
+
61
+ == Using factories
62
+
63
+ factory_girl supports several different build strategies: build, create, attributes_for and stub:
64
+
65
+ # Returns a User instance that's not saved
66
+ user = Factory.build(:user)
67
+
68
+ # Returns a saved User instance
69
+ user = Factory.create(:user)
70
+
71
+ # Returns a hash of attributes that can be used to build a User instance:
72
+ attrs = Factory.attributes_for(:user)
73
+
74
+ # Returns an object with all defined attributes stubbed out:
75
+ stub = Factory.stub(:user)
76
+
77
+ You can use the Factory method as a shortcut for the default build strategy:
78
+
79
+ # Same as Factory.create :user:
80
+ user = Factory(:user)
81
+
82
+ The default strategy can be overriden:
83
+
84
+ # Now same as Factory.build(:user)
85
+ Factory.define :user, :default_strategy => :build do |u|
86
+ ...
87
+ end
88
+
89
+ user = Factory(:user)
90
+
91
+ No matter which startegy is used, it's possible to override the defined attributes by passing a hash:
92
+
93
+ # Build a User instance and override the first_name property
94
+ user = Factory.build(:user, :first_name => 'Joe')
95
+ user.first_name
96
+ # => "Joe"
97
+
98
+ == Lazy Attributes
99
+
100
+ 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:
101
+
102
+ Factory.define :user do |u|
103
+ # ...
104
+ u.activation_code { User.generate_activation_code }
105
+ end
106
+
107
+ == Dependent Attributes
108
+
109
+ Attributes can be based on the values of other attributes using the proxy that is yieled to lazy attribute blocks:
110
+
111
+ Factory.define :user do |u|
112
+ u.first_name 'Joe'
113
+ u.last_name 'Blow'
114
+ u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
115
+ end
116
+
117
+ Factory(:user, :last_name => 'Doe').email
118
+ # => "joe.doe@example.com"
119
+
120
+ == Associations
121
+
122
+ Associated instances can be generated by using the association method when
123
+ defining a lazy attribute:
124
+
125
+ Factory.define :post do |p|
126
+ # ...
127
+ p.author {|author| author.association(:user, :last_name => 'Writely') }
128
+ end
129
+
130
+ The behavior of the association method varies depending on the build strategy used for the parent object.
131
+
132
+ # Builds and saves a User and a Post
133
+ post = Factory(:post)
134
+ post.new_record? # => false
135
+ post.author.new_record # => false
136
+
137
+ # Builds and saves a User, and then builds but does not save a Post
138
+ Factory.build(:post)
139
+ post.new_record? # => true
140
+ post.author.new_record # => false
141
+
142
+ Because this pattern is so common, a prettier syntax is available for defining
143
+ associations:
144
+
145
+ # The following definitions are equivilent:
146
+ Factory.define :post do |p|
147
+ p.author {|a| a.association(:user) }
148
+ end
149
+
150
+ Factory.define :post do |p|
151
+ p.association :author, :factory => :user
152
+ end
153
+
154
+ If the factory name is the same as the association name, the factory name can
155
+ be left out.
156
+
157
+ == Inheritance
158
+
159
+ You can easily create multiple factories for the same class without repeating common attributes by using inheritance:
160
+
161
+ Factory.define :post do |p|
162
+ # the 'title' attribute is required for all posts
163
+ p.title 'A title'
164
+ end
165
+
166
+ Factory.define :approved_post, :parent => :post do |p|
167
+ p.approved true
168
+ # the 'approver' association is required for an approved post
169
+ p.association :approver, :factory => :user
170
+ end
171
+
172
+ == Sequences
173
+
174
+ Unique values in a specific format (for example, e-mail addresses) can be
175
+ generated using sequences. Sequences are defined by calling Factory.sequence,
176
+ and values in a sequence are generated by calling Factory.next:
177
+
178
+ # Defines a new sequence
179
+ Factory.sequence :email do |n|
180
+ "person#{n}@example.com"
181
+ end
182
+
183
+ Factory.next :email
184
+ # => "person1@example.com"
185
+
186
+ Factory.next :email
187
+ # => "person2@example.com"
188
+
189
+ Sequences can be used in lazy attributes:
190
+
191
+ Factory.define :user do |f|
192
+ f.email { 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.define :user do |f|
199
+ f.sequence(:email) {|n| "person#{n}@example.com" }
200
+ end
201
+
202
+ == Alternate Syntaxes
203
+
204
+ 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.
205
+
206
+ == More Information
207
+
208
+ Our blog: http://giantrobots.thoughtbot.com
209
+
210
+ factory_girl rdoc: http://rdoc.info/projects/thoughtbot/factory_girl
211
+
212
+ Mailing list: http://groups.google.com/group/factory_girl
213
+
214
+ factory_girl issues: http://github.com/thoughtbot/factory_girl/issues
215
+
216
+ == Contributing
217
+
218
+ Please read the contribution guidelines before submitting patches or pull requests.
219
+
220
+ == Author
221
+
222
+ factory_girl was written by Joe Ferris with contributions from several authors, including:
223
+ * Alex Sharp
224
+ * Eugene Bolshakov
225
+ * Jon Yurek
226
+ * Josh Nichols
227
+ * Josh Owens
228
+
229
+ The syntax layers are derived from software written by the following authors:
230
+ * Pete Yandell
231
+ * Rick Bradley
232
+ * Yossef Mendelssohn
233
+
234
+ Thanks to all members of thoughtbot for inspiration, ideas, and funding.
235
+
236
+ 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{factory_girl}
41
+ s.version = "1.2.2"
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,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
@@ -0,0 +1,20 @@
1
+ class Factory
2
+ class Attribute #:nodoc:
3
+
4
+ class Association < Attribute #:nodoc:
5
+
6
+ attr_reader :factory
7
+
8
+ def initialize(name, factory, overrides)
9
+ super(name)
10
+ @factory = factory
11
+ @overrides = overrides
12
+ end
13
+
14
+ def add_to(proxy)
15
+ proxy.associate(name, @factory, @overrides)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ class Factory
2
+ class Attribute #:nodoc:
3
+
4
+ class Dynamic < Attribute #:nodoc:
5
+ def initialize(name, block)
6
+ super(name)
7
+ @block = block
8
+ end
9
+
10
+ def add_to(proxy)
11
+ value = @block.arity.zero? ? @block.call : @block.call(proxy)
12
+ if Factory::Sequence === value
13
+ raise SequenceAbuseError
14
+ end
15
+ proxy.set(name, value)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ class Factory
2
+ class Attribute #:nodoc:
3
+
4
+ class Static < Attribute #:nodoc:
5
+
6
+ def initialize(name, value)
7
+ super(name)
8
+ @value = value
9
+ end
10
+
11
+ def add_to(proxy)
12
+ proxy.set(name, @value)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ class Factory
2
+
3
+ # Raised when defining an invalid attribute:
4
+ # * Defining an attribute which has a name ending in "="
5
+ # * Defining an attribute with both a static and lazy value
6
+ # * Defining an attribute twice in the same factory
7
+ class AttributeDefinitionError < RuntimeError
8
+ end
9
+
10
+ class Attribute #:nodoc:
11
+
12
+ attr_reader :name
13
+
14
+ def initialize(name)
15
+ @name = name.to_sym
16
+
17
+ if @name.to_s =~ /=$/
18
+ attribute_name = $`
19
+ raise AttributeDefinitionError,
20
+ "factory_girl uses 'f.#{attribute_name} value' syntax " +
21
+ "rather than 'f.#{attribute_name} = value'"
22
+ end
23
+ end
24
+
25
+ def add_to(proxy)
26
+ end
27
+ end
28
+
29
+ end