thoughtbot-factory_girl 1.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,31 +1,41 @@
1
- = factory_girl
1
+ h1. factory_girl
2
2
 
3
- written by Joe Ferris <jferris@thoughtbot.com>
4
- thanks to Tammer Saleh, Dan Croak, and Jon Yurek of thoughtbot, inc.
5
- Copyright 2008 Joe Ferris and thoughtbot, inc.
3
+ Written by "Joe Ferris":mailto:jferris@thoughtbot.com.
6
4
 
7
- == Defining factories
5
+ Thanks to Tammer Saleh, Dan Croak, and Jon Yurek of thoughtbot, inc.
8
6
 
9
- # This will guess the User class
10
- Factory.define :user do |u|
11
- u.first_name 'John'
12
- u.last_name 'Doe'
13
- u.admin false
14
- end
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
+ h2. Defining factories
16
+
17
+ <pre><code># This will guess the User class
18
+ Factory.define :user do |u|
19
+ u.first_name 'John'
20
+ u.last_name 'Doe'
21
+ u.admin false
22
+ end
23
+
24
+ # This will use the User class (Admin would have been guessed)
25
+ Factory.define :admin, :class => User do |u|
26
+ u.first_name 'Admin'
27
+ u.last_name 'User'
28
+ u.admin true
29
+ end</code></pre>
15
30
 
16
- # This will use the User class (Admin would have been guessed)
17
- Factory.define :admin, :class => User do |u|
18
- u.first_name 'Admin'
19
- u.last_name 'User'
20
- u.admin true
21
- end
22
31
 
23
32
  It is recommended that you create a test/factories.rb file and define your
24
33
  factories there. This file can be included from test_helper or directly from
25
34
  your test files. Don't forget:
26
- require 'factory_girl'
35
+ <pre><code>require 'factory_girl'</code></pre>
36
+
27
37
 
28
- == Lazy Attributes
38
+ h2. Lazy Attributes
29
39
 
30
40
  Most attributes can be added using static values that are evaluated when the
31
41
  factory is defined, but some attributes (such as associations and other
@@ -33,73 +43,83 @@ attributes that must be dynamically generated) will need values assigned each
33
43
  time an instance is generated. These "lazy" attributes can be added by passing
34
44
  a block instead of a parameter:
35
45
 
36
- Factory.define :user do |u|
37
- # ...
38
- u.activation_code { User.generate_activation_code }
39
- end
46
+ <pre><code>Factory.define :user do |u|
47
+ # ...
48
+ u.activation_code { User.generate_activation_code }
49
+ end</code></pre>
40
50
 
41
- == Dependent Attributes
51
+
52
+ h2. Dependent Attributes
42
53
 
43
54
  Some attributes may need to be generated based on the values of other
44
55
  attributes. This can be done by calling the attribute name on
45
56
  Factory::AttributeProxy, which is yielded to lazy attribute blocks:
46
57
 
47
- Factory.define :user do |u|
48
- u.first_name 'Joe'
49
- u.last_name 'Blow'
50
- u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
51
- end
58
+ <pre><code>Factory.define :user do |u|
59
+ u.first_name 'Joe'
60
+ u.last_name 'Blow'
61
+ u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
62
+ end
63
+
64
+ Factory(:user, :last_name => 'Doe').email
65
+ # => "joe.doe@example.com"</code></pre>
52
66
 
53
- Factory(:user, :last_name => 'Doe').email
54
- # => "joe.doe@example.com"
55
67
 
56
- == Associations
68
+ h2. Associations
57
69
 
58
70
  Associated instances can be generated by using the association method when
59
71
  defining a lazy attribute:
60
72
 
61
- Factory.define :post do |p|
62
- # ...
63
- p.author {|author| author.association(:user, :last_name => 'Writely') }
64
- end
73
+ <pre><code>Factory.define :post do |p|
74
+ # ...
75
+ p.author {|author| author.association(:user, :last_name => 'Writely') }
76
+ end</code></pre>
77
+
65
78
 
66
79
  When using the association method, the same build strategy (build, create, or attributes_for) will be used for all generated instances:
67
80
 
68
- # Builds and saves a User and a Post
69
- post = Factory(:post)
70
- post.new_record? # => false
71
- post.author.new_record # => false
81
+ <pre><code># Builds and saves a User and a Post
82
+ post = Factory(:post)
83
+ post.new_record? # => false
84
+ post.author.new_record # => false
85
+
86
+ # Builds but does not save a User and a Post
87
+ Factory.build(:post)
88
+ post.new_record? # => true
89
+ post.author.new_record # => true</code></pre>
72
90
 
73
- # Builds but does not save a User and a Post
74
- Factory.build(:post)
75
- post.new_record? # => true
76
- post.author.new_record # => true
77
91
 
78
- == Sequences
92
+ h2. Sequences
79
93
 
80
94
  Unique values in a specific format (for example, e-mail addresses) can be
81
95
  generated using sequences. Sequences are defined by calling Factory.sequence,
82
96
  and values in a sequence are generated by calling Factory.next:
