factory_girl 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +4 -5
  2. data/README.rdoc +6 -6
  3. data/Rakefile +11 -14
  4. data/lib/factory_girl/attribute.rb +3 -2
  5. data/lib/factory_girl/attribute/dynamic.rb +5 -2
  6. data/lib/factory_girl/proxy/stub.rb +33 -12
  7. data/lib/factory_girl/sequence.rb +3 -0
  8. data/spec/factory_girl/aliases_spec.rb +29 -0
  9. data/spec/factory_girl/attribute/association_spec.rb +25 -0
  10. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  11. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  12. data/spec/factory_girl/attribute_spec.rb +30 -0
  13. data/spec/factory_girl/factory_spec.rb +490 -0
  14. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  15. data/spec/factory_girl/proxy/build_spec.rb +73 -0
  16. data/spec/factory_girl/proxy/create_spec.rb +83 -0
  17. data/spec/factory_girl/proxy/stub_spec.rb +69 -0
  18. data/spec/factory_girl/proxy_spec.rb +28 -0
  19. data/spec/factory_girl/sequence_spec.rb +66 -0
  20. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  21. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  22. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  23. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  24. data/spec/integration_spec.rb +265 -0
  25. data/{test → spec}/models.rb +0 -0
  26. data/{test/test_helper.rb → spec/spec_helper.rb} +12 -5
  27. metadata +44 -42
  28. data/test/aliases_test.rb +0 -29
  29. data/test/association_attribute_test.rb +0 -31
  30. data/test/attribute_test.rb +0 -32
  31. data/test/attributes_for_strategy_test.rb +0 -55
  32. data/test/build_strategy_test.rb +0 -79
  33. data/test/create_strategy_test.rb +0 -90
  34. data/test/dynamic_attribute_test.rb +0 -41
  35. data/test/factory_test.rb +0 -513
  36. data/test/integration_test.rb +0 -235
  37. data/test/sequence_test.rb +0 -76
  38. data/test/static_attribute_test.rb +0 -33
  39. data/test/strategy_test.rb +0 -33
  40. data/test/stub_strategy_test.rb +0 -61
  41. data/test/syntax/blueprint_test.rb +0 -39
  42. data/test/syntax/generate_test.rb +0 -63
  43. data/test/syntax/make_test.rb +0 -39
  44. data/test/syntax/sham_test.rb +0 -40
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/make'
4
+
5
+ describe "a factory using make syntax" do
6
+ before do
7
+ Factory.define :user do |factory|
8
+ factory.first_name 'Bill'
9
+ factory.last_name 'Nye'
10
+ factory.email 'science@guys.net'
11
+ end
12
+ end
13
+
14
+ after do
15
+ Factory.factories.clear
16
+ end
17
+
18
+ describe "after making an instance" do
19
+ before do
20
+ @instance = User.make(:last_name => 'Rye')
21
+ end
22
+
23
+ it "should use attributes from the factory" do
24
+ @instance.first_name.should == 'Bill'
25
+ end
26
+
27
+ it "should use attributes passed to make" do
28
+ @instance.last_name.should == 'Rye'
29
+ end
30
+
31
+ it "should save the record" do
32
+ @instance.should_not be_new_record
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/sham'
4
+
5
+ describe "a factory using sham syntax" do
6
+ before do
7
+ Sham.name { "Name" }
8
+ Sham.email { "somebody#{rand(5)}@example.com" }
9
+
10
+ Factory.define :user do |factory|
11
+ factory.first_name { Sham.name }
12
+ factory.last_name { Sham.name }
13
+ factory.email { Sham.email }
14
+ end
15
+ end
16
+
17
+ after do
18
+ Factory.factories.clear
19
+ Factory.sequences.clear
20
+ end
21
+
22
+ describe "after making an instance" do
23
+ before do
24
+ @instance = Factory(:user, :last_name => 'Rye')
25
+ end
26
+
27
+ it "should support a sham called 'name'" do
28
+ @instance.first_name.should == 'Name'
29
+ end
30
+
31
+ it "should use the sham for the email" do
32
+ @instance.email.should =~ /somebody\d@example.com/
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,265 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ describe "integration" do
4
+ before do
5
+ Factory.define :user, :class => 'user' do |f|
6
+ f.first_name 'Jimi'
7
+ f.last_name 'Hendrix'
8
+ f.admin false
9
+ f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
10
+ end
11
+
12
+ Factory.define Post, :default_strategy => :attributes_for do |f|
13
+ f.name 'Test Post'
14
+ f.association :author, :factory => :user
15
+ end
16
+
17
+ Factory.define :admin, :class => User do |f|
18
+ f.first_name 'Ben'
19
+ f.last_name 'Stein'
20
+ f.admin true
21
+ f.sequence(:username) { |n| "username#{n}" }
22
+ f.email { Factory.next(:email) }
23
+ end
24
+
25
+ Factory.define :sequence_abuser, :class => User do |f|
26
+ f.first_name { Factory.sequence(:email) }
27
+ end
28
+
29
+ Factory.define :guest, :parent => :user do |f|
30
+ f.last_name 'Anonymous'
31
+ f.username 'GuestUser'
32
+ end
33
+
34
+ Factory.sequence :email do |n|
35
+ "somebody#{n}@example.com"
36
+ end
37
+ end
38
+
39
+ after do
40
+ Factory.factories.clear
41
+ end
42
+
43
+ describe "a generated attributes hash" do
44
+
45
+ before do
46
+ @attrs = Factory.attributes_for(:user, :first_name => 'Bill')
47
+ end
48
+
49
+ it "should assign all attributes" do
50
+ expected_attrs = [:admin, :email, :first_name, :last_name]
51
+ actual_attrs = @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
52
+ actual_attrs.should == expected_attrs
53
+ end
54
+
55
+ it "should correctly assign lazy, dependent attributes" do
56
+ @attrs[:email].should == "bill.hendrix@example.com"
57
+ end
58
+
59
+ it "should override attrbutes" do
60
+ @attrs[:first_name].should == 'Bill'
61
+ end
62
+
63
+ it "should not assign associations" do
64
+ Factory.attributes_for(:post)[:author].should be_nil
65
+ end
66
+
67
+ end
68
+
69
+ describe "a built instance" do
70
+
71
+ before do
72
+ @instance = Factory.build(:post)
73
+ end
74
+
75
+ it "should not be saved" do
76
+ @instance.should be_new_record
77
+ end
78
+
79
+ it "should assign associations" do
80
+ @instance.author.should be_kind_of(User)
81
+ end
82
+
83
+ it "should save associations" do
84
+ @instance.author.should_not be_new_record
85
+ end
86
+
87
+ it "should not assign both an association and its foreign key" do
88
+ Factory.build(:post, :author_id => 1).author_id.should == 1
89
+ end
90
+
91
+ end
92
+
93
+ describe "a created instance" do
94
+
95
+ before do
96
+ @instance = Factory.create('post')
97
+ end
98
+
99
+ it "should be saved" do
100
+ @instance.should_not be_new_record
101
+ end
102
+
103
+ it "should assign associations" do
104
+ @instance.author.should be_kind_of(User)
105
+ end
106
+
107
+ it "should save associations" do
108
+ @instance.author.should_not be_new_record
109
+ end
110
+
111
+ end
112
+
113
+ describe "a generated stub instance" do
114
+
115
+ before do
116
+ @stub = Factory.stub(:user, :first_name => 'Bill')
117
+ end
118
+
119
+ it "should assign all attributes" do
120
+ [:admin, :email, :first_name, :last_name].each do |attr|
121
+ @stub.send(attr).should_not be_nil
122
+ end
123
+ end
124
+
125
+ it "should correctly assign attributes" do
126
+ @stub.email.should == "bill.hendrix@example.com"
127
+ end
128
+
129
+ it "should override attributes" do
130
+ @stub.first_name.should == 'Bill'
131
+ end
132
+
133
+ it "should assign associations" do
134
+ Factory.stub(:post).author.should_not be_nil
135
+ end
136
+
137
+ it "should have an id" do
138
+ @stub.id.should > 0
139
+ end
140
+
141
+ it "should have unique IDs" do
142
+ @other_stub = Factory.stub(:user)
143
+ @stub.id.should_not == @other_stub.id
144
+ end
145
+
146
+ it "should not be considered a new record" do
147
+ @stub.should_not be_new_record
148
+ end
149
+
150
+ it "should raise exception if connection to the database is attempted" do
151
+ lambda { @stub.connection }.should raise_error(RuntimeError)
152
+ lambda { @stub.update_attribute(:first_name, "Nick") }.should raise_error(RuntimeError)
153
+ lambda { @stub.reload }.should raise_error(RuntimeError)
154
+ lambda { @stub.destroy }.should raise_error(RuntimeError)
155
+ lambda { @stub.save }.should raise_error(RuntimeError)
156
+ lambda { @stub.increment!(:age) }.should raise_error(RuntimeError)
157
+ end
158
+ end
159
+
160
+ describe "an instance generated by a factory with a custom class name" do
161
+
162
+ before do
163
+ @instance = Factory.create(:admin)
164
+ end
165
+
166
+ it "should use the correct class name" do
167
+ @instance.should be_kind_of(User)
168
+ end
169
+
170
+ it "should use the correct factory definition" do
171
+ @instance.should be_admin
172
+ end
173
+
174
+ end
175
+
176
+ describe "an instance generated by a factory that inherits from another factory" do
177
+ before do
178
+ @instance = Factory.create(:guest)
179
+ end
180
+
181
+ it "should use the class name of the parent factory" do
182
+ @instance.should be_kind_of(User)
183
+ end
184
+
185
+ it "should have attributes of the parent" do
186
+ @instance.first_name.should == 'Jimi'
187
+ end
188
+
189
+ it "should have attributes defined in the factory itself" do
190
+ @instance.username.should == 'GuestUser'
191
+ end
192
+
193
+ it "should have attributes that have been overriden" do
194
+ @instance.last_name.should == 'Anonymous'
195
+ end
196
+ end
197
+
198
+ describe "an attribute generated by a sequence" do
199
+
200
+ before do
201
+ @email = Factory.attributes_for(:admin)[:email]
202
+ end
203
+
204
+ it "should match the correct format" do
205
+ @email.should =~ /^somebody\d+@example\.com$/
206
+ end
207
+
208
+ describe "after the attribute has already been generated once" do
209
+
210
+ before do
211
+ @another_email = Factory.attributes_for(:admin)[:email]
212
+ end
213
+
214
+ it "should match the correct format" do
215
+ @email.should =~ /^somebody\d+@example\.com$/
216
+ end
217
+
218
+ it "should not be the same as the first generated value" do
219
+ @another_email.should_not == @email
220
+ end
221
+
222
+ end
223
+
224
+ end
225
+
226
+ describe "an attribute generated by an in-line sequence" do
227
+
228
+ before do
229
+ @username = Factory.attributes_for(:admin)[:username]
230
+ end
231
+
232
+ it "should match the correct format" do
233
+ @username.should =~ /^username\d+$/
234
+ end
235
+
236
+ describe "after the attribute has already been generated once" do
237
+
238
+ before do
239
+ @another_username = Factory.attributes_for(:admin)[:username]
240
+ end
241
+
242
+ it "should match the correct format" do
243
+ @username.should =~ /^username\d+$/
244
+ end
245
+
246
+ it "should not be the same as the first generated value" do
247
+ @another_username.should_not == @username
248
+ end
249
+
250
+ end
251
+
252
+ end
253
+
254
+ describe "a factory with a default strategy specified" do
255
+ it "should generate instances according to the strategy" do
256
+ Factory(:post).should be_kind_of(Hash)
257
+ end
258
+ end
259
+
260
+ it "should raise Factory::SequenceAbuseError" do
261
+ lambda {
262
+ Factory(:sequence_abuser)
263
+ }.should raise_error(Factory::SequenceAbuseError)
264
+ end
265
+ end
File without changes
@@ -2,10 +2,17 @@ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  $: << File.join(File.dirname(__FILE__))
3
3
 
