lacomartincik-factory_girl 1.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +228 -0
  5. data/Rakefile +74 -0
  6. data/lib/factory_girl.rb +23 -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 +18 -0
  10. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  11. data/lib/factory_girl/attribute/static.rb +17 -0
  12. data/lib/factory_girl/factory.rb +360 -0
  13. data/lib/factory_girl/proxy.rb +62 -0
  14. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  15. data/lib/factory_girl/proxy/build.rb +29 -0
  16. data/lib/factory_girl/proxy/create.rb +10 -0
  17. data/lib/factory_girl/proxy/stub.rb +49 -0
  18. data/lib/factory_girl/sequence.rb +63 -0
  19. data/lib/factory_girl/syntax.rb +12 -0
  20. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  21. data/lib/factory_girl/syntax/generate.rb +68 -0
  22. data/lib/factory_girl/syntax/make.rb +39 -0
  23. data/lib/factory_girl/syntax/sham.rb +42 -0
  24. data/spec/factory_girl/aliases_spec.rb +29 -0
  25. data/spec/factory_girl/attribute/association_spec.rb +25 -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 +490 -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 +34 -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. metadata +113 -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,228 @@
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 thoughtbot-factory_girl --source http://gems.github.com
11
+
12
+ Note: if you install factory_girl using the gem from Github, you'll need this
13
+ in your environment.rb if you want to use Rails 2.1+'s dependency manager:
14
+
15
+ config.gem "thoughtbot-factory_girl",
16
+ :lib => "factory_girl",
17
+ :source => "http://gems.github.com"
18
+
19
+ == Defining factories
20
+
21
+ 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:
22
+
23
+ # This will guess the User class
24
+ Factory.define :user do |u|
25
+ u.first_name 'John'
26
+ u.last_name 'Doe'
27
+ u.admin false
28
+ end
29
+
30
+ # This will use the User class (Admin would have been guessed)
31
+ Factory.define :admin, :class => User do |u|
32
+ u.first_name 'Admin'
33
+ u.last_name 'User'
34
+ u.admin true
35
+ end
36
+
37
+ # The same, but using a string instead of class constant
38
+ Factory.define :admin, :class => 'user' do |u|
39
+ u.first_name 'Admin'
40
+ u.last_name 'User'
41
+ u.admin true
42
+ end
43
+
44
+ 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
+
46
+ Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:
47
+
48
+ test/factories.rb
49
+ spec/factories.rb
50
+ test/factories/*.rb
51
+ spec/factories/*.rb
52
+
53
+ == Using factories
54
+
55
+ factory_girl supports several different build strategies: build, create, attributes_for and stub:
56
+
57
+ # Returns a User instance that's not saved
58
+ user = Factory.build(:user)
59
+
60
+ # Returns a saved User instance
61
+ user = Factory.create(:user)
62
+
63
+ # Returns a hash of attributes that can be used to build a User instance:
64
+ attrs = Factory.attributes_for(:user)
65
+
66
+ # Returns an object with all defined attributes stubbed out:
67
+ stub = Factory.stub(:user)
68
+
69
+ You can use the Factory method as a shortcut for the default build strategy:
70
+
71
+ # Same as Factory.create :user:
72
+ user = Factory(:user)
73
+
74
+ The default strategy can be overriden:
75
+
76
+ # Now same as Factory.build(:user)
77
+ Factory.define :user, :default_strategy => :build do |u|
78
+ ...
79
+ end
80
+
81
+ user = Factory(:user)
82
+
83
+ No matter which startegy is used, it's possible to override the defined attributes by passing a hash:
84
+
85
+ # Build a User instance and override the first_name property
86
+ user = Factory.build(:user, :first_name => 'Joe')
87
+ user.first_name
88
+ # => "Joe"
89
+
90
+ == Lazy Attributes
91
+
92
+ 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:
93
+
94
+ Factory.define :user do |u|
95
+ # ...
96
+ u.activation_code { User.generate_activation_code }
97
+ end
98
+
99
+ == Dependent Attributes
100
+
101
+ Attributes can be based on the values of other attributes using the proxy that is yieled to lazy attribute blocks:
102
+
103
+ Factory.define :user do |u|
104
+ u.first_name 'Joe'
105
+ u.last_name 'Blow'
106
+ u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
107
+ end
108
+
109
+ Factory(:user, :last_name => 'Doe').email
110
+ # => "joe.doe@example.com"
111
+
112
+ == Associations
113
+
114
+ Associated instances can be generated by using the association method when
115
+ defining a lazy attribute:
116
+
117
+ Factory.define :post do |p|
118
+ # ...
119
+ p.author {|author| author.association(:user, :last_name => 'Writely') }
120
+ end
121
+
122
+ The behavior of the association method varies depending on the build strategy used for the parent object.
123
+
124
+ # Builds and saves a User and a Post
125
+ post = Factory(:post)
126
+ post.new_record? # => false
127
+ post.author.new_record # => false
128
+
129
+ # Builds and saves a User, and then builds but does not save a Post
130
+ Factory.build(:post)
131
+ post.new_record? # => true
132
+ post.author.new_record # => false
133
+
134
+ Because this pattern is so common, a prettier syntax is available for defining
135
+ associations:
136
+
137
+ # The following definitions are equivilent:
138
+ Factory.define :post do |p|
139
+ p.author {|a| a.association(:user) }
140
+ end
141
+
142
+ Factory.define :post do |p|
143
+ p.association :author, :factory => :user
144
+ end
145
+
146
+ If the factory name is the same as the association name, the factory name can
147
+ be left out.
148
+
149
+ == Inheritance
150
+
151
+ You can easily create multiple factories for the same class without repeating common attributes by using inheritance:
152
+
153
+ Factory.define :post do |p|
154
+ # the 'title' attribute is required for all posts
155
+ p.title 'A title'
156
+ end
157
+
158
+ Factory.define :approved_post, :parent => :post do |p|
159
+ p.approved true
160
+ # the 'approver' association is required for an approved post
161
+ p.association :approver, :factory => :user
162
+ end
163
+
164
+ == Sequences
165
+
166
+ Unique values in a specific format (for example, e-mail addresses) can be
167
+ generated using sequences. Sequences are defined by calling Factory.sequence,
168
+ and values in a sequence are generated by calling Factory.next:
169
+
170
+ # Defines a new sequence
171
+ Factory.sequence :email do |n|
172
+ "person#{n}@example.com"
173
+ end
174
+
175
+ Factory.next :email
176
+ # => "person1@example.com"
177
+
178
+ Factory.next :email
179
+ # => "person2@example.com"
180
+
181
+ Sequences can be used in lazy attributes:
182
+
183
+ Factory.define :user do |f|
184
+ f.email { Factory.next(:email) }
185
+ end
186
+
187
+ And it's also possible to define an in-line sequence that is only used in
188
+ a particular factory:
189
+
190
+ Factory.define :user do |f|
191
+ f.sequence(:email) {|n| "person#{n}@example.com" }
192
+ end
193
+
194
+ == Alternate Syntaxes
195
+
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.
197
+
198
+ == More Information
199
+
200
+ Our blog: http://giantrobots.thoughtbot.com
201
+
202
+ factory_girl rdoc: http://rdoc.info/projects/thoughtbot/factory_girl
203
+
204
+ Mailing list: http://groups.google.com/group/factory_girl
205
+
206
+ factory_girl issues: http://github.com/thoughtbot/factory_girl/issues
207
+
208
+ == Contributing
209
+
210
+ Please read the contribution guidelines before submitting patches or pull requests.
211
+
212
+ == Author
213
+
214
+ factory_girl was written by Joe Ferris with contributions from several authors, including:
215
+ * Alex Sharp
216
+ * Eugene Bolshakov
217
+ * Jon Yurek
218
+ * Josh Nichols
219
+ * Josh Owens
220
+
221
+ The syntax layers are derived from software written by the following authors:
222
+ * Pete Yandell
223
+ * Rick Bradley
224
+ * Yossef Mendelssohn
225
+
226
+ Thanks to all members of thoughtbot for inspiration, ideas, and funding.
227
+
228
+ Copyright 2008-2009 Joe Ferris and thoughtbot[http://www.thoughtbot.com], inc.
@@ -0,0 +1,74 @@
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
+
10
+ desc 'Default: run the specs.'
11
+ task :default => :spec
12
+
13
+ Spec::Rake::SpecTask.new do |t|
14
+ t.spec_opts = ['--options', "spec/spec.opts"]
15
+ end
16
+
17
+ desc 'Performs code coverage on the factory_girl plugin.'
18
+ Rcov::RcovTask.new do |t|
19
+ t.test_files = FileList['spec/*_spec.rb']
20
+ t.verbose = true
21
+ end
22
+
23
+ desc 'Generate documentation for the factory_girl plugin.'
24
+ Rake::RDocTask.new(:rdoc) do |rdoc|
25
+ rdoc.rdoc_dir = 'rdoc'
26
+ rdoc.title = 'Factory Girl'
27
+ rdoc.options << '--line-numbers' << "--main" << "README.rdoc"
28
+ rdoc.rdoc_files.include('README.rdoc')
29
+ rdoc.rdoc_files.include('CONTRIBUTION_GUIDELINES.rdoc')
30
+ rdoc.rdoc_files.include('lib/**/*.rb')
31
+ end
32
+
33
+ desc 'Update documentation on website'
34
+ task :sync_docs => 'rdoc' do
35
+ `rsync -ave ssh rdoc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/factory_girl`
36
+ end
37
+
38
+ spec = Gem::Specification.new do |s|
39
+ s.name = %q{factory_girl}
40
+ s.version = "1.2.1.1"
41
+ s.summary = %q{factory_girl provides a framework and DSL for defining and
42
+ using model instance factories.}
43
+ s.description = %q{factory_girl provides a framework and DSL for defining and
44
+ using factories - less error-prone, more explicit, and
45
+ all-around easier to work with than fixtures.}
46
+
47
+ s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'spec/**/*.rb']
48
+ s.require_path = 'lib'
49
+ s.test_files = Dir[*['spec/**/*_spec.rb']]
50
+
51
+ s.has_rdoc = true
52
+ s.extra_rdoc_files = ["README.rdoc"]
53
+ s.rdoc_options = ['--line-numbers', "--main", "README.rdoc"]
54
+
55
+ s.authors = ["Joe Ferris", "Ladislav Martincik"]
56
+ s.email = %q{jferris@thoughtbot.com ladislav.martincik@xing.com}
57
+
58
+ s.platform = Gem::Platform::RUBY
59
+ end
60
+
61
+ Rake::GemPackageTask.new spec do |pkg|
62
+ pkg.need_tar = true
63
+ pkg.need_zip = true
64
+ end
65
+
66
+ desc "Clean files generated by rake tasks"
67
+ task :clobber => [:clobber_rdoc, :clobber_package]
68
+
69
+ desc "Generate a gemspec file"
70
+ task :gemspec do
71
+ File.open("#{spec.name}.gemspec", 'w') do |f|
72
+ f.write spec.to_ruby
73
+ end
74
+ end
@@ -0,0 +1,23 @@
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/sequence'
13
+ require 'factory_girl/aliases'
14
+
15
+ # Shortcut for Factory.default_strategy.
16
+ #
17
+ # Example:
18
+ # Factory(:user, :name => 'Joe')
19
+ def Factory (name, attrs = {})
20
+ Factory.default_strategy(name, attrs)
21
+ end
22
+
23
+ Factory.find_definitions
@@ -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,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