lacomartincik-factory_girl 1.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +228 -0
  5. data/Rakefile +74 -0
  6. data/lib/factory_girl.rb +23 -0
  7. data/lib/factory_girl/aliases.rb +50 -0
  8. data/lib/factory_girl/attribute.rb +29 -0
  9. data/lib/factory_girl/attribute/association.rb +18 -0
  10. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  11. data/lib/factory_girl/attribute/static.rb +17 -0
  12. data/lib/factory_girl/factory.rb +360 -0
  13. data/lib/factory_girl/proxy.rb +62 -0
  14. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  15. data/lib/factory_girl/proxy/build.rb +29 -0
  16. data/lib/factory_girl/proxy/create.rb +10 -0
  17. data/lib/factory_girl/proxy/stub.rb +49 -0
  18. data/lib/factory_girl/sequence.rb +63 -0
  19. data/lib/factory_girl/syntax.rb +12 -0
  20. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  21. data/lib/factory_girl/syntax/generate.rb +68 -0
  22. data/lib/factory_girl/syntax/make.rb +39 -0
  23. data/lib/factory_girl/syntax/sham.rb +42 -0
  24. data/spec/factory_girl/aliases_spec.rb +29 -0
  25. data/spec/factory_girl/attribute/association_spec.rb +25 -0
  26. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  27. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  28. data/spec/factory_girl/attribute_spec.rb +30 -0
  29. data/spec/factory_girl/factory_spec.rb +490 -0
  30. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  31. data/spec/factory_girl/proxy/build_spec.rb +73 -0
  32. data/spec/factory_girl/proxy/create_spec.rb +83 -0
  33. data/spec/factory_girl/proxy/stub_spec.rb +69 -0
  34. data/spec/factory_girl/proxy_spec.rb +28 -0
  35. data/spec/factory_girl/sequence_spec.rb +66 -0
  36. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  37. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  38. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  39. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  40. data/spec/integration_spec.rb +265 -0
  41. data/spec/models.rb +43 -0
  42. data/spec/spec_helper.rb +18 -0
  43. metadata +113 -0
@@ -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
@@ -0,0 +1,43 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => 'sqlite3',
3
+ :database => File.join(File.dirname(__FILE__), 'test.db')
4
+ )
5
+
6
+ class CreateSchema < ActiveRecord::Migration
7
+ def self.up
8
+ create_table :users, :force => true do |t|
9
+ t.string :first_name
10
+ t.string :last_name
11
+ t.string :email
12
+ t.string :username
13
+ t.boolean :admin, :default => false
14
+ end
15
+
16
+ create_table :posts, :force => true do |t|
17
+ t.string :name
18
+ t.integer :author_id
19
+ end
20
+
21
+ create_table :business, :force => true do |t|
22
+ t.string :name
23
+ t.integer :owner_id
24
+ end
25
+ end
26
+ end
27
+
28
+ CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
29
+
30
+ class User < ActiveRecord::Base
31
+ validates_presence_of :first_name, :last_name, :email
32
+ has_many :posts, :foreign_key => 'author_id'
33
+ end
34
+
35
+ class Business < ActiveRecord::Base
36
+ validates_presence_of :name, :owner_id
37
+ belongs_to :owner, :class_name => 'User'
38
+ end
39
+
40
+ class Post < ActiveRecord::Base
41
+ validates_presence_of :name, :author_id
42
+ belongs_to :author, :class_name => 'User'
43
+ end
@@ -0,0 +1,18 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+
6
+ require 'activerecord'
7
+
8
+ require 'spec'
9
+ require 'spec/autorun'
10
+ require 'rr'
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 ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lacomartincik-factory_girl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Joe Ferris
8
+ - Ladislav Martincik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-06-11 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ 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.
18
+ email: jferris@thoughtbot.com ladislav.martincik@xing.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - Changelog
27
+ - CONTRIBUTION_GUIDELINES.rdoc
28
+ - LICENSE
29
+ - Rakefile
30
+ - README.rdoc
31
+ - lib/factory_girl/aliases.rb
32
+ - lib/factory_girl/attribute/association.rb
33
+ - lib/factory_girl/attribute/dynamic.rb
34
+ - lib/factory_girl/attribute/static.rb
35
+ - lib/factory_girl/attribute.rb
36
+ - lib/factory_girl/factory.rb
37
+ - lib/factory_girl/proxy/attributes_for.rb
38
+ - lib/factory_girl/proxy/build.rb
39
+ - lib/factory_girl/proxy/create.rb
40
+ - lib/factory_girl/proxy/stub.rb
41
+ - lib/factory_girl/proxy.rb
42
+ - lib/factory_girl/sequence.rb
43
+ - lib/factory_girl/syntax/blueprint.rb
44
+ - lib/factory_girl/syntax/generate.rb
45
+ - lib/factory_girl/syntax/make.rb
46
+ - lib/factory_girl/syntax/sham.rb
47
+ - lib/factory_girl/syntax.rb
48
+ - lib/factory_girl.rb
49
+ - spec/factory_girl/aliases_spec.rb
50
+ - spec/factory_girl/attribute/association_spec.rb
51
+ - spec/factory_girl/attribute/dynamic_spec.rb
52
+ - spec/factory_girl/attribute/static_spec.rb
53
+ - spec/factory_girl/attribute_spec.rb
54
+ - spec/factory_girl/factory_spec.rb
55
+ - spec/factory_girl/proxy/attributes_for_spec.rb
56
+ - spec/factory_girl/proxy/build_spec.rb
57
+ - spec/factory_girl/proxy/create_spec.rb
58
+ - spec/factory_girl/proxy/stub_spec.rb
59
+ - spec/factory_girl/proxy_spec.rb
60
+ - spec/factory_girl/sequence_spec.rb
61
+ - spec/factory_girl/syntax/blueprint_spec.rb
62
+ - spec/factory_girl/syntax/generate_spec.rb
63
+ - spec/factory_girl/syntax/make_spec.rb
64
+ - spec/factory_girl/syntax/sham_spec.rb
65
+ - spec/integration_spec.rb
66
+ - spec/models.rb
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage:
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --line-numbers
73
+ - --main
74
+ - README.rdoc
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.2.0
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: factory_girl provides a framework and DSL for defining and using model instance factories.
96
+ test_files:
97
+ - spec/factory_girl/aliases_spec.rb
98
+ - spec/factory_girl/attribute/association_spec.rb
99
+ - spec/factory_girl/attribute/dynamic_spec.rb
100
+ - spec/factory_girl/attribute/static_spec.rb
101
+ - spec/factory_girl/attribute_spec.rb
102
+ - spec/factory_girl/factory_spec.rb
103
+ - spec/factory_girl/proxy/attributes_for_spec.rb
104
+ - spec/factory_girl/proxy/build_spec.rb
105
+ - spec/factory_girl/proxy/create_spec.rb
106
+ - spec/factory_girl/proxy/stub_spec.rb
107
+ - spec/factory_girl/proxy_spec.rb
108
+ - spec/factory_girl/sequence_spec.rb
109
+ - spec/factory_girl/syntax/blueprint_spec.rb
110
+ - spec/factory_girl/syntax/generate_spec.rb
111
+ - spec/factory_girl/syntax/make_spec.rb
112
+ - spec/factory_girl/syntax/sham_spec.rb
113
+ - spec/integration_spec.rb