factory_girl 1.1.5 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.rdoc +228 -0
  2. data/Rakefile +7 -5
  3. data/lib/factory_girl.rb +11 -4
  4. data/lib/factory_girl/aliases.rb +18 -7
  5. data/lib/factory_girl/attribute.rb +10 -20
  6. data/lib/factory_girl/attribute/association.rb +18 -0
  7. data/lib/factory_girl/attribute/dynamic.rb +17 -0
  8. data/lib/factory_girl/attribute/static.rb +17 -0
  9. data/lib/factory_girl/factory.rb +199 -119
  10. data/lib/factory_girl/proxy.rb +62 -0
  11. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  12. data/lib/factory_girl/proxy/build.rb +29 -0
  13. data/lib/factory_girl/proxy/create.rb +10 -0
  14. data/lib/factory_girl/proxy/stub.rb +28 -0
  15. data/lib/factory_girl/sequence.rb +2 -0
  16. data/lib/factory_girl/syntax.rb +12 -0
  17. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  18. data/lib/factory_girl/syntax/generate.rb +68 -0
  19. data/lib/factory_girl/syntax/make.rb +39 -0
  20. data/lib/factory_girl/syntax/sham.rb +42 -0
  21. data/test/aliases_test.rb +1 -1
  22. data/test/association_attribute_test.rb +31 -0
  23. data/test/attribute_test.rb +8 -46
  24. data/test/attributes_for_strategy_test.rb +55 -0
  25. data/test/build_strategy_test.rb +79 -0
  26. data/test/create_strategy_test.rb +90 -0
  27. data/test/dynamic_attribute_test.rb +41 -0
  28. data/test/factory_test.rb +255 -243
  29. data/test/integration_test.rb +91 -3
  30. data/test/models.rb +1 -0
  31. data/test/sequence_test.rb +1 -1
  32. data/test/static_attribute_test.rb +33 -0
  33. data/test/strategy_test.rb +33 -0
  34. data/test/stub_strategy_test.rb +52 -0
  35. data/test/syntax/blueprint_test.rb +39 -0
  36. data/test/syntax/generate_test.rb +63 -0
  37. data/test/syntax/make_test.rb +39 -0
  38. data/test/syntax/sham_test.rb +40 -0
  39. data/test/test_helper.rb +1 -0
  40. metadata +42 -8
  41. data/README.textile +0 -151
  42. data/lib/factory_girl/attribute_proxy.rb +0 -92
  43. data/test/attribute_proxy_test.rb +0 -121
@@ -5,6 +5,7 @@ require 'rubygems'
5
5
  require 'test/unit'
6
6
  require 'activerecord'
7
7
  require 'factory_girl'
8
+ gem 'thoughtbot-shoulda', ">= 2.0.0"
8
9
  require 'shoulda'
9
10
  require 'mocha'
10
11
  require 'models'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_girl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.2.0
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-12-11 00:00:00 -05:00
12
+ date: 2009-02-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -20,26 +20,49 @@ executables: []
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README.textile
23
+ - README.rdoc
24
24
  files:
25
25
  - Changelog
26
26
  - CONTRIBUTION_GUIDELINES.rdoc
27
27
  - LICENSE
28
28
  - Rakefile
29
- - README.textile
29
+ - README.rdoc
30
30
  - lib/factory_girl/aliases.rb
31
+ - lib/factory_girl/attribute/association.rb
32
+ - lib/factory_girl/attribute/dynamic.rb
33
+ - lib/factory_girl/attribute/static.rb
31
34
  - lib/factory_girl/attribute.rb
32
- - lib/factory_girl/attribute_proxy.rb
33
35
  - lib/factory_girl/factory.rb
36
+ - lib/factory_girl/proxy/attributes_for.rb
37
+ - lib/factory_girl/proxy/build.rb
38
+ - lib/factory_girl/proxy/create.rb
39
+ - lib/factory_girl/proxy/stub.rb
40
+ - lib/factory_girl/proxy.rb
34
41
  - lib/factory_girl/sequence.rb
42
+ - lib/factory_girl/syntax/blueprint.rb
43
+ - lib/factory_girl/syntax/generate.rb
44
+ - lib/factory_girl/syntax/make.rb
45
+ - lib/factory_girl/syntax/sham.rb
46
+ - lib/factory_girl/syntax.rb
35
47
  - lib/factory_girl.rb
36
48
  - test/aliases_test.rb
37
- - test/attribute_proxy_test.rb
49
+ - test/association_attribute_test.rb
38
50
  - test/attribute_test.rb
51
+ - test/attributes_for_strategy_test.rb
52
+ - test/build_strategy_test.rb
53
+ - test/create_strategy_test.rb
54
+ - test/dynamic_attribute_test.rb
39
55
  - test/factory_test.rb
40
56
  - test/integration_test.rb
41
57
  - test/models.rb
42
58
  - test/sequence_test.rb
