factory_girl 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +9 -1
- data/lib/factory_girl.rb +12 -1
- data/lib/factory_girl/attribute_proxy.rb +1 -1
- data/lib/factory_girl/factory.rb +7 -5
- data/test/attribute_proxy_test.rb +20 -0
- data/test/factory_test.rb +72 -12
- data/test/integration_test.rb +2 -2
- data/test/models.rb +10 -0
- metadata +2 -2
data/Rakefile
CHANGED
@@ -3,6 +3,7 @@ require 'rake'
|
|
3
3
|
require 'rake/testtask'
|
4
4
|
require 'rake/rdoctask'
|
5
5
|
require 'rake/gempackagetask'
|
6
|
+
require 'rcov/rcovtask'
|
6
7
|
require 'date'
|
7
8
|
|
8
9
|
desc 'Default: run unit tests.'
|
@@ -15,6 +16,13 @@ Rake::TestTask.new(:test) do |t|
|
|
15
16
|
t.verbose = true
|
16
17
|
end
|
17
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
|
+
|
18
26
|
desc 'Generate documentation for the factory_girl plugin.'
|
19
27
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
20
28
|
rdoc.rdoc_dir = 'rdoc'
|
@@ -31,7 +39,7 @@ end
|
|
31
39
|
|
32
40
|
spec = Gem::Specification.new do |s|
|
33
41
|
s.name = %q{factory_girl}
|
34
|
-
s.version = "1.1.
|
42
|
+
s.version = "1.1.4"
|
35
43
|
s.summary = %q{factory_girl provides a framework and DSL for defining and
|
36
44
|
using model instance factories.}
|
37
45
|
s.description = %q{factory_girl provides a framework and DSL for defining and
|
data/lib/factory_girl.rb
CHANGED
@@ -13,4 +13,15 @@ def Factory (name, attrs = {})
|
|
13
13
|
Factory.create(name, attrs)
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
if defined? Rails
|
17
|
+
Rails.configuration.after_initialize do
|
18
|
+
Factory.definition_file_paths = [
|
19
|
+
File.join(RAILS_ROOT, 'test', 'factories'),
|
20
|
+
File.join(RAILS_ROOT, 'spec', 'factories')
|
21
|
+
]
|
22
|
+
Factory.find_definitions
|
23
|
+
end
|
24
|
+
else
|
25
|
+
Factory.find_definitions
|
26
|
+
end
|
27
|
+
|
data/lib/factory_girl/factory.rb
CHANGED
@@ -182,10 +182,12 @@ class Factory
|
|
182
182
|
|
183
183
|
def find_definitions #:nodoc:
|
184
184
|
definition_file_paths.each do |path|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
185
|
+
require("#{path}.rb") if File.exists?("#{path}.rb")
|
186
|
+
|
187
|
+
if File.directory? path
|
188
|
+
Dir[File.join(path, '*.rb')].each do |file|
|
189
|
+
require file
|
190
|
+
end
|
189
191
|
end
|
190
192
|
end
|
191
193
|
end
|
@@ -223,7 +225,7 @@ class Factory
|
|
223
225
|
|
224
226
|
def class_for (class_or_to_s)
|
225
227
|
if class_or_to_s.respond_to?(:to_sym)
|
226
|
-
class_or_to_s.to_s.classify.constantize
|
228
|
+
class_or_to_s.to_s.pluralize.classify.constantize
|
227
229
|
else
|
228
230
|
class_or_to_s
|
229
231
|
end
|
@@ -68,6 +68,26 @@ class AttributeProxyTest < Test::Unit::TestCase
|
|
68
68
|
|
69
69
|
end
|
70
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
|
+
|
71
91
|
context "fetching the value of an attribute" do
|
72
92
|
|
73
93
|
setup do
|
data/test/factory_test.rb
CHANGED
@@ -159,6 +159,7 @@ class FactoryTest < Test::Unit::TestCase
|
|
159
159
|
@name = :user
|
160
160
|
@factory.association(@name)
|
161
161
|
Post.any_instance.stubs(:user=)
|
162
|
+
Factory.stubs(:create)
|
162
163
|
end
|
163
164
|
|
164
165
|
should "add an attribute with the name of the association" do
|
@@ -166,7 +167,7 @@ class FactoryTest < Test::Unit::TestCase
|
|
166
167
|
end
|
167
168
|
|
168
169
|
should "create a block that builds the association" do
|
169
|
-
Factory.expects(:
|
170
|
+
Factory.expects(:create).with(@name, {})
|
170
171
|
@factory.build
|
171
172
|
end
|
172
173
|
|
@@ -179,6 +180,7 @@ class FactoryTest < Test::Unit::TestCase
|
|
179
180
|
@name = :author
|
180
181
|
@factory_name = :user
|
181
182
|
@factory.association(@name, :factory => @factory_name)
|
183
|
+
Factory.stubs(:create)
|
182
184
|
end
|
183
185
|
|
184
186
|
should "add an attribute with the name of the association" do
|
@@ -186,7 +188,7 @@ class FactoryTest < Test::Unit::TestCase
|
|
186
188
|
end
|
187
189
|
|
188
190
|
should "create a block that builds the association" do
|
189
|
-
Factory.expects(:
|
191
|
+
Factory.expects(:create).with(@factory_name, {})
|
190
192
|
@factory.build
|
191
193
|
end
|
192
194
|
|
@@ -344,6 +346,24 @@ class FactoryTest < Test::Unit::TestCase
|
|
344
346
|
end
|
345
347
|
|
346
348
|
end
|
349
|
+
|
350
|
+
context "a factory with a name ending in s" do
|
351
|
+
|
352
|
+
setup do
|
353
|
+
@name = :business
|
354
|
+
@class = Business
|
355
|
+
@factory = Factory.new(@name)
|
356
|
+
end
|
357
|
+
|
358
|
+
should "have a factory name" do
|
359
|
+
assert_equal @name, @factory.factory_name
|
360
|
+
end
|
361
|
+
|
362
|
+
should "have a build class" do
|
363
|
+
assert_equal @class, @factory.build_class
|
364
|
+
end
|
365
|
+
|
366
|
+
end
|
347
367
|
|
348
368
|
context "a factory with a string for a name" do
|
349
369
|
|
@@ -414,21 +434,61 @@ class FactoryTest < Test::Unit::TestCase
|
|
414
434
|
end
|
415
435
|
|
416
436
|
end
|
437
|
+
|
438
|
+
def self.context_in_directory_with_files(*files)
|
439
|
+
context "in a directory with #{files.to_sentence}" do
|
440
|
+
setup do
|
441
|
+
@pwd = Dir.pwd
|
442
|
+
@tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
|
443
|
+
FileUtils.mkdir_p @tmp_dir
|
444
|
+
Dir.chdir(@tmp_dir)
|
445
|
+
|
446
|
+
files.each do |file|
|
447
|
+
FileUtils.mkdir_p File.dirname(file)
|
448
|
+
FileUtils.touch file
|
449
|
+
Factory.stubs(:require).with(file)
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
teardown do
|
454
|
+
Dir.chdir(@pwd)
|
455
|
+
FileUtils.rm_rf(@tmp_dir)
|
456
|
+
end
|
417
457
|
|
418
|
-
|
419
|
-
|
420
|
-
|
458
|
+
yield
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
def self.should_require_definitions_from(file)
|
463
|
+
should "load definitions from #{file}" do
|
421
464
|
Factory.expects(:require).with(file)
|
422
465
|
Factory.find_definitions
|
423
|
-
end
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
context_in_directory_with_files 'factories.rb' do
|
470
|
+
should_require_definitions_from 'factories.rb'
|
424
471
|
end
|
472
|
+
|
473
|
+
%w(spec test).each do |dir|
|
474
|
+
context_in_directory_with_files File.join(dir, 'factories.rb') do
|
475
|
+
should_require_definitions_from "#{dir}/factories.rb"
|
476
|
+
end
|
477
|
+
|
478
|
+
context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb') do
|
479
|
+
should_require_definitions_from "#{dir}/factories/post_factory.rb"
|
480
|
+
end
|
481
|
+
|
482
|
+
context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do
|
483
|
+
should_require_definitions_from "#{dir}/factories/post_factory.rb"
|
484
|
+
should_require_definitions_from "#{dir}/factories/person_factory.rb"
|
485
|
+
end
|
425
486
|
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
Factory.find_definitions
|
487
|
+
context_in_directory_with_files File.join(dir, 'factories.rb'), File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do
|
488
|
+
should_require_definitions_from "#{dir}/factories.rb"
|
489
|
+
should_require_definitions_from "#{dir}/factories/post_factory.rb"
|
490
|
+
should_require_definitions_from "#{dir}/factories/person_factory.rb"
|
491
|
+
end
|
432
492
|
end
|
433
493
|
|
434
494
|
end
|
data/test/integration_test.rb
CHANGED
@@ -70,8 +70,8 @@ class IntegrationTest < Test::Unit::TestCase
|
|
70
70
|
assert_kind_of User, @instance.author
|
71
71
|
end
|
72
72
|
|
73
|
-
should "
|
74
|
-
assert
|
73
|
+
should "save associations" do
|
74
|
+
assert !@instance.author.new_record?
|
75
75
|
end
|
76
76
|
|
77
77
|
should "not assign both an association and its foreign key" do
|
data/test/models.rb
CHANGED
@@ -16,6 +16,11 @@ class CreateSchema < ActiveRecord::Migration
|
|
16
16
|
t.string :name
|
17
17
|
t.integer :author_id
|
18
18
|
end
|
19
|
+
|
20
|
+
create_table :business, :force => true do |t|
|
21
|
+
t.string :name
|
22
|
+
t.integer :owner_id
|
23
|
+
end
|
19
24
|
end
|
20
25
|
end
|
21
26
|
|
@@ -26,6 +31,11 @@ class User < ActiveRecord::Base
|
|
26
31
|
has_many :posts, :foreign_key => 'author_id'
|
27
32
|
end
|
28
33
|
|
34
|
+
class Business < ActiveRecord::Base
|
35
|
+
validates_presence_of :name, :owner_id
|
36
|
+
belongs_to :owner, :class_name => 'User'
|
37
|
+
end
|
38
|
+
|
29
39
|
class Post < ActiveRecord::Base
|
30
40
|
validates_presence_of :name, :author_id
|
31
41
|
belongs_to :author, :class_name => 'User'
|
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.
|
4
|
+
version: 1.1.4
|
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
|
+
date: 2008-11-28 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|