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,52 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy::AttributesFor do
4
+ before do
5
+ @proxy = Factory::Proxy::AttributesFor.new(@class)
6
+ end
7
+
8
+ describe "when asked to associate with another factory" do
9
+ before do
10
+ stub(Factory).create
11
+ @proxy.associate(:owner, :user, {})
12
+ end
13
+
14
+ it "should not set a value for the association" do
15
+ (@proxy.result.key?(:owner)).should_not be
16
+ end
17
+ end
18
+
19
+ it "should return nil when building an association" do
20
+ @proxy.association(:user).should be_nil
21
+ end
22
+
23
+ it "should not call Factory.create when building an association" do
24
+ stub(Factory).create
25
+ @proxy.association(:user).should be_nil
26
+ Factory.should have_received.create.never
27
+ end
28
+
29
+ it "should always return nil when building an association" do
30
+ @proxy.set(:association, 'x')
31
+ @proxy.association(:user).should be_nil
32
+ end
33
+
34
+ it "should return a hash when asked for the result" do
35
+ @proxy.result.should be_kind_of(Hash)
36
+ end
37
+
38
+ describe "after setting an attribute" do
39
+ before do
40
+ @proxy.set(:attribute, 'value')
41
+ end
42
+
43
+ it "should set that value in the resulting hash" do
44
+ @proxy.result[:attribute].should == 'value'
45
+ end
46
+
47
+ it "should return that value when asked for that attribute" do
48
+ @proxy.get(:attribute).should == 'value'
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy::Build do
4
+ before do
5
+ @class = Class.new
6
+ @instance = "built-instance"
7
+ @association = "associated-instance"
8
+
9
+ stub(@class).new { @instance }
10
+ stub(@instance).attribute { 'value' }
11
+ stub(Factory).create { @association }
12
+ stub(@instance, :attribute=)
13
+ stub(@instance, :owner=)
14
+
15
+ @proxy = Factory::Proxy::Build.new(@class)
16
+ end
17
+
18
+ it "should instantiate the class" do
19
+ @class.should have_received.new
20
+ end
21
+
22
+ describe "when asked to associate with another factory" do
23
+ before do
24
+ @proxy.associate(:owner, :user, {})
25
+ end
26
+
27
+ it "should create the associated instance" do
28
+ Factory.should have_received.create(:user, {})
29
+ end
30
+
31
+ it "should set the associated instance" do
32
+ @instance.should have_received.method_missing(:owner=, @association)
33
+ end
34
+ end
35
+
36
+ it "should call Factory.create when building an association" do
37
+ association = 'association'
38
+ attribs = { :first_name => 'Billy' }
39
+ stub(Factory).create { association }
40
+ @proxy.association(:user, attribs).should == association
41
+ Factory.should have_received.create(:user, attribs)
42
+ end
43
+
44
+ it "should return the built instance when asked for the result" do
45
+ @proxy.result.should == @instance
46
+ end
47
+
48
+ describe "when setting an attribute" do
49
+ before do
50
+ stub(@instance).attribute = 'value'
51
+ @proxy.set(:attribute, 'value')
52
+ end
53
+
54
+ it "should set that value" do
55
+ @instance.should have_received.method_missing(:attribute=, 'value')
56
+ end
57
+ end
58
+
59
+ describe "when getting an attribute" do
60
+ before do
61
+ @result = @proxy.get(:attribute)
62
+ end
63
+
64
+ it "should ask the built class for the value" do
65
+ @instance.should have_received.attribute
66
+ end
67
+
68
+ it "should return the value for that attribute" do
69
+ @result.should == 'value'
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,83 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy::Create do
4
+ before do
5
+ @class = Class.new
6
+ @instance = "built-instance"
7
+ @association = "associated-instance"
8
+
9
+ stub(@class).new { @instance }
10
+ stub(Factory).create { @association }
11
+ stub(@instance).attribute { 'value' }
12
+ stub(@instance, :attribute=)
13
+ stub(@instance, :owner=)
14
+ stub(@instance).save!
15
+
16
+ @proxy = Factory::Proxy::Create.new(@class)
17
+ end
18
+
19
+ it "should instantiate the class" do
20
+ @class.should have_received.new
21
+ end
22
+
23
+ describe "when asked to associate with another factory" do
24
+ before do
25
+ @proxy.associate(:owner, :user, {})
26
+ end
27
+
28
+ it "should create the associated instance" do
29
+ Factory.should have_received.create(:user, {})
30
+ end
31
+
32
+ it "should set the associated instance" do
33
+ @instance.should have_received.method_missing(:owner=, @association)
34
+ end
35
+ end
36
+
37
+ it "should call Factory.create when building an association" do
38
+ association = 'association'
39
+ attribs = { :first_name => 'Billy' }
40
+ stub(Factory).create { association }
41
+ @proxy.association(:user, attribs).should == association
42
+ Factory.should have_received.create(:user, attribs)
43
+ end
44
+
45
+ describe "when asked for the result" do
46
+ before do
47
+ @result = @proxy.result
48
+ end
49
+
50
+ it "should save the instance" do
51
+ @instance.should have_received.save!
52
+ end
53
+
54
+ it "should return the built instance" do
55
+ @result.should == @instance
56
+ end
57
+ end
58
+
59
+ describe "when setting an attribute" do
60
+ before do
61
+ @proxy.set(:attribute, 'value')
62
+ end
63
+
64
+ it "should set that value" do
65
+ @instance.should have_received.method_missing(:attribute=, 'value')
66
+ end
67
+ end
68
+
69
+ describe "when getting an attribute" do
70
+ before do
71
+ @result = @proxy.get(:attribute)
72
+ end
73
+
74
+ it "should ask the built class for the value" do
75
+ @instance.should have_received.attribute
76
+ end
77
+
78
+ it "should return the value for that attribute" do
79
+ @result.should == 'value'
80
+ end
81
+ end
82
+ end
83
+
@@ -0,0 +1,69 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy::Stub do
4
+ before do
5
+ @class = "class"
6
+ @instance = "instance"
7
+ stub(@class).new { @instance }
8
+ stub(@instance, :id=)
9
+ stub(@instance).id { 42 }
10
+ stub(@instance).reload { @instance.connection.reload }
11
+
12
+ @stub = Factory::Proxy::Stub.new(@class)
13
+ end
14
+
15
+ it "should not be a new record" do
16
+ @stub.result.should_not be_new_record
17
+ end
18
+
19
+ it "should not be able to connect to the database" do
20
+ lambda { @stub.result.reload }.should raise_error(RuntimeError)
21
+ end
22
+
23
+ describe "when a user factory exists" do
24
+ before do
25
+ @user = "user"
26
+ stub(Factory).stub(:user, {}) { @user }
27
+ end
28
+
29
+ describe "when asked to associate with another factory" do
30
+ before do
31
+ stub(@instance).owner { @user }
32
+ mock(Factory).stub(:user, {}) { @user }
33
+ mock(@stub).set(:owner, @user)
34
+
35
+ @stub.associate(:owner, :user, {})
36
+ end
37
+
38
+ it "should set a value for the association" do
39
+ @stub.result.owner.should == @user
40
+ end
41
+ end
42
+
43
+ it "should return the association when building one" do
44
+ mock(Factory).create.never
45
+ @stub.association(:user).should == @user
46
+ end
47
+
48
+ it "should return the actual instance when asked for the result" do
49
+ @stub.result.should == @instance
50
+ end
51
+ end
52
+
53
+ describe "with an existing attribute" do
54
+ before do
55
+ @value = "value"
56
+ mock(@instance).send(:attribute) { @value }
57
+ mock(@instance).send(:attribute=, @value)
58
+ @stub.set(:attribute, @value)
59
+ end
60
+
61
+ it "should to the resulting object" do
62
+ @stub.attribute.should == 'value'
63
+ end
64
+
65
+ it "should return that value when asked for that attribute" do
66
+ @stub.get(:attribute).should == @value
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy do
4
+ before do
5
+ @proxy = Factory::Proxy.new(Class.new)
6
+ end
7
+
8
+ it "should do nothing when asked to set an attribute to a value" do
9
+ lambda { @proxy.set(:name, 'a name') }.should_not raise_error
10
+ end
11
+
12
+ it "should return nil when asked for an attribute" do
13
+ @proxy.get(:name).should be_nil
14
+ end
15
+
16
+ it "should call get for a missing method" do
17
+ mock(@proxy).get(:name) { "it's a name" }
18
+ @proxy.name.should == "it's a name"
19
+ end
20
+
21
+ it "should do nothing when asked to associate with another factory" do
22
+ lambda { @proxy.associate(:owner, :user, {}) }.should_not raise_error
23
+ end
24
+
25
+ it "should raise an error when asked for the result" do
26
+ lambda { @proxy.result }.should raise_error(NotImplementedError)
27
+ end
28
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Factory::Sequence do
4
+ describe "a sequence" do
5
+ before do
6
+ @sequence = Factory::Sequence.new {|n| "=#{n}" }
7
+ end
8
+
9
+ it "should start with a value of 1" do
10
+ @sequence.next.should == "=1"
11
+ end
12
+
13
+ describe "after being called" do
14
+ before do
15
+ @sequence.next
16
+ end
17
+
18
+ it "should use the next value" do
19
+ @sequence.next.should == "=2"
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "defining a sequence" do
25
+ before do
26
+ @sequence = "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
41
+ 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
+
53
+ Factory.sequence(@name) {}
54
+ end
55
+
56
+ it "should call next on the sequence when sent next" do
57
+ mock(@sequence).next
58
+
59
+ Factory.next(@name)
60
+ end
61
+
62
+ it "should return the value from the sequence" do
63
+ Factory.next(@name).should == @value
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/blueprint'
4
+
5
+ describe "a blueprint" do
6
+ before do
7
+ Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
8
+ User.blueprint do
9
+ first_name { 'Bill' }
10
+ last_name { 'Nye' }
11
+ email { Factory.next(:email) }
12
+ end
13
+ end
14
+
15
+ after do
16
+ Factory.factories.clear
17
+ Factory.sequences.clear
18
+ end
19
+
20
+ describe "after making an instance" do
21
+ before do
22
+ @instance = Factory(:user, :last_name => 'Rye')
23
+ end
24
+
25
+ it "should use attributes from the blueprint" do
26
+ @instance.first_name.should == 'Bill'
27
+ end
28
+
29
+ it "should evaluate attribute blocks for each instance" do
30
+ @instance.email.should =~ /somebody\d+@example.com/
31
+ Factory(:user).email.should_not == @instance.email
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ require 'factory_girl/syntax/generate'
4
+
5
+ describe "a factory using generate 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
+ it "should not raise an error when generating an invalid instance" do
19
+ lambda { User.generate(:first_name => nil) }.should_not raise_error
20
+ end
21
+
22
+ it "should raise an error when forcefully generating an invalid instance" do
23
+ lambda { User.generate!(:first_name => nil) }.should raise_error(ActiveRecord::RecordInvalid)
24
+ end
25
+
26
+ %w(generate generate! spawn).each do |method|
27
+ it "should yield a generated instance when using #{method} with a block" do
28
+ saved_instance = nil
29
+ User.send(method) {|instance| saved_instance = instance }
30
+ saved_instance.should be_kind_of(User)
31
+ end
32
+
33
+ describe "after generating an instance using #{method}" do
34
+ before do
35
+ @instance = User.send(method, :last_name => 'Rye')
36
+ end
37
+
38
+ it "should use attributes from the factory" do
39
+ @instance.first_name.should == 'Bill'
40
+ end
41
+
42
+ it "should use attributes passed to generate" do
43
+ @instance.last_name.should == 'Rye'
44
+ end
45
+
46
+ if method == 'spawn'
47
+ it "should not save the record" do
48
+ @instance.should be_new_record
49
+ end
50
+ else
51
+ it "should save the record" do
52
+ @instance.should_not be_new_record
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end