masa-iwasaki-factory_girl 1.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,261 @@
1
+ = What this fork added to original factory_girl
2
+ == Factory#find_or_create
3
+ By default, the association created by factory_girl is based on Factory#create. This means there would be twice creation for project_1 in the code shown below.
4
+
5
+ # Factory definitions
6
+ Factory.define :user_john, :class => User do |r|
7
+ r.name "John"
8
+ r.association :project, :factory => :project_1
9
+ end
10
+
11
+ Factory.define :user_bob, :class => User do |r|
12
+ r.name "Bob"
13
+ r.association :project, :factory => :project_1
14
+ end
15
+
16
+ Factory.define :project_1, :class => Project do |r|
17
+ r.name "first project"
18
+ end
19
+
20
+ # Spec
21
+ describe User do
22
+ it "should create only one project" do
23
+ Factory(:user_john)
24
+ Factory(:user_bob)
25
+ Project.count.should eql(1) #FAILED: expected 1, got 2
26
+ end
27
+ end
28
+
29
+ This fork added Factory#find_or_create. This method keeps an instance which has already created. This means you would not have the creation of a same factory and the code shown above turns green.
30
+
31
+ This fork also changed default_strategy from create to find_or_create. This change affects the definitions written in some syntax. If you are using bluprint or sham syntax, you might need to use "Factory.create(factory_name)" explicityly instead of "Facotry(factory_name)".
32
+
33
+ = factory_girl
34
+
35
+ 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.
36
+
37
+ == Download
38
+
39
+ Github: http://github.com/thoughtbot/factory_girl/tree/master
40
+
41
+ Gem:
42
+ gem install thoughtbot-factory_girl --source http://gems.github.com
43
+
44
+ Note: if you install factory_girl using the gem from Github, you'll need this
45
+ in your environment.rb if you want to use Rails 2.1+'s dependency manager:
46
+
47
+ config.gem "thoughtbot-factory_girl",
48
+ :lib => "factory_girl",
49
+ :source => "http://gems.github.com"
50
+
51
+ == Defining factories
52
+
53
+ 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:
54
+
55
+ # This will guess the User class
56
+ Factory.define :user do |u|
57
+ u.first_name 'John'
58
+ u.last_name 'Doe'
59
+ u.admin false
60
+ end
61
+
62
+ # This will use the User class (Admin would have been guessed)
63
+ Factory.define :admin, :class => User do |u|
64
+ u.first_name 'Admin'
65
+ u.last_name 'User'
66
+ u.admin true
67
+ end
68
+
69
+ # The same, but using a string instead of class constant
70
+ Factory.define :admin, :class => 'user' do |u|
71
+ u.first_name 'Admin'
72
+ u.last_name 'User'
73
+ u.admin true
74
+ end
75
+
76
+ 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.
77
+
78
+ Factories can either be defined anywhere, but will automatically be loaded if they are defined in files at the following locations:
79
+
80
+ test/factories.rb
81
+ spec/factories.rb
82
+ test/factories/*.rb
83
+ spec/factories/*.rb
84
+
85
+ == Using factories
86
+
87
+ factory_girl supports several different build strategies: build, create, attributes_for and stub:
88
+
89
+ # Returns a User instance that's not saved
90
+ user = Factory.build(:user)
91
+
92
+ # Returns a saved User instance
93
+ user = Factory.create(:user)
94
+
95
+ # Returns a hash of attributes that can be used to build a User instance:
96
+ attrs = Factory.attributes_for(:user)
97
+
98
+ # Returns an object with all defined attributes stubbed out:
99
+ stub = Factory.stub(:user)
100
+
101
+ You can use the Factory method as a shortcut for the default build strategy:
102
+
103
+ # Same as Factory.create :user:
104
+ user = Factory(:user)
105
+
106
+ The default strategy can be overriden:
107
+
108
+ # Now same as Factory.build(:user)
109
+ Factory.define :user, :default_strategy => :build do |u|
110
+ ...
111
+ end
112
+
113
+ user = Factory(:user)
114
+
115
+ No matter which startegy is used, it's possible to override the defined attributes by passing a hash:
116
+
117
+ # Build a User instance and override the first_name property
118
+ user = Factory.build(:user, :first_name => 'Joe')
119
+ user.first_name
120
+ # => "Joe"
121
+
122
+ == Lazy Attributes
123
+
124
+ 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:
125
+
126
+ Factory.define :user do |u|
127
+ # ...
128
+ u.activation_code { User.generate_activation_code }
129
+ end
130
+
131
+ == Dependent Attributes
132
+
133
+ Attributes can be based on the values of other attributes using the proxy that is yieled to lazy attribute blocks:
134
+
135
+ Factory.define :user do |u|
136
+ u.first_name 'Joe'
137
+ u.last_name 'Blow'
138
+ u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
139
+ end
140
+
141
+ Factory(:user, :last_name => 'Doe').email
142
+ # => "joe.doe@example.com"
143
+
144
+ == Associations
145
+
146
+ Associated instances can be generated by using the association method when
147
+ defining a lazy attribute:
148
+
149
+ Factory.define :post do |p|
150
+ # ...
151
+ p.author {|author| author.association(:user, :last_name => 'Writely') }
152
+ end
153
+
154
+ The behavior of the association method varies depending on the build strategy used for the parent object.
155
+
156
+ # Builds and saves a User and a Post
157
+ post = Factory(:post)
158
+ post.new_record? # => false
159
+ post.author.new_record # => false
160
+
161
+ # Builds and saves a User, and then builds but does not save a Post
162
+ Factory.build(:post)
163
+ post.new_record? # => true
164
+ post.author.new_record # => false
165
+
166
+ Because this pattern is so common, a prettier syntax is available for defining
167
+ associations:
168
+
169
+ # The following definitions are equivilent:
170
+ Factory.define :post do |p|
171
+ p.author {|a| a.association(:user) }
172
+ end
173
+
174
+ Factory.define :post do |p|
175
+ p.association :author, :factory => :user
176
+ end
177
+
178
+ If the factory name is the same as the association name, the factory name can
179
+ be left out.
180
+
181
+
182
+ == Inheritance
183
+
184
+ You can easily create multiple factories for the same class without repeating common attributes by using inheritance:
185
+
186
+ Factory.define :post do |p|
187
+ # the 'title' attribute is required for all posts
188
+ p.title 'A title'
189
+ end
190
+
191
+ Factory.define :approved_post, :parent => :post do |p|
192
+ p.approved true
193
+ # the 'approver' association is required for an approved post
194
+ p.association :approver, :factory => :user
195
+ end
196
+
197
+ == Sequences
198
+
199
+ Unique values in a specific format (for example, e-mail addresses) can be
200
+ generated using sequences. Sequences are defined by calling Factory.sequence,
201
+ and values in a sequence are generated by calling Factory.next:
202
+
203
+ # Defines a new sequence
204
+ Factory.sequence :email do |n|
205
+ "person#{n}@example.com"
206
+ end
207
+
208
+ Factory.next :email
209
+ # => "person1@example.com"
210
+
211
+ Factory.next :email
212
+ # => "person2@example.com"
213
+
214
+ Sequences can be used in lazy attributes:
215
+
216
+ Factory.define :user do |f|
217
+ f.email { Factory.next(:email) }
218
+ end
219
+
220
+ And it's also possible to define an in-line sequence that is only used in
221
+ a particular factory:
222
+
223
+ Factory.define :user do |f|
224
+ f.sequence(:email) {|n| "person#{n}@example.com" }
225
+ end
226
+
227
+ == Alternate Syntaxes
228
+
229
+ 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.
230
+
231
+ == More Information
232
+
233
+ Our blog: http://giantrobots.thoughtbot.com
234
+
235
+ factory_girl rdoc: http://rdoc.info/projects/thoughtbot/factory_girl
236
+
237
+ Mailing list: http://groups.google.com/group/factory_girl
238
+
239
+ factory_girl issues: http://github.com/thoughtbot/factory_girl/issues
240
+
241
+ == Contributing
242
+
243
+ Please read the contribution guidelines before submitting patches or pull requests.
244
+
245
+ == Author
246
+
247
+ factory_girl was written by Joe Ferris with contributions from several authors, including:
248
+ * Alex Sharp
249
+ * Eugene Bolshakov
250
+ * Jon Yurek
251
+ * Josh Nichols
252
+ * Josh Owens
253
+
254
+ The syntax layers are derived from software written by the following authors:
255
+ * Pete Yandell
256
+ * Rick Bradley
257
+ * Yossef Mendelssohn
258
+
259
+ Thanks to all members of thoughtbot for inspiration, ideas, and funding.
260
+
261
+ Copyright 2008-2009 Joe Ferris and thoughtbot[http://www.thoughtbot.com], inc.
data/Rakefile ADDED
@@ -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.0"
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"]
56
+ s.email = %q{jferris@thoughtbot.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,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,18 @@
1
+ class Factory
2
+ class Attribute #:nodoc:
3
+
4
+ class Association < Attribute #:nodoc:
5
+
6
+ def initialize(name, factory, overrides)
7
+ super(name)
8
+ @factory = factory
9
+ @overrides = overrides
10
+ end
11
+
12
+ def add_to(proxy)
13
+ proxy.associate(name, @factory, @overrides)
14
+ end
15
+ end
16
+
17
+ end
18
+ 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.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