factory_girl 1.3.3 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +68 -60
- data/features/support/test.db +0 -0
- data/lib/factory_girl.rb +6 -12
- data/lib/factory_girl/aliases.rb +2 -31
- data/lib/factory_girl/attribute.rb +1 -1
- data/lib/factory_girl/attribute/association.rb +1 -1
- data/lib/factory_girl/attribute/callback.rb +1 -1
- data/lib/factory_girl/attribute/dynamic.rb +3 -3
- data/lib/factory_girl/attribute/static.rb +1 -1
- data/lib/factory_girl/definition_proxy.rb +180 -0
- data/lib/factory_girl/deprecated.rb +18 -0
- data/lib/factory_girl/factory.rb +120 -355
- data/lib/factory_girl/find_definitions.rb +25 -0
- data/lib/factory_girl/proxy.rb +4 -6
- data/lib/factory_girl/proxy/attributes_for.rb +1 -1
- data/lib/factory_girl/proxy/build.rb +7 -5
- data/lib/factory_girl/proxy/create.rb +1 -1
- data/lib/factory_girl/proxy/stub.rb +11 -5
- data/lib/factory_girl/rails2.rb +1 -1
- data/lib/factory_girl/sequence.rb +5 -40
- data/lib/factory_girl/step_definitions.rb +7 -7
- data/lib/factory_girl/syntax.rb +7 -7
- data/lib/factory_girl/syntax/blueprint.rb +5 -4
- data/lib/factory_girl/syntax/default.rb +31 -0
- data/lib/factory_girl/syntax/generate.rb +13 -8
- data/lib/factory_girl/syntax/make.rb +8 -6
- data/lib/factory_girl/syntax/sham.rb +11 -8
- data/lib/factory_girl/syntax/vintage.rb +196 -0
- data/lib/factory_girl/version.rb +4 -0
- data/spec/acceptance/acceptance_spec.rb +43 -60
- data/spec/acceptance/syntax/blueprint_spec.rb +1 -5
- data/spec/acceptance/syntax/generate_spec.rb +1 -4
- data/spec/acceptance/syntax/make_spec.rb +1 -4
- data/spec/acceptance/syntax/sham_spec.rb +9 -7
- data/spec/acceptance/syntax/vintage_spec.rb +184 -0
- data/spec/factory_girl/aliases_spec.rb +5 -5
- data/spec/factory_girl/attribute/association_spec.rb +3 -3
- data/spec/factory_girl/attribute/callback_spec.rb +3 -3
- data/spec/factory_girl/attribute/dynamic_spec.rb +20 -9
- data/spec/factory_girl/attribute/static_spec.rb +5 -5
- data/spec/factory_girl/attribute_spec.rb +5 -5
- data/spec/factory_girl/definition_proxy_spec.rb +138 -0
- data/spec/factory_girl/deprecated_spec.rb +66 -0
- data/spec/factory_girl/factory_spec.rb +283 -566
- data/spec/factory_girl/find_definitions_spec.rb +89 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +2 -2
- data/spec/factory_girl/proxy/build_spec.rb +17 -12
- data/spec/factory_girl/proxy/create_spec.rb +17 -12
- data/spec/factory_girl/proxy/stub_spec.rb +6 -5
- data/spec/factory_girl/proxy_spec.rb +2 -2
- data/spec/factory_girl/sequence_spec.rb +15 -38
- data/spec/spec_helper.rb +4 -0
- metadata +28 -11
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
share_examples_for "finds definitions" do
|
4
|
+
before do
|
5
|
+
stub(FactoryGirl).require
|
6
|
+
FactoryGirl.find_definitions
|
7
|
+
end
|
8
|
+
subject { FactoryGirl }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "definition loading" do
|
12
|
+
def self.in_directory_with_files(*files)
|
13
|
+
before do
|
14
|
+
@pwd = Dir.pwd
|
15
|
+
@tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
|
16
|
+
FileUtils.mkdir_p @tmp_dir
|
17
|
+
Dir.chdir(@tmp_dir)
|
18
|
+
|
19
|
+
files.each do |file|
|
20
|
+
FileUtils.mkdir_p File.dirname(file)
|
21
|
+
FileUtils.touch file
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
after do
|
26
|
+
Dir.chdir(@pwd)
|
27
|
+
FileUtils.rm_rf(@tmp_dir)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def require_definitions_from(file)
|
32
|
+
simple_matcher do |given, matcher|
|
33
|
+
has_received = have_received.method_missing(:require, file)
|
34
|
+
result = has_received.matches?(given)
|
35
|
+
matcher.description = "require definitions from #{file}"
|
36
|
+
matcher.failure_message = has_received.failure_message
|
37
|
+
result
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "with factories.rb" do
|
42
|
+
in_directory_with_files 'factories.rb'
|
43
|
+
it_should_behave_like "finds definitions"
|
44
|
+
it { should require_definitions_from('factories.rb') }
|
45
|
+
end
|
46
|
+
|
47
|
+
%w(spec test).each do |dir|
|
48
|
+
describe "with a factories file under #{dir}" do
|
49
|
+
in_directory_with_files File.join(dir, 'factories.rb')
|
50
|
+
it_should_behave_like "finds definitions"
|
51
|
+
it { should require_definitions_from("#{dir}/factories.rb") }
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "with a factories file under #{dir}/factories" do
|
55
|
+
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb')
|
56
|
+
it_should_behave_like "finds definitions"
|
57
|
+
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "with several factories files under #{dir}/factories" do
|
61
|
+
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'),
|
62
|
+
File.join(dir, 'factories', 'person_factory.rb')
|
63
|
+
it_should_behave_like "finds definitions"
|
64
|
+
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
65
|
+
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "with several factories files under #{dir}/factories in non-alphabetical order" do
|
69
|
+
in_directory_with_files File.join(dir, 'factories', 'b.rb'),
|
70
|
+
File.join(dir, 'factories', 'a.rb')
|
71
|
+
it "should load the files in the right order" do
|
72
|
+
@loaded = []
|
73
|
+
stub(FactoryGirl).require { |a| @loaded << File.split(a)[-1] }
|
74
|
+
FactoryGirl.find_definitions
|
75
|
+
@loaded.should == ["a.rb", "b.rb"]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "with nested and unnested factories files under #{dir}" do
|
80
|
+
in_directory_with_files File.join(dir, 'factories.rb'),
|
81
|
+
File.join(dir, 'factories', 'post_factory.rb'),
|
82
|
+
File.join(dir, 'factories', 'person_factory.rb')
|
83
|
+
it_should_behave_like "finds definitions"
|
84
|
+
it { should require_definitions_from("#{dir}/factories.rb") }
|
85
|
+
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
86
|
+
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Proxy::AttributesFor do
|
4
4
|
before do
|
5
|
-
@proxy =
|
5
|
+
@proxy = FactoryGirl::Proxy::AttributesFor.new(@class)
|
6
6
|
end
|
7
7
|
|
8
8
|
describe "when asked to associate with another factory" do
|
@@ -1,18 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Proxy::Build do
|
4
4
|
before do
|
5
5
|
@class = Class.new
|
6
6
|
@instance = "built-instance"
|
7
|
-
@association = "associated-instance"
|
8
7
|
|
9
8
|
stub(@class).new { @instance }
|
10
9
|
stub(@instance).attribute { 'value' }
|
11
|
-
stub(Factory).create { @association }
|
12
10
|
stub(@instance, :attribute=)
|
13
11
|
stub(@instance, :owner=)
|
14
12
|
|
15
|
-
@proxy =
|
13
|
+
@proxy = FactoryGirl::Proxy::Build.new(@class)
|
16
14
|
end
|
17
15
|
|
18
16
|
it "should instantiate the class" do
|
@@ -21,11 +19,16 @@ describe Factory::Proxy::Build do
|
|
21
19
|
|
22
20
|
describe "when asked to associate with another factory" do
|
23
21
|
before do
|
24
|
-
@
|
22
|
+
@association = "associated-instance"
|
23
|
+
@associated_factory = "associated-factory"
|
24
|
+
stub(FactoryGirl).factory_by_name { @associated_factory }
|
25
|
+
stub(@associated_factory).run { @association }
|
26
|
+
@overrides = { 'attr' => 'value' }
|
27
|
+
@proxy.associate(:owner, :user, @overrides)
|
25
28
|
end
|
26
29
|
|
27
30
|
it "should create the associated instance" do
|
28
|
-
|
31
|
+
@associated_factory.should have_received.run(FactoryGirl::Proxy::Create, @overrides)
|
29
32
|
end
|
30
33
|
|
31
34
|
it "should set the associated instance" do
|
@@ -33,12 +36,14 @@ describe Factory::Proxy::Build do
|
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
36
|
-
it "should
|
37
|
-
association =
|
38
|
-
|
39
|
-
stub(
|
40
|
-
|
41
|
-
|
39
|
+
it "should run create when building an association" do
|
40
|
+
association = "associated-instance"
|
41
|
+
associated_factory = "associated-factory"
|
42
|
+
stub(FactoryGirl).factory_by_name { associated_factory }
|
43
|
+
stub(associated_factory).run { association }
|
44
|
+
overrides = { 'attr' => 'value' }
|
45
|
+
@proxy.association(:user, overrides).should == association
|
46
|
+
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
42
47
|
end
|
43
48
|
|
44
49
|
it "should return the built instance when asked for the result" do
|
@@ -1,19 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Proxy::Create do
|
4
4
|
before do
|
5
5
|
@class = Class.new
|
6
6
|
@instance = "built-instance"
|
7
|
-
@association = "associated-instance"
|
8
7
|
|
9
8
|
stub(@class).new { @instance }
|
10
|
-
stub(Factory).create { @association }
|
11
9
|
stub(@instance).attribute { 'value' }
|
12
10
|
stub(@instance, :attribute=)
|
13
11
|
stub(@instance, :owner=)
|
14
12
|
stub(@instance).save!
|
15
13
|
|
16
|
-
@proxy =
|
14
|
+
@proxy = FactoryGirl::Proxy::Create.new(@class)
|
17
15
|
end
|
18
16
|
|
19
17
|
it "should instantiate the class" do
|
@@ -22,11 +20,16 @@ describe Factory::Proxy::Create do
|
|
22
20
|
|
23
21
|
describe "when asked to associate with another factory" do
|
24
22
|
before do
|
25
|
-
@
|
23
|
+
@association = "associated-instance"
|
24
|
+
@associated_factory = "associated-factory"
|
25
|
+
stub(FactoryGirl).factory_by_name { @associated_factory }
|
26
|
+
stub(@associated_factory).run { @association }
|
27
|
+
@overrides = { 'attr' => 'value' }
|
28
|
+
@proxy.associate(:owner, :user, @overrides)
|
26
29
|
end
|
27
30
|
|
28
31
|
it "should create the associated instance" do
|
29
|
-
|
32
|
+
@associated_factory.should have_received.run(FactoryGirl::Proxy::Create, @overrides)
|
30
33
|
end
|
31
34
|
|
32
35
|
it "should set the associated instance" do
|
@@ -34,12 +37,14 @@ describe Factory::Proxy::Create do
|
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
37
|
-
it "should
|
38
|
-
association =
|
39
|
-
|
40
|
-
stub(
|
41
|
-
|
42
|
-
|
40
|
+
it "should run create when building an association" do
|
41
|
+
association = "associated-instance"
|
42
|
+
associated_factory = "associated-factory"
|
43
|
+
stub(FactoryGirl).factory_by_name { associated_factory }
|
44
|
+
stub(associated_factory).run { association }
|
45
|
+
overrides = { 'attr' => 'value' }
|
46
|
+
@proxy.association(:user, overrides).should == association
|
47
|
+
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
43
48
|
end
|
44
49
|
|
45
50
|
describe "when asked for the result" do
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Proxy::Stub do
|
4
4
|
before do
|
5
5
|
@class = "class"
|
6
6
|
@instance = "instance"
|
@@ -9,7 +9,7 @@ describe Factory::Proxy::Stub do
|
|
9
9
|
stub(@instance).id { 42 }
|
10
10
|
stub(@instance).reload { @instance.connection.reload }
|
11
11
|
|
12
|
-
@stub =
|
12
|
+
@stub = FactoryGirl::Proxy::Stub.new(@class)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should not be a new record" do
|
@@ -23,13 +23,14 @@ describe Factory::Proxy::Stub do
|
|
23
23
|
describe "when a user factory exists" do
|
24
24
|
before do
|
25
25
|
@user = "user"
|
26
|
-
stub(
|
26
|
+
stub(FactoryGirl).factory_by_name { @associated_factory }
|
27
|
+
@associated_factory = 'associate-factory'
|
27
28
|
end
|
28
29
|
|
29
30
|
describe "when asked to associate with another factory" do
|
30
31
|
before do
|
31
32
|
stub(@instance).owner { @user }
|
32
|
-
mock(
|
33
|
+
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
33
34
|
mock(@stub).set(:owner, @user)
|
34
35
|
|
35
36
|
@stub.associate(:owner, :user, {})
|
@@ -41,7 +42,7 @@ describe Factory::Proxy::Stub do
|
|
41
42
|
end
|
42
43
|
|
43
44
|
it "should return the association when building one" do
|
44
|
-
mock(
|
45
|
+
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
45
46
|
@stub.association(:user).should == @user
|
46
47
|
end
|
47
48
|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
4
|
-
describe "a sequence" do
|
3
|
+
describe FactoryGirl::Sequence do
|
4
|
+
describe "a basic sequence" do
|
5
5
|
before do
|
6
|
-
@sequence =
|
6
|
+
@sequence = FactoryGirl::Sequence.new {|n| "=#{n}" }
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should start with a value of 1" do
|
@@ -20,47 +20,24 @@ describe Factory::Sequence do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
24
|
-
describe "
|
23
|
+
|
24
|
+
describe "a custom sequence" do
|
25
25
|
before do
|
26
|
-
@sequence = "
|
27
|
-
@name = :count
|
28
|
-
stub(Factory::Sequence).new { @sequence }
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should create a new sequence" do
|
32
|
-
mock(Factory::Sequence).new() { @sequence }
|
33
|
-
Factory.sequence(@name)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should use the supplied block as the sequence generator" do
|
37
|
-
stub(Factory::Sequence).new.yields(1)
|
38
|
-
yielded = false
|
39
|
-
Factory.sequence(@name) {|n| yielded = true }
|
40
|
-
(yielded).should be
|
26
|
+
@sequence = FactoryGirl::Sequence.new("A") {|n| "=#{n}" }
|
41
27
|
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe "after defining a sequence" do
|
45
|
-
before do
|
46
|
-
@sequence = "sequence"
|
47
|
-
@name = :test
|
48
|
-
@value = '1 2 5'
|
49
|
-
|
50
|
-
stub(@sequence).next { @value }
|
51
|
-
stub(Factory::Sequence).new { @sequence }
|
52
28
|
|
53
|
-
|
29
|
+
it "should start with a value of A" do
|
30
|
+
@sequence.next.should == "=A"
|
54
31
|
end
|
55
32
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
33
|
+
describe "after being called" do
|
34
|
+
before do
|
35
|
+
@sequence.next
|
36
|
+
end
|
61
37
|
|
62
|
-
|
63
|
-
|
38
|
+
it "should use the next value" do
|
39
|
+
@sequence.next.should == "=B"
|
40
|
+
end
|
64
41
|
end
|
65
42
|
end
|
66
43
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_girl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 299253613
|
5
|
+
prerelease: true
|
6
6
|
segments:
|
7
|
-
-
|
8
|
-
-
|
9
|
-
-
|
10
|
-
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
- beta1
|
11
|
+
version: 2.0.0.beta1
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- Joe Ferris
|
@@ -15,7 +16,7 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date:
|
19
|
+
date: 2010-10-02 00:00:00 -04:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
@@ -144,7 +145,10 @@ files:
|
|
144
145
|
- lib/factory_girl/attribute/dynamic.rb
|
145
146
|
- lib/factory_girl/attribute/static.rb
|
146
147
|
- lib/factory_girl/attribute.rb
|
148
|
+
- lib/factory_girl/definition_proxy.rb
|
149
|
+
- lib/factory_girl/deprecated.rb
|
147
150
|
- lib/factory_girl/factory.rb
|
151
|
+
- lib/factory_girl/find_definitions.rb
|
148
152
|
- lib/factory_girl/proxy/attributes_for.rb
|
149
153
|
- lib/factory_girl/proxy/build.rb
|
150
154
|
- lib/factory_girl/proxy/create.rb
|
@@ -154,10 +158,13 @@ files:
|
|
154
158
|
- lib/factory_girl/sequence.rb
|
155
159
|
- lib/factory_girl/step_definitions.rb
|
156
160
|
- lib/factory_girl/syntax/blueprint.rb
|
161
|
+
- lib/factory_girl/syntax/default.rb
|
157
162
|
- lib/factory_girl/syntax/generate.rb
|
158
163
|
- lib/factory_girl/syntax/make.rb
|
159
164
|
- lib/factory_girl/syntax/sham.rb
|
165
|
+
- lib/factory_girl/syntax/vintage.rb
|
160
166
|
- lib/factory_girl/syntax.rb
|
167
|
+
- lib/factory_girl/version.rb
|
161
168
|
- lib/factory_girl.rb
|
162
169
|
- spec/acceptance/acceptance_helper.rb
|
163
170
|
- spec/acceptance/acceptance_spec.rb
|
@@ -166,13 +173,17 @@ files:
|
|
166
173
|
- spec/acceptance/syntax/generate_spec.rb
|
167
174
|
- spec/acceptance/syntax/make_spec.rb
|
168
175
|
- spec/acceptance/syntax/sham_spec.rb
|
176
|
+
- spec/acceptance/syntax/vintage_spec.rb
|
169
177
|
- spec/factory_girl/aliases_spec.rb
|
170
178
|
- spec/factory_girl/attribute/association_spec.rb
|
171
179
|
- spec/factory_girl/attribute/callback_spec.rb
|
172
180
|
- spec/factory_girl/attribute/dynamic_spec.rb
|
173
181
|
- spec/factory_girl/attribute/static_spec.rb
|
174
182
|
- spec/factory_girl/attribute_spec.rb
|
183
|
+
- spec/factory_girl/definition_proxy_spec.rb
|
184
|
+
- spec/factory_girl/deprecated_spec.rb
|
175
185
|
- spec/factory_girl/factory_spec.rb
|
186
|
+
- spec/factory_girl/find_definitions_spec.rb
|
176
187
|
- spec/factory_girl/proxy/attributes_for_spec.rb
|
177
188
|
- spec/factory_girl/proxy/build_spec.rb
|
178
189
|
- spec/factory_girl/proxy/create_spec.rb
|
@@ -208,12 +219,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
208
219
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
209
220
|
none: false
|
210
221
|
requirements:
|
211
|
-
- - "
|
222
|
+
- - ">"
|
212
223
|
- !ruby/object:Gem::Version
|
213
|
-
hash:
|
224
|
+
hash: 25
|
214
225
|
segments:
|
215
|
-
-
|
216
|
-
|
226
|
+
- 1
|
227
|
+
- 3
|
228
|
+
- 1
|
229
|
+
version: 1.3.1
|
217
230
|
requirements: []
|
218
231
|
|
219
232
|
rubyforge_project:
|
@@ -227,13 +240,17 @@ test_files:
|
|
227
240
|
- spec/acceptance/syntax/generate_spec.rb
|
228
241
|
- spec/acceptance/syntax/make_spec.rb
|
229
242
|
- spec/acceptance/syntax/sham_spec.rb
|
243
|
+
- spec/acceptance/syntax/vintage_spec.rb
|
230
244
|
- spec/factory_girl/aliases_spec.rb
|
231
245
|
- spec/factory_girl/attribute/association_spec.rb
|
232
246
|
- spec/factory_girl/attribute/callback_spec.rb
|
233
247
|
- spec/factory_girl/attribute/dynamic_spec.rb
|
234
248
|
- spec/factory_girl/attribute/static_spec.rb
|
235
249
|
- spec/factory_girl/attribute_spec.rb
|
250
|
+
- spec/factory_girl/definition_proxy_spec.rb
|
251
|
+
- spec/factory_girl/deprecated_spec.rb
|
236
252
|
- spec/factory_girl/factory_spec.rb
|
253
|
+
- spec/factory_girl/find_definitions_spec.rb
|
237
254
|
- spec/factory_girl/proxy/attributes_for_spec.rb
|
238
255
|
- spec/factory_girl/proxy/build_spec.rb
|
239
256
|
- spec/factory_girl/proxy/create_spec.rb
|