dima-exe-factory_girl 1.1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
- data/Changelog +29 -0
- data/LICENSE +19 -0
- data/README.textile +151 -0
- data/Rakefile +76 -0
- data/lib/factory_girl/aliases.rb +39 -0
- data/lib/factory_girl/attribute.rb +38 -0
- data/lib/factory_girl/attribute_proxy.rb +92 -0
- data/lib/factory_girl/factory.rb +337 -0
- data/lib/factory_girl/sequence.rb +58 -0
- data/lib/factory_girl.rb +27 -0
- data/test/aliases_test.rb +29 -0
- data/test/attribute_proxy_test.rb +121 -0
- data/test/attribute_test.rb +70 -0
- data/test/factory_test.rb +565 -0
- data/test/integration_test.rb +147 -0
- data/test/models.rb +51 -0
- data/test/sequence_test.rb +76 -0
- data/test/test_helper.rb +10 -0
- metadata +79 -0
@@ -0,0 +1,10 @@
|
|
1
|
+
We're using GitHub[http://github.com/thoughtbot/factory_girl/tree/master] and Lighthouse[http://thoughtbot.lighthouseapp.com/projects/14354], and we've been getting any combination of github pull requests, Lighthouse tickets, 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
|
+
* We prefer git branches over patches, but we can take either.
|
5
|
+
* If you're using git, please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier.
|
6
|
+
* If you're submitting patches, please cut each fix or feature into a separate patch.
|
7
|
+
* There should be a Lighthouse[http://thoughtbot.lighthouseapp.com/projects/14354] ticket for any submission. If you've found a bug and want to fix it, open a new ticket at the same time.
|
8
|
+
* Please <b>don't send pull requests</b> Just update the lighthouse ticket with the url for your fix (or attach the patch) 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.
|
9
|
+
* Contributions without tests won't be accepted.
|
10
|
+
* 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.textile
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
h1. factory_girl
|
2
|
+
|
3
|
+
Written by "Joe Ferris":mailto:jferris@thoughtbot.com.
|
4
|
+
|
5
|
+
Thanks to Tammer Saleh, Dan Croak, and Jon Yurek of thoughtbot, inc.
|
6
|
+
|
7
|
+
Copyright 2008 Joe Ferris and thoughtbot, inc.
|
8
|
+
|
9
|
+
h2. Download
|
10
|
+
|
11
|
+
Github: "Page":http://github.com/thoughtbot/factory_girl/tree/master "Clone":git://github.com/thoughtbot/factory_girl.git
|
12
|
+
|
13
|
+
Gem: <pre>gem install thoughtbot-factory_girl --source http://gems.github.com</pre>
|
14
|
+
|
15
|
+
Note: if you install factory_girl using the gem from Github, you'll need this
|
16
|
+
in your environment.rb if you want to use Rails 2.1's dependency manager:
|
17
|
+
|
18
|
+
config.gem "thoughtbot-factory_girl",
|
19
|
+
:lib => "factory_girl",
|
20
|
+
:source => "http://gems.github.com"
|
21
|
+
|
22
|
+
h2. Contributing
|
23
|
+
|
24
|
+
Please read the contribution guidelines before submitting patches or pull requests.
|
25
|
+
|
26
|
+
h2. Defining factories
|
27
|
+
|
28
|
+
<pre><code># This will guess the User class
|
29
|
+
Factory.define :user do |u|
|
30
|
+
u.first_name 'John'
|
31
|
+
u.last_name 'Doe'
|
32
|
+
u.admin false
|
33
|
+
end
|
34
|
+
|
35
|
+
# This will use the User class (Admin would have been guessed)
|
36
|
+
Factory.define :admin, :class => User do |u|
|
37
|
+
u.first_name 'Admin'
|
38
|
+
u.last_name 'User'
|
39
|
+
u.admin true
|
40
|
+
end</code></pre>
|
41
|
+
|
42
|
+
|
43
|
+
It is recommended that you create a test/factories.rb file and define your
|
44
|
+
factories there. This file can be included from test_helper or directly from
|
45
|
+
your test files. Don't forget:
|
46
|
+
<pre><code>require 'factory_girl'</code></pre>
|
47
|
+
|
48
|
+
|
49
|
+
h2. Lazy Attributes
|
50
|
+
|
51
|
+
Most attributes can be added using static values that are evaluated when the
|
52
|
+
factory is defined, but some attributes (such as associations and other
|
53
|
+
attributes that must be dynamically generated) will need values assigned each
|
54
|
+
time an instance is generated. These "lazy" attributes can be added by passing
|
55
|
+
a block instead of a parameter:
|
56
|
+
|
57
|
+
<pre><code>Factory.define :user do |u|
|
58
|
+
# ...
|
59
|
+
u.activation_code { User.generate_activation_code }
|
60
|
+
end</code></pre>
|
61
|
+
|
62
|
+
|
63
|
+
h2. Dependent Attributes
|
64
|
+
|
65
|
+
Some attributes may need to be generated based on the values of other
|
66
|
+
attributes. This can be done by calling the attribute name on
|
67
|
+
Factory::AttributeProxy, which is yielded to lazy attribute blocks:
|
68
|
+
|
69
|
+
<pre><code>Factory.define :user do |u|
|
70
|
+
u.first_name 'Joe'
|
71
|
+
u.last_name 'Blow'
|
72
|
+
u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
|
73
|
+
end
|
74
|
+
|
75
|
+
Factory(:user, :last_name => 'Doe').email
|
76
|
+
# => "joe.doe@example.com"</code></pre>
|
77
|
+
|
78
|
+
|
79
|
+
h2. Associations
|
80
|
+
|
81
|
+
Associated instances can be generated by using the association method when
|
82
|
+
defining a lazy attribute:
|
83
|
+
|
84
|
+
<pre><code>Factory.define :post do |p|
|
85
|
+
# ...
|
86
|
+
p.author {|author| author.association(:user, :last_name => 'Writely') }
|
87
|
+
end</code></pre>
|
88
|
+
|
89
|
+
|
90
|
+
When using the association method, the same build strategy (build, create, or attributes_for) will be used for all generated instances:
|
91
|
+
|
92
|
+
<pre><code># Builds and saves a User and a Post
|
93
|
+
post = Factory(:post)
|
94
|
+
post.new_record? # => false
|
95
|
+
post.author.new_record # => false
|
96
|
+
|
97
|
+
# Builds but does not save a User and a Post
|
98
|
+
Factory.build(:post)
|
99
|
+
post.new_record? # => true
|
100
|
+
post.author.new_record # => true</code></pre>
|
101
|
+
|
102
|
+
Because this pattern is so common, a prettier syntax is available for defining
|
103
|
+
associations:
|
104
|
+
|
105
|
+
<pre><code># The following definitions are equivilent:
|
106
|
+
Factory.define :post do |p|
|
107
|
+
p.author {|a| a.association(:user) }
|
108
|
+
end
|
109
|
+
|
110
|
+
Factory.define :post do |p|
|
111
|
+
p.association :author, :factory => :user
|
112
|
+
end</code></pre>
|
113
|
+
|
114
|
+
If the factory name is the same as the association name, the factory name can
|
115
|
+
be left out.
|
116
|
+
|
117
|
+
|
118
|
+
h2. Sequences
|
119
|
+
|
120
|
+
Unique values in a specific format (for example, e-mail addresses) can be
|
121
|
+
generated using sequences. Sequences are defined by calling Factory.sequence,
|
122
|
+
and values in a sequence are generated by calling Factory.next:
|
123
|
+
|
124
|
+
<pre><code># Defines a new sequence
|
125
|
+
Factory.sequence :email do |n|
|
126
|
+
"person#{n}@example.com"
|
127
|
+
end
|
128
|
+
|
129
|
+
Factory.next :email
|
130
|
+
# => "person1@example.com"
|
131
|
+
|
132
|
+
Factory.next :email
|
133
|
+
# => "person2@example.com"</code></pre>
|
134
|
+
|
135
|
+
|
136
|
+
h2. Using factories
|
137
|
+
|
138
|
+
<pre><code># Build and save a User instance
|
139
|
+
Factory(:user)
|
140
|
+
|
141
|
+
# Build a User instance and override the first_name property
|
142
|
+
Factory.build(:user, :first_name => 'Joe')
|
143
|
+
|
144
|
+
# Return an attributes Hash that can be used to build a User instance
|
145
|
+
attrs = Factory.attributes_for(:user)</code></pre>
|
146
|
+
|
147
|
+
h2. More Information
|
148
|
+
|
149
|
+
"Our blog":http://giantrobots.thoughtbot.com
|
150
|
+
|
151
|
+
"factory_girl rdoc":http://dev.thoughtbot.com/factory_girl
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
require 'rcov/rcovtask'
|
7
|
+
require 'date'
|
8
|
+
|
9
|
+
desc 'Default: run unit tests.'
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
desc 'Test the factory_girl plugin.'
|
13
|
+
Rake::TestTask.new(:test) do |t|
|
14
|
+
t.libs << 'lib'
|
15
|
+
t.pattern = 'test/**/*_test.rb'
|
16
|
+
t.verbose = true
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Performs code coverage on the factory_girl plugin.'
|
20
|
+
Rcov::RcovTask.new do |t|
|
21
|
+
t.libs << "test"
|
22
|
+
t.test_files = FileList['test/*_test.rb']
|
23
|
+
t.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Generate documentation for the factory_girl plugin.'
|
27
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
28
|
+
rdoc.rdoc_dir = 'rdoc'
|
29
|
+
rdoc.title = 'Factory Girl'
|
30
|
+
rdoc.options << '--line-numbers' << '--inline-source' << "--main" << "README.textile"
|
31
|
+
rdoc.rdoc_files.include('README.textile')
|
32
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
33
|
+
end
|
34
|
+
|
35
|
+
desc 'Update documentation on website'
|
36
|
+
task :sync_docs => 'rdoc' do
|
37
|
+
`rsync -ave ssh rdoc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/factory_girl`
|
38
|
+
end
|
39
|
+
|
40
|
+
spec = Gem::Specification.new do |s|
|
41
|
+
s.name = %q{factory_girl}
|
42
|
+
s.version = "1.1.5"
|
43
|
+
s.summary = %q{factory_girl provides a framework and DSL for defining and
|
44
|
+
using model instance factories.}
|
45
|
+
s.description = %q{factory_girl provides a framework and DSL for defining and
|
46
|
+
using factories - less error-prone, more explicit, and
|
47
|
+
all-around easier to work with than fixtures.}
|
48
|
+
|
49
|
+
s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'test/**/*.rb']
|
50
|
+
s.require_path = 'lib'
|
51
|
+
s.test_files = Dir[*['test/**/*_test.rb']]
|
52
|
+
|
53
|
+
s.has_rdoc = true
|
54
|
+
s.extra_rdoc_files = ["README.textile"]
|
55
|
+
s.rdoc_options = ['--line-numbers', '--inline-source', "--main", "README.textile"]
|
56
|
+
|
57
|
+
s.authors = ["Joe Ferris"]
|
58
|
+
s.email = %q{jferris@thoughtbot.com}
|
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
|
@@ -0,0 +1,39 @@
|
|
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 insert like String#sub.
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
#
|
23
|
+
# Factory.alias /(.*)_confirmation/, '\1'
|
24
|
+
def self.alias (pattern, replace)
|
25
|
+
self.aliases << [pattern, replace]
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.aliases_for (attribute) #:nodoc:
|
29
|
+
aliases.collect do |params|
|
30
|
+
pattern, replace = *params
|
31
|
+
if pattern.match(attribute.to_s)
|
32
|
+
attribute.to_s.sub(pattern, replace).to_sym
|
33
|
+
else
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
end.compact << attribute
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Factory
|
2
|
+
|
3
|
+
class AttributeDefinitionError < RuntimeError
|
4
|
+
end
|
5
|
+
|
6
|
+
class Attribute #:nodoc:
|
7
|
+
|
8
|
+
attr_reader :name
|
9
|
+
|
10
|
+
def initialize (name, static_value, lazy_block)
|
11
|
+
name = name.to_sym
|
12
|
+
|
13
|
+
if name.to_s =~ /=$/
|
14
|
+
raise AttributeDefinitionError,
|
15
|
+
"factory_girl uses 'f.#{name.to_s.chop} value' syntax " +
|
16
|
+
"rather than 'f.#{name} = value'"
|
17
|
+
end
|
18
|
+
|
19
|
+
unless static_value.nil? || lazy_block.nil?
|
20
|
+
raise AttributeDefinitionError, "Both value and block given"
|
21
|
+
end
|
22
|
+
|
23
|
+
@name = name
|
24
|
+
@static_value = static_value
|
25
|
+
@lazy_block = lazy_block
|
26
|
+
end
|
27
|
+
|
28
|
+
def value (proxy)
|
29
|
+
if @lazy_block.nil?
|
30
|
+
@static_value
|
31
|
+
else
|
32
|
+
@lazy_block.call(proxy)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
class Factory
|
2
|
+
|
3
|
+
class AttributeProxy
|
4
|
+
|
5
|
+
attr_accessor :factory, :attribute_name, :strategy, :current_values #:nodoc:
|
6
|
+
|
7
|
+
def initialize (factory, attr, strategy, values) #:nodoc:
|
8
|
+
@factory = factory
|
9
|
+
@attribute_name = attr
|
10
|
+
@strategy = strategy
|
11
|
+
@current_values = values
|
12
|
+
end
|
13
|
+
|
14
|
+
# Generates an association using the current build strategy.
|
15
|
+
#
|
16
|
+
# Arguments:
|
17
|
+
# name: (Symbol)
|
18
|
+
# The name of the factory that should be used to generate this
|
19
|
+
# association.
|
20
|
+
# attributes: (Hash)
|
21
|
+
# A hash of attributes that should be overridden for this association.
|
22
|
+
#
|
23
|
+
# Returns:
|
24
|
+
# The generated association for the current build strategy. Note that
|
25
|
+
# assocaitions are not generated for the attributes_for strategy. Returns
|
26
|
+
# nil in this case.
|
27
|
+
#
|
28
|
+
# Example:
|
29
|
+
#
|
30
|
+
# Factory.define :user do |f|
|
31
|
+
# # ...
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# Factory.define :post do |f|
|
35
|
+
# # ...
|
36
|
+
# f.author {|a| a.association :user, :name => 'Joe' }
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# # Builds (but doesn't save) a Post and a User
|
40
|
+
# Factory.build(:post)
|
41
|
+
#
|
42
|
+
# # Builds and saves a User, builds a Post, assigns the User to the
|
43
|
+
# # author association, and saves the User.
|
44
|
+
# Factory.create(:post)
|
45
|
+
#
|
46
|
+
def association (name, attributes = {})
|
47
|
+
if strategy == :attributes_for
|
48
|
+
nil
|
49
|
+
else
|
50
|
+
Factory.create(name, attributes)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns the value for specified attribute. A value will only be available
|
55
|
+
# if it was overridden when calling the factory, or if a value is added
|
56
|
+
# earlier in the definition of the factory.
|
57
|
+
#
|
58
|
+
# Arguments:
|
59
|
+
# attribute: (Symbol)
|
60
|
+
# The attribute whose value should be returned.
|
61
|
+
#
|
62
|
+
# Returns:
|
63
|
+
# The value of the requested attribute. (Object)
|
64
|
+
def value_for (attribute)
|
65
|
+
unless current_values.key?(attribute)
|
66
|
+
raise ArgumentError, "No such attribute: #{attribute.inspect}"
|
67
|
+
end
|
68
|
+
current_values[attribute]
|
69
|
+
end
|
70
|
+
|
71
|
+
# Undefined methods are delegated to value_for, which means that:
|
72
|
+
#
|
73
|
+
# Factory.define :user do |f|
|
74
|
+
# f.first_name 'Ben'
|
75
|
+
# f.last_name {|a| a.value_for(:first_name) }
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# and:
|
79
|
+
#
|
80
|
+
# Factory.define :user do |f|
|
81
|
+
# f.first_name 'Ben'
|
82
|
+
# f.last_name {|a| a.first_name }
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# are equivilent.
|
86
|
+
def method_missing (name, *args, &block)
|
87
|
+
current_values[name]
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|