factory_girl 2.0.0.beta1 → 2.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/Appraisals +12 -0
  2. data/{CONTRIBUTION_GUIDELINES.rdoc → CONTRIBUTION_GUIDELINES.md} +3 -3
  3. data/GETTING_STARTED.md +246 -0
  4. data/Gemfile +12 -0
  5. data/Gemfile.lock +62 -0
  6. data/README.md +64 -0
  7. data/Rakefile +15 -30
  8. data/features/factory_girl_steps.feature +26 -0
  9. data/features/step_definitions/database_steps.rb +2 -0
  10. data/features/support/env.rb +0 -9
  11. data/features/support/factories.rb +15 -0
  12. data/features/support/test.db +0 -0
  13. data/lib/factory_girl.rb +2 -0
  14. data/lib/factory_girl/definition_proxy.rb +10 -43
  15. data/lib/factory_girl/factory.rb +48 -17
  16. data/lib/factory_girl/proxy.rb +3 -3
  17. data/lib/factory_girl/proxy/attributes_for.rb +1 -1
  18. data/lib/factory_girl/proxy/build.rb +3 -3
  19. data/lib/factory_girl/proxy/create.rb +6 -2
  20. data/lib/factory_girl/proxy/stub.rb +3 -3
  21. data/lib/factory_girl/registry.rb +59 -0
  22. data/lib/factory_girl/sequence.rb +23 -9
  23. data/lib/factory_girl/step_definitions.rb +16 -9
  24. data/lib/factory_girl/syntax/blueprint.rb +2 -2
  25. data/lib/factory_girl/syntax/default.rb +5 -3
  26. data/lib/factory_girl/syntax/generate.rb +6 -6
  27. data/lib/factory_girl/syntax/make.rb +2 -2
  28. data/lib/factory_girl/syntax/methods.rb +75 -0
  29. data/lib/factory_girl/syntax/sham.rb +2 -2
  30. data/lib/factory_girl/syntax/vintage.rb +16 -79
  31. data/lib/factory_girl/version.rb +1 -1
  32. data/spec/acceptance/acceptance_helper.rb +7 -12
  33. data/spec/acceptance/attribute_aliases_spec.rb +26 -0
  34. data/spec/acceptance/attributes_for_spec.rb +48 -0
  35. data/spec/acceptance/build_spec.rb +35 -0
  36. data/spec/acceptance/build_stubbed_spec.rb +79 -0
  37. data/spec/acceptance/callbacks_spec.rb +42 -0
  38. data/spec/acceptance/create_spec.rb +67 -0
  39. data/spec/acceptance/default_strategy_spec.rb +27 -0
  40. data/spec/acceptance/definition_spec.rb +28 -0
  41. data/spec/acceptance/parent_spec.rb +39 -0
  42. data/spec/acceptance/sequence_spec.rb +32 -0
  43. data/spec/acceptance/syntax/blueprint_spec.rb +7 -5
  44. data/spec/acceptance/syntax/generate_spec.rb +10 -4
  45. data/spec/acceptance/syntax/make_spec.rb +7 -4
  46. data/spec/acceptance/syntax/sham_spec.rb +15 -8
  47. data/spec/acceptance/syntax/vintage_spec.rb +57 -17
  48. data/spec/factory_girl/attribute/dynamic_spec.rb +1 -1
  49. data/spec/factory_girl/definition_proxy_spec.rb +13 -11
  50. data/spec/factory_girl/factory_spec.rb +29 -52
  51. data/spec/factory_girl/find_definitions_spec.rb +34 -23
  52. data/spec/factory_girl/proxy/attributes_for_spec.rb +6 -6
  53. data/spec/factory_girl/proxy/build_spec.rb +4 -4
  54. data/spec/factory_girl/proxy/create_spec.rb +11 -3
  55. data/spec/factory_girl/proxy/stub_spec.rb +6 -6
  56. data/spec/factory_girl/proxy_spec.rb +1 -1
  57. data/spec/factory_girl/registry_spec.rb +92 -0
  58. data/spec/factory_girl/sequence_spec.rb +65 -8
  59. data/spec/spec_helper.rb +75 -29
  60. metadata +66 -43
  61. data/README.rdoc +0 -282
  62. data/spec/acceptance/acceptance_spec.rb +0 -288
  63. data/spec/acceptance/models.rb +0 -48