4
4
  require 'rubygems'
5
- require 'test/unit'
5
+
6
6
  require 'activerecord'
7
- require 'factory_girl'
8
- gem 'thoughtbot-shoulda', ">= 2.0.0"
9
- require 'shoulda'
10
- require 'mocha'
7
+
8
+ require 'spec'
9
+ require 'spec/autorun'
10
+ require 'rr'
11
+
11
12
  require 'models'
13
+
14
+ require 'factory_girl'
15
+
16
+ Spec::Runner.configure do |config|
17
+ config.mock_with RR::Adapters::Rspec
18
+ end
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.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Ferris
@@ -9,11 +9,14 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-09 00:00:00 -04:00
12
+ date: 2009-07-15 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: factory_girl provides a framework and DSL for defining and using factories - less error-prone, more explicit, and all-around easier to work with than fixtures.
16
+ description: |-
17
+ factory_girl provides a framework and DSL for defining and
18
+ using factories - less error-prone, more explicit, and
19
+ all-around easier to work with than fixtures.
17
20
  email: jferris@thoughtbot.com
18
21
  executables: []
19
22
 
@@ -45,33 +48,32 @@ files:
45
48
  - lib/factory_girl/syntax/sham.rb
46
49
  - lib/factory_girl/syntax.rb
47
50
  - lib/factory_girl.rb