59
+ - test/static_attribute_test.rb
60
+ - test/strategy_test.rb
61
+ - test/stub_strategy_test.rb
62
+ - test/syntax/blueprint_test.rb
63
+ - test/syntax/generate_test.rb
64
+ - test/syntax/make_test.rb
65
+ - test/syntax/sham_test.rb
43
66
  - test/test_helper.rb
44
67
  has_rdoc: true
45
68
  homepage:
@@ -50,7 +73,7 @@ rdoc_options:
50
73
  - --line-numbers
51
74
  - --inline-source
52
75
  - --main
53
- - README.textile
76
+ - README.rdoc
54
77
  require_paths:
55
78
  - lib
56
79
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -74,8 +97,19 @@ specification_version: 2
74
97
  summary: factory_girl provides a framework and DSL for defining and using model instance factories.
75
98
  test_files:
76
99
  - test/aliases_test.rb
77
- - test/attribute_proxy_test.rb
100
+ - test/association_attribute_test.rb
78
101
  - test/attribute_test.rb
102
+ - test/attributes_for_strategy_test.rb
103
+ - test/build_strategy_test.rb
104
+ - test/create_strategy_test.rb
105
+ - test/dynamic_attribute_test.rb
79
106
  - test/factory_test.rb
80
107
  - test/integration_test.rb
81
108
  - test/sequence_test.rb
109
+ - test/static_attribute_test.rb
110
+ - test/strategy_test.rb
111
+ - test/stub_strategy_test.rb
112
+ - test/syntax/blueprint_test.rb
113
+ - test/syntax/generate_test.rb
114
+ - test/syntax/make_test.rb
115
+ - test/syntax/sham_test.rb
@@ -1,151 +0,0 @@
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
@@ -1,92 +0,0 @@
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
@@ -1,121 +0,0 @@
1
- require(File.join(File.dirname(__FILE__), 'test_helper'))
2
-
3
- class AttributeProxyTest < Test::Unit::TestCase
4
-
5
- context "an association proxy" do
6
-
7
- setup do
8
- @factory = mock('factory')
9
- @attr = :user
10
- @attrs = { :first_name => 'John' }
11
- @strategy = :create
12
- @proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
13
- end
14
-
15
- should "have a factory" do
16
- assert_equal @factory, @proxy.factory
17
- end
18
-
19
- should "have an attribute name" do
20
- assert_equal @attr, @proxy.attribute_name
21
- end
22
-
23
- should "have a build strategy" do
24
- assert_equal @strategy, @proxy.strategy
25
- end
26
-
27
- should "have attributes" do
28
- assert_equal @attrs, @proxy.current_values
29
- end
30
-
31
- context "building an association" do
32
-
33
- setup do
34
- @association = mock('built-user')
35
- @name = :user
36
- @attribs = { :first_name => 'Billy' }
37
-
38
- Factory.stubs(@strategy).returns(@association)
39
- end
40
-
41
- should "delegate to the appropriate method on Factory" do
42
- Factory.expects(@strategy).with(@name, @attribs).returns(@association)
43
- @proxy.association(@name, @attribs)
44
- end
45
-
46
- should "return the built association" do
47
- assert_equal @association, @proxy.association(@name)
48
- end
49
-
50
- end
51
-
52
- context "building an association using the attributes_for strategy" do
53
-
54
- setup do
55
- @strategy = :attributes_for
56
- @proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
57
- end
58
-
59
- should "not build the association" do
60
- Factory.expects(@strategy).never
61
- @proxy.association(:user)
62
- end
63
-
64
- should "return nil for the association" do
65
- Factory.stubs(@strategy).returns(:user)
66
- assert_nil @proxy.association(:user)
67
- end
68
-
69
- end
70
-
71
- context "building an association using the build strategy" do
72
-
73
- setup do
74
- @strategy = :build
75
- @built = 'object'
76
- @proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
77
- Factory.stubs(:create).returns(@built)
78
- end
79
-
80
- should "create the association" do
81
- Factory.expects(:create).with(:user, {}).returns(@built)
82
- @proxy.association(:user)
83
- end
84
-
85
- should "return the created object" do
86
- assert_equal @built, @proxy.association(:user)
87
- end
88
-
89
- end
90
-
91
- context "fetching the value of an attribute" do
92
-
93
- setup do
94
- @attr = :first_name
95
- end
96
-
97
- should "return the correct value" do
98
- assert_equal @attrs[@attr], @proxy.value_for(@attr)
99
- end
100
-
101
- should "call value_for for undefined methods" do
102
- assert_equal @attrs[@attr], @proxy.send(@attr)
103
- end
104
-
105
- end
106
-
107
- context "fetching the value of an undefined attribute" do
108
-
109
- setup do
110
- @attr = :beachball
111
- end
112
-
113
- should "raise an ArgumentError" do
114
- assert_raise(ArgumentError) { @proxy.value_for(@attr) }
115
- end
116
-
117
- end
118
-
119
- end
120
-
121
- end