factory_girl 2.0.0.beta2 → 2.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/Appraisals +1 -0
  2. data/CONTRIBUTION_GUIDELINES.md +7 -7
  3. data/GETTING_STARTED.md +36 -11
  4. data/Gemfile +2 -2
  5. data/Gemfile.lock +28 -21
  6. data/README.md +2 -2
  7. data/Rakefile +2 -2
  8. data/features/factory_girl_steps.feature +33 -0
  9. data/features/support/factories.rb +25 -18
  10. data/features/support/test.db +0 -0
  11. data/lib/factory_girl.rb +27 -0
  12. data/lib/factory_girl/attribute.rb +4 -0
  13. data/lib/factory_girl/attribute/association.rb +4 -0
  14. data/lib/factory_girl/attribute/dynamic.rb +1 -1
  15. data/lib/factory_girl/attribute/implicit.rb +36 -0
  16. data/lib/factory_girl/attribute/sequence.rb +16 -0
  17. data/lib/factory_girl/definition_proxy.rb +2 -2
  18. data/lib/factory_girl/factory.rb +1 -13
  19. data/lib/factory_girl/find_definitions.rb +1 -3
  20. data/lib/factory_girl/proxy/build.rb +2 -2
  21. data/lib/factory_girl/proxy/stub.rb +6 -2
  22. data/lib/factory_girl/rails2.rb +7 -1
  23. data/lib/factory_girl/registry.rb +7 -21
  24. data/lib/factory_girl/sequence.rb +3 -14
  25. data/lib/factory_girl/step_definitions.rb +5 -5
  26. data/lib/factory_girl/syntax/blueprint.rb +1 -1
  27. data/lib/factory_girl/syntax/default.rb +3 -3
  28. data/lib/factory_girl/syntax/generate.rb +3 -3
  29. data/lib/factory_girl/syntax/make.rb +5 -1
  30. data/lib/factory_girl/syntax/methods.rb +51 -4
  31. data/lib/factory_girl/syntax/sham.rb +2 -2
  32. data/lib/factory_girl/syntax/vintage.rb +31 -12
  33. data/lib/factory_girl/version.rb +1 -1
  34. data/spec/acceptance/build_list_spec.rb +42 -0
  35. data/spec/acceptance/create_list_spec.rb +42 -0
  36. data/spec/acceptance/sequence_spec.rb +6 -4
  37. data/spec/acceptance/syntax/blueprint_spec.rb +3 -3
  38. data/spec/acceptance/syntax/make_spec.rb +19 -1
  39. data/spec/acceptance/syntax/vintage_spec.rb +4 -4
  40. data/spec/factory_girl/attribute/association_spec.rb +4 -0
  41. data/spec/factory_girl/attribute/implicit_spec.rb +50 -0
  42. data/spec/factory_girl/attribute/sequence_spec.rb +21 -0
  43. data/spec/factory_girl/attribute_spec.rb +4 -0
  44. data/spec/factory_girl/definition_proxy_spec.rb +2 -13
  45. data/spec/factory_girl/factory_spec.rb +4 -2
  46. data/spec/factory_girl/find_definitions_spec.rb +9 -0
  47. data/spec/factory_girl/proxy/build_spec.rb +2 -2
  48. data/spec/factory_girl/proxy/create_spec.rb +2 -2
  49. data/spec/factory_girl/proxy/stub_spec.rb +5 -1
  50. data/spec/factory_girl/registry_spec.rb +11 -20
  51. data/spec/factory_girl/sequence_spec.rb +15 -19
  52. data/spec/factory_girl_spec.rb +17 -0
  53. data/spec/spec_helper.rb +2 -1
  54. metadata +45 -73
@@ -1,4 +1,4 @@
1
1
  module FactoryGirl
2
- VERSION = "2.0.0.beta2"
2
+ VERSION = "2.0.0.beta3"
3
3
  end