data/Appraisals ADDED
@@ -0,0 +1,12 @@
1
+ appraise "2.1" do
2
+ gem "activerecord", "~> 2.1"
3
+ end
4
+
5
+ appraise "2.3" do
6
+ gem "activerecord", "~> 2.3"
7
+ end
8
+
9
+ appraise "3.0" do
10
+ gem "activerecord", "~> 3.0"
11
+ end
12
+
@@ -1,9 +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.
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
2
 
3
- * Make sure you're accessing the source from the {official repository}[http://github.com/thoughtbot/factory_girl/tree/master].
3
+ * Make sure you're accessing the source from the [official repository](http://github.com/thoughtbot/factory_girl/tree/master).
4
4
  * Please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier.
5
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.
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
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
8
  * Contributions without tests won't be accepted.
9
9
  * Please don't submit patches or branches that update the Gem version.
@@ -0,0 +1,246 @@
1
+ Getting Started
2
+ ===============
3
+
4
+ Defining factories
5
+ ------------------
6
+
7
+ 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:
8
+
9
+ # This will guess the User class
10
+ FactoryGirl.define do
11
+ factory :user do
12
+ first_name 'John'
13
+ last_name 'Doe'
14
+ admin false
15
+ end
16
+
17
+ # This will use the User class (Admin would have been guessed)
18
+ factory :admin, :class => User do
19
+ first_name 'Admin'
20
+ last_name 'User'
21
+ admin true
22
+ end
23
+
24
+ # The same, but using a string instead of class constant
25
+ factory :admin, :class => 'user' do
26
+ first_name 'Admin'
27
+ last_name 'User'
28
+ admin true
29
+ end
30
+ end
31
+
32
+ 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.
33
+
34
+ Attempting to define multiple factories with the same name will raise an error.
35
+
36
+ Factories can be defined anywhere, but will be automatically loaded if they
37
+ are defined in files at the following locations:
38
+
39
+ test/factories.rb
40
+ spec/factories.rb
41
+ test/factories/*.rb
42
+ spec/factories/*.rb
43
+
44
+ Using factories
45
+ ---------------
46
+
47
+ factory_girl supports several different build strategies: build, create, attributes_for and stub:
48
+
49
+ # Returns a User instance that's not saved
50
+ user = FactoryGirl.build(:user)
51
+
52
+ # Returns a saved User instance
53
+ user = FactoryGirl.create(:user)
54
+
55
+ # Returns a hash of attributes that can be used to build a User instance:
56
+ attrs = FactoryGirl.attributes_for(:user)
57
+
58
+ # Returns an object with all defined attributes stubbed out:
59
+ stub = FactoryGirl.stub(:user)
60
+
61
+ No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
62
+
63
+ # Build a User instance and override the first_name property
64
+ user = FactoryGirl.build(:user, :first_name => 'Joe')
65
+ user.first_name
66
+ # => "Joe"
67
+
68
+ If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
69
+
70
+ # rspec
71
+ RSpec.configure do |config|
72
+ config.include Factory::Syntax::Methods
73
+ end
74
+
75
+ # Test::Unit
76
+ class Test::Unit::TestCase
77
+ include Factory::Syntax::Methods
78
+ end
79
+
80
+ Lazy Attributes
81
+ ---------------
82
+
83
+ 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:
84
+
85
+ factory :user do
86
+ # ...
87
+ activation_code { User.generate_activation_code }
88
+ end
89
+
90
+ Dependent Attributes
91
+ --------------------
92
+
93
+ Attributes can be based on the values of other attributes using the proxy that is yielded to lazy attribute blocks:
94
+
95
+ factory :user do
96
+ first_name 'Joe'
97
+ last_name 'Blow'
98
+ email { "#{first_name}.#{last_name}@example.com".downcase }
99
+ end
100
+
101
+ FactoryGirl.create(:user, :last_name => 'Doe').email
102
+ # => "joe.doe@example.com"
103
+
104
+ Associations
105
+ ------------
106
+
107
+ Associated instances can be generated by using the association method when
108
+ defining a lazy attribute:
109
+
110
+ factory :post do
111
+ # ...
112
+ author
113
+ end
114
+
115
+ You can also specify a different factory or override attributes:
116
+
117
+ factory :post do
118
+ # ...
119
+ association :author, :factory => :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 = FactoryGirl.create(: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
+ post = FactoryGirl.build(:post)
131
+ post.new_record? # => true
132
+ post.author.new_record # => false
133
+
134
+ If the factory name is the same as the association name, the factory name can
135
+ be left out.
136
+
137
+ Inheritance
138
+ -----------
139
+
140
+ You can easily create multiple factories for the same class without repeating common attributes by using inheritance:
141
+
142
+ factory :post do
143
+ # the 'title' attribute is required for all posts
144
+ title 'A title'
145
+ end
146
+
147
+ factory :approved_post, :parent => :post do
148
+ approved true
149
+ # the 'approver' association is required for an approved post
150
+ association :approver, :factory => :user
151
+ end
152
+
153
+ Sequences
154
+ ---------
155
+
156
+ Unique values in a specific format (for example, e-mail addresses) can be
157
+ generated using sequences. Sequences are defined by calling sequence in a
158
+ definition block, and values in a sequence are generated by calling
159
+ Factory.next:
160
+
161
+ # Defines a new sequence
162
+ FactoryGirl.define do
163
+ sequence :email do |n|
164
+ "person#{n}@example.com"
165
+ end
166
+ end
167
+
168
+ Factory.next :email
169
+ # => "person1@example.com"
170
+
171
+ Factory.next :email
172
+ # => "person2@example.com"
173
+
174
+ Sequences can be used as attributes:
175
+
176
+ factory :user do
177
+ email
178
+ end
179
+
180
+ Or in lazy attributes:
181
+
182
+ factory :invite do
183
+ invitee { Factory.next(:email) }
184
+ end
185
+
186
+ And it's also possible to define an in-line sequence that is only used in
187
+ a particular factory:
188
+
189
+ factory :user do
190
+ sequence(:email) {|n| "person#{n}@example.com" }
191
+ end
192
+
193
+ Callbacks
194
+ ---------
195
+
196
+ Factory_girl makes available three callbacks for injecting some code:
197
+
198
+ * after_build - called after a factory is built (via FactoryGirl.build)
199
+ * after_create - called after a factory is saved (via FactoryGirl.create)
200
+ * after_stub - called after a factory is stubbed (via FactoryGirl.stub)
201
+
202
+ Examples:
203
+
204
+ # Define a factory that calls the generate_hashed_password method after it is built
205
+ factory :user do
206
+ after_build { |user| generate_hashed_password(user) }
207
+ end
208
+
209
+ Note that you'll have an instance of the user in the block. This can be useful.
210
+
211
+ You can also define multiple types of callbacks on the same factory:
212
+
213
+ factory :user do
214
+ after_build { |user| do_something_to(user) }
215
+ after_create { |user| do_something_else_to(user) }
216
+ end
217
+
218
+ Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
219
+
220
+ factory :user do
221
+ after_create { this_runs_first }
222
+ after_create { then_this }
223
+ end
224
+
225
+ Calling FactoryGirl.create will invoke both after_build and after_create callbacks.
226
+
227
+ Also, like standard attributes, child factories will inherit (and can also define) callbacks from their parent factory.
228
+
229
+ Alternate Syntaxes
230
+ ------------------
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. For example, the Machinist-style syntax is popular:
233
+
234
+ require 'factory_girl/syntax/blueprint'
235
+ require 'factory_girl/syntax/make'
236
+ require 'factory_girl/syntax/sham'
237
+
238
+ Sham.email {|n| "#{n}@example.com" }
239
+
240
+ User.blueprint do
241
+ name { 'Billy Bob' }
242
+ email { Sham.email }
243
+ end
244
+
245
+ User.make(:name => 'Johnny')
246
+
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ gem "cucumber"
3
+ gem "rake"
4
+ gem "rspec", "~> 2.0"
5
+ gem "rcov"
6
+ gem "activerecord", :require => false
7
+ gem "rr"
8
+ gem "sqlite3-ruby", :require => false
9
+ gem "appraisal"
10
+ gem "yard"
11
+ gem "bluecloth"
12
+
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.1)
5
+ activesupport (= 3.0.1)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4.1)
8
+ activerecord (3.0.1)
9
+ activemodel (= 3.0.1)
10
+ activesupport (= 3.0.1)
11
+ arel (~> 1.0.0)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.1)
14
+ appraisal (0.1)
15
+ bundler
16
+ rake
17
+ arel (1.0.1)
18
+ activesupport (~> 3.0.0)
19
+ bluecloth (2.0.9)
20
+ builder (2.1.2)
21
+ cucumber (0.9.4)
22
+ builder (~> 2.1.2)
23
+ diff-lcs (~> 1.1.2)
24
+ gherkin (~> 2.2.9)
25
+ json (~> 1.4.6)
26
+ term-ansicolor (~> 1.0.5)
27
+ diff-lcs (1.1.2)
28
+ gherkin (2.2.9)
29
+ json (~> 1.4.6)
30
+ term-ansicolor (~> 1.0.5)
31
+ i18n (0.4.2)
32
+ json (1.4.6)
33
+ rake (0.8.7)
34
+ rcov (0.9.9)
35
+ rr (1.0.2)
36
+ rspec (2.1.0)
37
+ rspec-core (~> 2.1.0)
38
+ rspec-expectations (~> 2.1.0)
39
+ rspec-mocks (~> 2.1.0)
40
+ rspec-core (2.1.0)
41
+ rspec-expectations (2.1.0)
42
+ diff-lcs (~> 1.1.2)
43
+ rspec-mocks (2.1.0)
44
+ sqlite3-ruby (1.3.2)
45
+ term-ansicolor (1.0.5)
46
+ tzinfo (0.3.23)
47
+ yard (0.6.4)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ activerecord
54
+ appraisal
55
+ bluecloth
56
+ cucumber
57
+ rake
58
+ rcov
59
+ rr
60
+ rspec (~> 2.0)
61
+ sqlite3-ruby
62
+ yard
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ factory_girl
2
+ ============
3
+
4
+ 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.
5
+
6
+ If you want to use factory_girl with Rails 3, see
7
+ [factory_girl_rails](http://github.com/thoughtbot/factory_girl_rails).
8
+
9
+ Documentation
10
+ -------------
11
+
12
+ You should find the documentation for your version of factory_girl on [Rubygems](http://rubygems.org/gems/factory_girl).
13
+
14
+ See {file:GETTING_STARTED.md} for information on defining and using factories.
15
+
16
+ Download
17
+ --------
18
+
19
+ [Github](http://github.com/thoughtbot/factory_girl/tree/master) or
20
+
21
+ Rubygems:
22
+ gem install factory_girl
23
+
24
+ More Information
25
+ ----------------
26
+
27
+ * [Rubygems](http://rubygems.org/gems/factory_girl)
28
+ * [Mailing list](http://groups.google.com/group/factory_girl)
29
+ * [Issues](http://github.com/thoughtbot/factory_girl/issues)
30
+ * [GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS](http://giantrobots.thoughtbot.com)
31
+
32
+ Contributing
33
+ ------------
34
+
35
+ Please read the contribution guidelines before submitting patches or pull requests.
36
+
37
+ Credits
38
+ -------
39
+
40
+ factory_girl was written by Joe Ferris with contributions from several authors, including:
41
+
42
+ * Alex Sharp
43
+ * Eugene Bolshakov
44
+ * Jon Yurek
45
+ * Josh Nichols
46
+ * Josh Owens
47
+ * Nate Sutton
48
+
49
+ The syntax layers are derived from software written by the following authors:
50
+
51
+ * Pete Yandell
52
+ * Rick Bradley
53
+ * Yossef Mendelssohn
54
+
55
+ ![thoughtbot](http://thoughtbot.com/images/tm/logo.png)
56
+
57
+ factory_girl is maintained and funded by [thoughtbot, inc](http://thoughtbot.com/community)
58
+
59
+ The names and logos for thoughtbot are trademarks of thoughtbot, inc.
60
+
61
+ License
62
+ -------
63
+
64
+ factory_girl is Copyright © 2008-2011 Joe Ferris and thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file.
data/Rakefile CHANGED
@@ -1,32 +1,28 @@
1
1
  require 'rubygems'
2
+ require 'bundler/setup'
2
3
  require 'rake'
3
- require 'rake/rdoctask'
4
4
  require 'rcov/rcovtask'
5
5
  require 'date'
6
6
  require 'rake/gempackagetask'
7
-
8
- require 'spec/rake/spectask'
7
+ require 'rspec/core/rake_task'
9
8
  require 'cucumber/rake/task'
9
+ require 'appraisal'
10
+ require 'yard'
10
11
 
11
12
  desc 'Default: run the specs and features.'
12
- task :default do
13
- system("rake -s spec:unit;")
14
- %w(2.1 2.3 3.0).each do |version|
15
- system("RAILS_VERSION=#{version} rake -s spec:acceptance features;")
16
- end
13
+ task :default => 'spec:unit' do
14
+ system("rake -s appraisal spec:acceptance features;")
17
15
  end
18
16
 
19
17
  namespace :spec do
20
18
  desc "Run unit specs"
21
- Spec::Rake::SpecTask.new('unit') do |t|
22
- t.spec_opts = ['--options', "spec/spec.opts"]
23
- t.spec_files = FileList['spec/factory_girl/**/*_spec.rb']
19
+ RSpec::Core::RakeTask.new('unit') do |t|
20
+ t.pattern = 'spec/factory_girl/**/*_spec.rb'
24
21
  end
