dm-factory_girl 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +286 -0
  5. data/Rakefile +81 -0
  6. data/lib/factory_girl.rb +35 -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 +20 -0
  10. data/lib/factory_girl/attribute/callback.rb +16 -0
  11. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  12. data/lib/factory_girl/attribute/static.rb +17 -0
  13. data/lib/factory_girl/factory.rb +395 -0
  14. data/lib/factory_girl/proxy.rb +79 -0
  15. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  16. data/lib/factory_girl/proxy/build.rb +30 -0
  17. data/lib/factory_girl/proxy/create.rb +18 -0
  18. data/lib/factory_girl/proxy/stub.rb +50 -0
  19. data/lib/factory_girl/sequence.rb +63 -0
  20. data/lib/factory_girl/step_definitions.rb +54 -0
  21. data/lib/factory_girl/syntax.rb +12 -0
  22. data/lib/factory_girl/syntax/blueprint.rb +45 -0
  23. data/lib/factory_girl/syntax/generate.rb +73 -0
  24. data/lib/factory_girl/syntax/make.rb +43 -0
  25. data/lib/factory_girl/syntax/sham.rb +42 -0
  26. data/spec/factory_girl/aliases_spec.rb +29 -0
  27. data/spec/factory_girl/attribute/association_spec.rb +29 -0
  28. data/spec/factory_girl/attribute/callback_spec.rb +23 -0
  29. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  30. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  31. data/spec/factory_girl/attribute_spec.rb +30 -0
  32. data/spec/factory_girl/factory_spec.rb +571 -0
  33. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  34. data/spec/factory_girl/proxy/build_spec.rb +81 -0
  35. data/spec/factory_girl/proxy/create_spec.rb +94 -0
  36. data/spec/factory_girl/proxy/stub_spec.rb +79 -0
  37. data/spec/factory_girl/proxy_spec.rb +84 -0
  38. data/spec/factory_girl/sequence_spec.rb +66 -0
  39. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  40. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  41. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  42. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  43. data/spec/integration_spec.rb +305 -0
  44. data/spec/models.rb +43 -0
  45. data/spec/spec_helper.rb +30 -0
  46. metadata +123 -0