4
4
 
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "build multiple instances" do
5
+ before do
6
+ define_model('Post', :title => :string)
7
+
8
+ FactoryGirl.define do
9
+ factory(:post) do |post|
10
+ post.title "Through the Looking Glass"
11
+ end
12
+ end
13
+ end
14
+
15
+ context "without default attributes" do
16
+ subject { FactoryGirl.build_list(:post, 20) }
17
+
18
+ its(:length) { should == 20 }
19
+
20
+ it "builds (but doesn't save) all the posts" do
21
+ subject.each do |record|
22
+ record.should be_new_record
23
+ end
24
+ end
25
+
26
+ it "uses the default factory values" do
27
+ subject.each do |record|
28
+ record.title.should == "Through the Looking Glass"
29
+ end
30
+ end
31
+ end
32
+
33
+ context "with default attributes" do
34
+ subject { FactoryGirl.build_list(:post, 20, :title => "The Hunting of the Snark") }
35
+
36
+ it "overrides the default values" do
37
+ subject.each do |record|
38
+ record.title.should == "The Hunting of the Snark"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "create multiple instances" do
5
+ before do
6
+ define_model('Post', :title => :string)
7
+
8
+ FactoryGirl.define do
9
+ factory(:post) do |post|
10
+ post.title "Through the Looking Glass"
11
+ end
12
+ end
13
+ end
14
+
15
+ context "without default attributes" do
16
+ subject { FactoryGirl.create_list(:post, 20) }
17
+
18
+ its(:length) { should == 20 }
19
+
20
+ it "creates all the posts" do
21
+ subject.each do |record|
22
+ record.should_not be_new_record
23
+ end
24
+ end
25
+
26
+ it "uses the default factory values" do
27
+ subject.each do |record|
28
+ record.title.should == "Through the Looking Glass"
29
+ end
30
+ end
31
+ end
32
+
33
+ context "with default attributes" do
34
+ subject { FactoryGirl.create_list(:post, 20, :title => "The Hunting of the Snark") }
35
+
36
+ it "overrides the default values" do
37
+ subject.each do |record|
38
+ record.title.should == "The Hunting of the Snark"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -2,6 +2,8 @@ require 'spec_helper'
2
2
  require 'acceptance/acceptance_helper'
3
3
 
4
4
  describe "sequences" do
5
+ include FactoryGirl::Syntax::Methods
6
+
5
7
  it "generates several values in the correct format" do
6
8
  FactoryGirl.define do
7
9
  sequence :email do |n|
@@ -9,8 +11,8 @@ describe "sequences" do
9
11
  end
10
12
  end
11
13
 
12
- first_value = FactoryGirl.create(:email)
13
- another_value = FactoryGirl.create(:email)
14
+ first_value = generate(:email)
15
+ another_value = generate(:email)
14
16
 
15
17
  first_value.should =~ /^somebody\d+@example\.com$/
16
18
  another_value.should =~ /^somebody\d+@example\.com$/
@@ -22,8 +24,8 @@ describe "sequences" do
22
24
  sequence :order
23
25
  end
24
26
 
25
- first_value = FactoryGirl.create(:order)
26
- another_value = FactoryGirl.create(:order)
27
+ first_value = generate(:order)
28
+ another_value = generate(:order)
27
29
 
28
30
  first_value.should == 1
29
31
  another_value.should == 2
@@ -9,9 +9,9 @@ describe "a blueprint" do
9
9
 
10
10
  Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
11
11
  User.blueprint do
12
- first_name { 'Bill' }
13
- last_name { 'Nye' }
14
- email { FactoryGirl.create(:email) }
12
+ first_name { 'Bill' }
13
+ last_name { 'Nye' }
14
+ email { FactoryGirl.generate(:email) }
15
15
  end
16
16
  end
17
17
 
@@ -15,7 +15,7 @@ describe "a factory using make syntax" do
15
15
  end
16
16
  end
17
17
 
18
- describe "after making an instance" do
18
+ describe "after make" do
19
19
  before do
20
20
  @instance = User.make(:last_name => 'Rye')
21
21
  end