25
22
 
26
23
  desc "Run acceptance specs"
27
- Spec::Rake::SpecTask.new('acceptance') do |t|
28
- t.spec_opts = ['--options', "spec/spec.opts"]
29
- t.spec_files = FileList['spec/acceptance/**/*_spec.rb']
24
+ RSpec::Core::RakeTask.new('acceptance') do |t|
25
+ t.pattern = 'spec/acceptance/**/*_spec.rb'
30
26
  end
31
27
  end
32
28
 
@@ -39,23 +35,8 @@ Rcov::RcovTask.new do |t|
39
35
  t.verbose = true
40
36
  end
41
37
 
42
- desc 'Generate documentation for the factory_girl plugin.'
43
- Rake::RDocTask.new(:rdoc) do |rdoc|
44
- rdoc.rdoc_dir = 'rdoc'
45
- rdoc.title = 'Factory Girl'
46
- rdoc.options << '--line-numbers' << "--main" << "README.rdoc"
47
- rdoc.rdoc_files.include('README.rdoc')
48
- rdoc.rdoc_files.include('CONTRIBUTION_GUIDELINES.rdoc')
49
- rdoc.rdoc_files.include('lib/**/*.rb')
50
- end
51
-
52
- desc 'Update documentation on website'
53
- task :sync_docs => 'rdoc' do
54
- `rsync -ave ssh rdoc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/factory_girl`
55
- end
56
-
57
38
  desc "Clean files generated by rake tasks"
58
- task :clobber => [:clobber_rdoc, :clobber_rcov]
39
+ task :clobber => [:clobber_rcov]
59
40
 
60
41
  Cucumber::Rake::Task.new(:features) do |t|
61
42
  t.fork = true
@@ -67,3 +48,7 @@ Rake::GemPackageTask.new($specification) do |package|
67
48
  package.need_zip = true
68
49
  package.need_tar = true
69
50
  end
51
+
52
+ YARD::Rake::YardocTask.new do |t|
53
+ end
54
+