@@ -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,81 @@
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
+ it "should run the :after_build callback when retrieving the result" do
49
+ spy = Object.new
50
+ stub(spy).foo
51
+ @proxy.add_callback(:after_build, proc{ spy.foo })
52
+ @proxy.result
53
+ spy.should have_received.foo
54
+ end
55
+
56
+ describe "when setting an attribute" do
57
+ before do
58
+ stub(@instance).attribute = 'value'
59
+ @proxy.set(:attribute, 'value')
60
+ end
61
+
62
+ it "should set that value" do
63
+ @instance.should have_received.method_missing(:attribute=, 'value')
64
+ end
65
+ end
66
+
67
+ describe "when getting an attribute" do
68
+ before do
69
+ @result = @proxy.get(:attribute)
70
+ end
71
+
72
+ it "should ask the built class for the value" do
73
+ @instance.should have_received.attribute
74
+ end
75
+
76
+ it "should return the value for that attribute" do
77
+ @result.should == 'value'
78
+ end
79
+ end
80
+ end
81
+
@@ -0,0 +1,94 @@
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 { 'true' }
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
+ @build_spy = Object.new
48
+ @create_spy = Object.new
49
+ stub(@build_spy).foo
50
+ stub(@create_spy).foo
51
+ @proxy.add_callback(:after_build, proc{ @build_spy.foo })
52
+ @proxy.add_callback(:after_create, proc{ @create_spy.foo })
53
+ @result = @proxy.result
54
+ end
55
+
56
+ it "should save the instance" do
57
+ @instance.should have_received.save
58
+ end
59
+
60
+ it "should return the built instance" do
61
+ @result.should == @instance
62
+ end
63
+
64
+ it "should run both the build and the create callbacks" do
65
+ @build_spy.should have_received.foo
66
+ @create_spy.should have_received.foo
67
+ end
68
+ end
69
+
70
+ describe "when setting an attribute" do
71
+ before do
72
+ @proxy.set(:attribute, 'value')
73
+ end
74
+
75
+ it "should set that value" do
76
+ @instance.should have_received.method_missing(:attribute=, 'value')
77
+ end
78
+ end
79
+
80
+ describe "when getting an attribute" do
81
+ before do
82
+ @result = @proxy.get(:attribute)
83
+ end
84
+
85
+ it "should ask the built class for the value" do
86
+ @instance.should have_received.attribute
87
+ end
88
+
89
+ it "should return the value for that attribute" do
90
+ @result.should == 'value'
91
+ end
92
+ end
93
+ end
94
+
@@ -0,0 +1,79 @@
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
+ describe "when asked for the result" do
49
+ it "should return the actual instance" do
50
+ @stub.result.should == @instance
51
+ end
52
+
53
+ it "should run the :after_stub callback" do
54
+ @spy = Object.new
55
+ stub(@spy).foo
56
+ @stub.add_callback(:after_stub, proc{ @spy.foo })
57
+ @stub.result
58
+ @spy.should have_received.foo
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "with an existing attribute" do
64
+ before do
65
+ @value = "value"
66
+ mock(@instance).send(:attribute) { @value }
67
+ mock(@instance).send(:attribute=, @value)
68
+ @stub.set(:attribute, @value)
69
+ end
70
+
71
+ it "should to the resulting object" do
72
+ @stub.attribute.should == 'value'
73
+ end
74
+
75
+ it "should return that value when asked for that attribute" do
76
+ @stub.get(:attribute).should == @value
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,84 @@
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
+
29
+ describe "when adding callbacks" do
30
+ before do
31
+ @first_block = proc{ 'block 1' }
32
+ @second_block = proc{ 'block 2' }
33
+ end
34
+ it "should add a callback" do
35
+ @proxy.add_callback(:after_create, @first_block)
36
+ @proxy.callbacks[:after_create].should be_eql([@first_block])
37
+ end
38
+
39
+ it "should add multiple callbacks of the same name" do
40
+ @proxy.add_callback(:after_create, @first_block)
41
+ @proxy.add_callback(:after_create, @second_block)
42
+ @proxy.callbacks[:after_create].should be_eql([@first_block, @second_block])
43
+ end
44
+
45
+ it "should add multiple callbacks of different names" do
46
+ @proxy.add_callback(:after_create, @first_block)
47
+ @proxy.add_callback(:after_build, @second_block)
48
+ @proxy.callbacks[:after_create].should be_eql([@first_block])
49
+ @proxy.callbacks[:after_build].should be_eql([@second_block])
50
+ end
51
+ end
52
+
53
+ describe "when running callbacks" do
54
+ before do
55
+ @first_spy = Object.new
56
+ @second_spy = Object.new
57
+ stub(@first_spy).foo
58
+ stub(@second_spy).foo
59
+ end
60
+
61
+ it "should run all callbacks with a given name" do
62
+ @proxy.add_callback(:after_create, proc{ @first_spy.foo })
63
+ @proxy.add_callback(:after_create, proc{ @second_spy.foo })
64
+ @proxy.run_callbacks(:after_create)
65
+ @first_spy.should have_received.foo
66
+ @second_spy.should have_received.foo
67
+ end
68
+
69
+ it "should only run callbacks with a given name" do
70
+ @proxy.add_callback(:after_create, proc{ @first_spy.foo })
71
+ @proxy.add_callback(:after_build, proc{ @second_spy.foo })
72
+ @proxy.run_callbacks(:after_create)
73
+ @first_spy.should have_received.foo
74
+ @second_spy.should_not have_received.foo
75
+ end
76
+
77
+ it "should pass in the instance if the block takes an argument" do
78
+ @proxy.instance_variable_set("@instance", @first_spy)
79
+ @proxy.add_callback(:after_create, proc{|spy| spy.foo })
80
+ @proxy.run_callbacks(:after_create)
81
+ @first_spy.should have_received.foo
82
+ end
83
+ end
84
+ 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