48
- - test/aliases_test.rb
49
- - test/association_attribute_test.rb
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
55
- - test/factory_test.rb
56
- - test/integration_test.rb
57
- - test/models.rb
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
66
- - test/test_helper.rb
51
+ - spec/factory_girl/aliases_spec.rb
52
+ - spec/factory_girl/attribute/association_spec.rb
53
+ - spec/factory_girl/attribute/dynamic_spec.rb
54
+ - spec/factory_girl/attribute/static_spec.rb
55
+ - spec/factory_girl/attribute_spec.rb
56
+ - spec/factory_girl/factory_spec.rb
57
+ - spec/factory_girl/proxy/attributes_for_spec.rb
58
+ - spec/factory_girl/proxy/build_spec.rb
59
+ - spec/factory_girl/proxy/create_spec.rb
60
+ - spec/factory_girl/proxy/stub_spec.rb
61
+ - spec/factory_girl/proxy_spec.rb
62
+ - spec/factory_girl/sequence_spec.rb
63
+ - spec/factory_girl/syntax/blueprint_spec.rb
64
+ - spec/factory_girl/syntax/generate_spec.rb
65
+ - spec/factory_girl/syntax/make_spec.rb
66
+ - spec/factory_girl/syntax/sham_spec.rb
67
+ - spec/integration_spec.rb
68
+ - spec/models.rb
69
+ - spec/spec_helper.rb
67
70
  has_rdoc: true