83
97
 
84
- # Defines a new sequence
85
- Factory.sequence :email do |n|
86
- "person#{n}@example.com"
87
- end
98
+ <pre><code># Defines a new sequence
99
+ Factory.sequence :email do |n|
100
+ "person#{n}@example.com"
101
+ end
102
+
103
+ Factory.next :email
104
+ # => "person1@example.com"
105
+
106
+ Factory.next :email
107
+ # => "person2@example.com"</code></pre>
108
+
88
109
 
89
- Factory.next :email
90
- # => "person1@example.com"
110
+ h2. Using factories
91
111
 
92
- Factory.next :email
93
- # => "person2@example.com"
112
+ <pre><code># Build and save a User instance
113
+ Factory(:user)
94
114
 
95
- == Using factories
115
+ # Build a User instance and override the first_name property
116
+ Factory.build(:user, :first_name => 'Joe')
96
117
 
97
- # Build and save a User instance
98
- Factory(:user)
118
+ # Return an attributes Hash that can be used to build a User instance
119
+ attrs = Factory.attributes_for(:user)</code></pre>
99
120
 
100
- # Build a User instance and override the first_name property
101
- Factory.build(:user, :first_name => 'Joe')
121
+ h2. More Information
102
122
 
103
- # Return an attributes Hash that can be used to build a User instance
104
- attrs = Factory.attributes_for(:user)
123
+ "Our blog":http://giantrobots.thoughtbot.com
105
124
 
125
+ "factory_girl rdoc":http://dev.thoughtbot.com/factory_girl
data/Rakefile CHANGED
@@ -31,7 +31,7 @@ end
31
31
 
32
32
  spec = Gem::Specification.new do |s|
33
33
  s.name = %q{factory_girl}
34
- s.version = "1.1"
34
+ s.version = "1.1.1"
35
35
  s.summary = %q{factory_girl provides a framework and DSL for defining and
36
36
  using model instance factories.}
37
37
  s.description = %q{factory_girl provides a framework and DSL for defining and
@@ -4,7 +4,7 @@ class Factory
4
4
  self.factories = {}
5
5
  self.sequences = {}
6
6
 
7
- attr_reader :name
7
+ attr_reader :factory_name
8
8
 
9
9
  # Defines a new factory that can be used by the build strategies (create and
10
10
  # build) to build new objects.
@@ -61,13 +61,13 @@ class Factory
61
61
  end
62
62
 
63
63
  def build_class #:nodoc:
64
- @build_class ||= @options[:class] || name.to_s.classify.constantize
64
+ @build_class ||= @options[:class] || factory_name.to_s.classify.constantize
65
65
  end
66
66
 
67
67
  def initialize (name, options = {}) #:nodoc:
68
68
  options.assert_valid_keys(:class)
69
- @name = name
70
- @options = options
69
+ @factory_name = name
70
+ @options = options
71
71
 
72
72
  @static_attributes = {}
73
73
  @lazy_attribute_blocks = {}
@@ -114,7 +114,7 @@ class Factory
114
114
  # and:
115
115
  #
116
116
  # Factory.define :user do |f|
117
- # f.add_attribute :user, 'Billy Idol'
117
+ # f.add_attribute :name, 'Billy Idol'
118
118
  # end
119
119
  #
120
120
  # are equivilent.
@@ -82,8 +82,8 @@ class FactoryTest < Test::Unit::TestCase
82
82
  @factory = Factory.new(@name)
83
83
  end
84
84
 
85
- should "have a name" do
86
- assert_equal @name, @factory.name
85
+ should "have a factory name" do
86
+ assert_equal @name, @factory.factory_name
87
87
  end
88
88
 
89
89
  should "have a build class" do
@@ -11,7 +11,7 @@ class IntegrationTest < Test::Unit::TestCase
11
11
  end
12
12
 
13
13
  Factory.define :post do |f|
14
- f.title 'Test Post'
14
+ f.name 'Test Post'
15
15
  f.author {|a| a.association(:user) }
16
16
  end
17
17
 
@@ -13,7 +13,7 @@ class CreateSchema < ActiveRecord::Migration
13
13
  end
14
14
 
15
15
  create_table :posts, :force => true do |t|
16
- t.string :title
16
+ t.string :name
17
17
  t.integer :author_id
18
18
  end
19
19
  end
@@ -27,6 +27,6 @@ class User < ActiveRecord::Base
27
27
  end
28
28
 
29
29
  class Post < ActiveRecord::Base
30
- validates_presence_of :title, :author_id
30
+ validates_presence_of :name, :author_id
31
31
  belongs_to :author, :class_name => 'User'
32
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thoughtbot-factory_girl
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.1"
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Ferris
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-06 00:00:00 -07:00
12
+ date: 2008-06-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements: []
69
69
 
70
70
  rubyforge_project:
71
- rubygems_version: 1.0.1
71
+ rubygems_version: 1.2.0
72
72
  signing_key:
73
73
  specification_version: 2
74
74
  summary: factory_girl provides a framework and DSL for defining and using model instance factories.