@@ -28,6 +28,24 @@ describe "a factory using make syntax" do
28
28
  @instance.last_name.should == 'Rye'
29
29
  end
30
30
 
31
+ it "should build the record" do
32
+ @instance.should be_new_record
33
+ end
34
+ end
35
+
36
+ describe "after make!" do
37
+ before do
38
+ @instance = User.make!(:last_name => 'Rye')
39
+ end
40
+
41
+ it "should use attributes from the factory" do
42
+ @instance.first_name.should == 'Bill'
43
+ end
44
+
45
+ it "should use attributes passed to make" do
46
+ @instance.last_name.should == 'Rye'
47
+ end
48
+
31
49
  it "should save the record" do
32
50
  @instance.should_not be_new_record
33
51
  end
@@ -11,7 +11,7 @@ describe "vintage syntax" do
11
11
  Factory.define :user do |factory|
12
12
  factory.first_name { 'Bill' }
13
13
  factory.last_name { 'Nye' }
14
- factory.email { Factory(:email) }
14
+ factory.email { Factory.next(:email) }
15
15
  end
16
16
  end
17
17
 
@@ -51,7 +51,7 @@ describe Factory, "given a parent factory" do
51
51
  before do
52
52
  @parent = FactoryGirl::Factory.new(:object)
53
53
  @parent.define_attribute(FactoryGirl::Attribute::Static.new(:name, 'value'))
54
- FactoryGirl.register(@parent)
54
+ FactoryGirl.register_factory(@parent)
55
55
  end
56
56
 
57
57
  it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
@@ -87,7 +87,7 @@ describe "defining a factory" do
87
87
 
88
88
  it "should add the factory to the list of factories" do
89
89
  Factory.define(@name) {|f| }
90
- @factory.should == FactoryGirl.find(@name)
90
+ @factory.should == FactoryGirl.factory_by_name(@name)
91
91
  end
92
92
  end
93
93
 
@@ -96,7 +96,7 @@ describe "after defining a factory" do
96
96
  @name = :user
97
97
  @factory = FactoryGirl::Factory.new(@name)
98
98
 
99
- FactoryGirl.register(@factory)
99
+ FactoryGirl.register_factory(@factory)
100
100
  end
101
101
 
102
102
  it "should use Proxy::AttributesFor for Factory.attributes_for" do
@@ -12,6 +12,10 @@ describe FactoryGirl::Attribute::Association do
12
12
  @attr.name.should == @name
13
13
  end
14
14
 
15
+ it "is an association" do
16
+ @attr.should be_association
17
+ end
18
+
15
19
  it "should have a factory" do
16
20
  @attr.factory.should == @factory
17
21
  end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Implicit do
4
+ before do
5
+ @name = :author
6
+ @attr = FactoryGirl::Attribute::Implicit.new(@name)
7
+ end
8
+
9
+ it "has a name" do
10
+ @attr.name.should == @name
11
+ end
12
+
13
+ context "with a known factory" do
14
+ before do
15
+ stub(FactoryGirl.factories).registered? { true }
16
+ end
17
+
18
+ it "associates the factory" do
19
+ proxy = "proxy"
20
+ stub(proxy).associate
21
+ @attr.add_to(proxy)
22
+ proxy.should have_received.associate(@name, @name, {})
23
+ end
24
+
25
+ it "is an association" do
26
+ @attr.should be_association
27
+ end
28
+
29
+ it "has a factory" do
30
+ @attr.factory.should == @name
31
+ end
32
+ end
33
+
34
+ context "with a known sequence" do
35
+ before do
36
+ FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@name, 1) { "magic" })
37
+ end
38
+
39
+ it "generates the sequence" do
40
+ proxy = "proxy"
41
+ stub(proxy).set
42
+ @attr.add_to(proxy)
43
+ proxy.should have_received.set(@name, "magic")
44
+ end
45
+
46
+ it "isn't an association" do
47
+ @attr.should_not be_association
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Sequence do
4
+ before do
5
+ @name = :first_name
6
+ @sequence = :name
7
+ FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@sequence, 5) { |n| "Name #{n}" })
8
+ @attr = FactoryGirl::Attribute::Sequence.new(@name, @sequence)
9
+ end
10
+
11
+ it "should have a name" do
12
+ @attr.name.should == @name
13
+ end
14
+
15
+ it "assigns the next value in the sequence" do
16
+ proxy = "proxy"
17
+ stub(proxy).set
18
+ @attr.add_to(proxy)
19
+ proxy.should have_received.set(@name, "Name 5")
20
+ end
21
+ end
@@ -10,6 +10,10 @@ describe FactoryGirl::Attribute do
10
10
  @attr.name.should == @name