68
- homepage:
71
+ homepage: http://thoughtbot.com/projects/factory_girl
69
72
  licenses: []
70
73
 
71
74
  post_install_message:
72
75
  rdoc_options:
73
76
  - --line-numbers
74
- - --inline-source
75
77
  - --main
76
78
  - README.rdoc
77
79
  require_paths:
@@ -93,23 +95,23 @@ requirements: []
93
95
  rubyforge_project:
94
96
  rubygems_version: 1.3.5
95
97
  signing_key:
96
- specification_version: 2
98
+ specification_version: 3
97
99
  summary: factory_girl provides a framework and DSL for defining and using model instance factories.
98
100
  test_files:
99
- - test/aliases_test.rb
100
- - test/association_attribute_test.rb
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
106
- - test/factory_test.rb
107
- - test/integration_test.rb
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
101
+ - spec/factory_girl/aliases_spec.rb
102
+ - spec/factory_girl/attribute/association_spec.rb
103
+ - spec/factory_girl/attribute/dynamic_spec.rb
104
+ - spec/factory_girl/attribute/static_spec.rb
105
+ - spec/factory_girl/attribute_spec.rb
106
+ - spec/factory_girl/factory_spec.rb
107
+ - spec/factory_girl/proxy/attributes_for_spec.rb
108
+ - spec/factory_girl/proxy/build_spec.rb
109
+ - spec/factory_girl/proxy/create_spec.rb
110
+ - spec/factory_girl/proxy/stub_spec.rb
111
+ - spec/factory_girl/proxy_spec.rb
112
+ - spec/factory_girl/sequence_spec.rb
113
+ - spec/factory_girl/syntax/blueprint_spec.rb
114
+ - spec/factory_girl/syntax/generate_spec.rb
115
+ - spec/factory_girl/syntax/make_spec.rb
116
+ - spec/factory_girl/syntax/sham_spec.rb
117
+ - spec/integration_spec.rb