11
11
  end
12
12
 
13
+ it "isn't an association" do
14
+ @attr.should_not be_association
15
+ end
16
+
13
17
  it "should do nothing when being added to a proxy" do
14
18
  @proxy = "proxy"
15
19
  stub(@proxy).set
@@ -111,22 +111,11 @@ describe FactoryGirl::DefinitionProxy do
111
111
  factory.attributes.should include(attribute)
112
112
  end
113
113
 
114
- it "adds an association when passed an undefined method without arguments or a block" do
114
+ it "adds an implicit attribute when passed an undefined method without arguments or a block" do
115
115
  name = :user
116
- FactoryGirl.register(FactoryGirl::Factory.new(name))
117
116
  attr = 'attribute'
118
117
  stub(attr).name { name }
119
- mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
120
- subject.send(name)
121
- factory.attributes.should include(attr)
122
- end
123
-
124
- it "adds a sequence when passed an undefined method without arguments or a block" do
125
- name = :airport
126
- FactoryGirl.register(FactoryGirl::Sequence.new(name))
127
- attr = 'attribute'
128
- stub(attr).name { name }
129
- mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
118
+ mock(FactoryGirl::Attribute::Implicit).new(name) { attr }
130
119
  subject.send(name)
131
120
  factory.attributes.should include(attr)
132
121
  end
@@ -89,12 +89,14 @@ describe FactoryGirl::Factory do
89
89
 
90
90
  it "should return associations" do
91
91
  factory = FactoryGirl::Factory.new(:post)
92
+ FactoryGirl.register_factory(FactoryGirl::Factory.new(:admin))
92
93
  factory.define_attribute(FactoryGirl::Attribute::Association.new(:author, :author, {}))
93
94
  factory.define_attribute(FactoryGirl::Attribute::Association.new(:editor, :editor, {}))
95
+ factory.define_attribute(FactoryGirl::Attribute::Implicit.new(:admin))
94
96
  factory.associations.each do |association|
95
- association.should be_a(FactoryGirl::Attribute::Association)
97
+ association.should be_association
96
98
  end
97
- factory.associations.size.should == 2
99
+ factory.associations.size.should == 3
98
100
  end
99
101
 
100
102
  it "should raise for a self referencing association" do
@@ -96,5 +96,14 @@ describe "definition loading" do
96
96
  it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
97
97
  end
98
98
  end
99
+
100
+ describe "with deeply nested factory files under #{dir}" do
101
+ in_directory_with_files File.join(dir, 'factories', 'subdirectory', 'post_factory.rb'),
102
+ File.join(dir, 'factories', 'subdirectory', 'person_factory.rb')
103
+ it_should_behave_like "finds definitions" do
104
+ it { should require_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") }
105
+ it { should require_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") }
106
+ end
107
+ end
99
108
  end
100
109
  end
@@ -21,7 +21,7 @@ describe FactoryGirl::Proxy::Build do
21
21
  before do
22
22
  @association = "associated-instance"
23
23
  @associated_factory = "associated-factory"
24
- stub(FactoryGirl).find { @associated_factory }
24
+ stub(FactoryGirl).factory_by_name { @associated_factory }
25
25
  stub(@associated_factory).run { @association }
26
26
  @overrides = { 'attr' => 'value' }
27
27
  @proxy.associate(:owner, :user, @overrides)
@@ -39,7 +39,7 @@ describe FactoryGirl::Proxy::Build do
39
39
  it "should run create when building an association" do
40
40
  association = "associated-instance"
41
41
  associated_factory = "associated-factory"
42
- stub(FactoryGirl).find { associated_factory }
42
+ stub(FactoryGirl).factory_by_name { associated_factory }
43
43
  stub(associated_factory).run { association }
44
44
  overrides = { 'attr' => 'value' }
45
45
  @proxy.association(:user, overrides).should == association
@@ -22,7 +22,7 @@ describe FactoryGirl::Proxy::Create do
22
22
  before do
23
23
  @association = "associated-instance"
24
24
  @associated_factory = "associated-factory"
25
- stub(FactoryGirl).find { @associated_factory }
25
+ stub(FactoryGirl).factory_by_name { @associated_factory }
26
26
  stub(@associated_factory).run { @association }
27
27
  @overrides = { 'attr' => 'value' }
28
28
  @proxy.associate(:owner, :user, @overrides)
@@ -40,7 +40,7 @@ describe FactoryGirl::Proxy::Create do
40
40
  it "should run create when building an association" do
41
41
  association = "associated-instance"
42
42
  associated_factory = "associated-factory"
43
- stub(FactoryGirl).find { associated_factory }
43
+ stub(FactoryGirl).factory_by_name { associated_factory }
44
44
  stub(associated_factory).run { association }
45
45
  overrides = { 'attr' => 'value' }
46
46
  @proxy.association(:user, overrides).should == association
@@ -16,6 +16,10 @@ describe FactoryGirl::Proxy::Stub do
16
16
  @stub.result(nil).should_not be_new_record
17
17
  end
18
18
 
19
+ it "should be persisted" do
20
+ @stub.result(nil).should be_persisted
21
+ end
22
+
19
23
  it "should not be able to connect to the database" do
20
24
  lambda { @stub.result(nil).reload }.should raise_error(RuntimeError)
21
25
  end
@@ -23,7 +27,7 @@ describe FactoryGirl::Proxy::Stub do
23
27
  describe "when a user factory exists" do
24
28
  before do
25
29
  @user = "user"
26
- stub(FactoryGirl).find { @associated_factory }
30
+ stub(FactoryGirl).factory_by_name { @associated_factory }
27
31
  @associated_factory = 'associate-factory'
28
32
  end
29
33
 
@@ -41,14 +41,13 @@ describe FactoryGirl::Registry do
41
41
  other_factory = FactoryGirl::Factory.new(:string)
42
42
  subject.add(factory)
43
43
  subject.add(other_factory)
44
- result = {}
44
+ result = []
45
45
 
46
- subject.each do |name, value|
47
- result[name] = value
46
+ subject.each do |value|
47
+ result << value
48
48
  end
49
49
 
50
- result.should == { factory.name => factory,
51
- other_factory.name => other_factory }
50
+ result.should =~ [factory, other_factory]
52
51
  end
53
52
 
54
53
  it "registers an sequence" do
@@ -71,22 +70,14 @@ describe FactoryGirl::Registry do
71
70
  end
72
71
  end
73
72
 
74
- context "on the FactoryGirl module" do
75
- it "finds a registered a factory" do
76
- FactoryGirl.register(factory)
77
- FactoryGirl.find(factory.name).should == factory
78
- end
79
-
80
- it "knows that a factory is registered by symbol" do
81
- FactoryGirl.register(factory)
82
- FactoryGirl.should be_registered(factory.name.to_sym)
83
- end
73
+ it "is enumerable" do
74
+ should be_kind_of(Enumerable)
75
+ end
84
76
 
85
- it "sets the registry" do
86
- registry = FactoryGirl::Registry.new
87
- FactoryGirl.registry = registry
88
- FactoryGirl.registry.should == registry
89
- end
77
+ it "clears registered factories" do
78
+ subject.add(factory)
79
+ subject.clear
80
+ subject.count.should == 0
90
81
  end
91
82
  end
92
83