masa-iwasaki-factory_girl 1.2.1.1 → 1.2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +56 -9
- data/Rakefile +19 -11
- data/lib/factory_girl.rb +2 -0
- data/lib/factory_girl/attribute/association.rb +2 -0
- data/lib/factory_girl/attribute/callback.rb +16 -0
- data/lib/factory_girl/attribute/dynamic.rb +1 -1
- data/lib/factory_girl/factory.rb +64 -44
- data/lib/factory_girl/proxy.rb +19 -0
- data/lib/factory_girl/proxy/build.rb +1 -0
- data/lib/factory_girl/proxy/create.rb +53 -1
- data/lib/factory_girl/proxy/stub.rb +7 -6
- data/lib/factory_girl/step_definitions.rb +54 -0
- data/spec/factory_girl/aliases_spec.rb +29 -0
- data/spec/factory_girl/attribute/association_spec.rb +29 -0
- data/spec/factory_girl/attribute/callback_spec.rb +23 -0
- data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
- data/spec/factory_girl/attribute/static_spec.rb +29 -0
- data/spec/factory_girl/attribute_spec.rb +30 -0
- data/spec/factory_girl/factory_spec.rb +582 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
- data/spec/factory_girl/proxy/build_spec.rb +81 -0
- data/spec/factory_girl/proxy/create_spec.rb +97 -0
- data/spec/factory_girl/proxy/stub_spec.rb +79 -0
- data/spec/factory_girl/proxy_spec.rb +84 -0
- data/spec/factory_girl/sequence_spec.rb +66 -0
- data/spec/factory_girl/syntax/blueprint_spec.rb +35 -0
- data/spec/factory_girl/syntax/generate_spec.rb +58 -0
- data/spec/factory_girl/syntax/make_spec.rb +35 -0
- data/spec/factory_girl/syntax/sham_spec.rb +36 -0
- data/spec/integration_spec.rb +304 -0
- data/spec/models.rb +43 -0
- data/spec/spec_helper.rb +18 -0
- metadata +49 -9
@@ -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).default_strategy { @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.default_strategy(: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).default_strategy { association }
|
40
|
+
@proxy.association(:user, attribs).should == association
|
41
|
+
Factory.should have_received.default_strategy(: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,97 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe Factory::Proxy::Create do
|
4
|
+
before do
|
5
|
+
Factory::clear_caches
|
6
|
+
@class = Class.new
|
7
|
+
@instance = "built-instance"
|
8
|
+
@association = "associated-instance"
|
9
|
+
|
10
|
+
stub(@class).new { @instance }
|
11
|
+
stub(Factory).default_strategy { @association }
|
12
|
+
stub(@instance).attribute { 'value' }
|
13
|
+
stub(@instance).attributes { {:key => 'value'} }
|
14
|
+
stub(@instance, :attribute=)
|
15
|
+
stub(@instance, :owner=)
|
16
|
+
stub(@instance).reload
|
17
|
+
stub(@instance).save!
|
18
|
+
|
19
|
+
@proxy = Factory::Proxy::Create.new(@class)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should instantiate the class" do
|
23
|
+
@class.should have_received.new
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "when asked to associate with another factory" do
|
27
|
+
before do
|
28
|
+
@proxy.associate(:owner, :user, {})
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should create the associated instance" do
|
32
|
+
Factory.should have_received.default_strategy(:user, {})
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should set the associated instance" do
|
36
|
+
@instance.should have_received.method_missing(:owner=, @association)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should call Factory.create when building an association" do
|
41
|
+
association = 'association'
|
42
|
+
attribs = { :first_name => 'Billy' }
|
43
|
+
stub(Factory).default_strategy { association }
|
44
|
+
@proxy.association(:user, attribs).should == association
|
45
|
+
Factory.should have_received.default_strategy(:user, attribs)
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "when asked for the result" do
|
49
|
+
before do
|
50
|
+
@build_spy = Object.new
|
51
|
+
@create_spy = Object.new
|
52
|
+
stub(@build_spy).foo
|
53
|
+
stub(@create_spy).foo
|
54
|
+
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
|
55
|
+
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
|
56
|
+
@result = @proxy.result
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should save the instance" do
|
60
|
+
@instance.should have_received.save!
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return the built instance" do
|
64
|
+
@result.should == @instance
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should run both the build and the create callbacks" do
|
68
|
+
@build_spy.should have_received.foo
|
69
|
+
@create_spy.should have_received.foo
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "when setting an attribute" do
|
74
|
+
before do
|
75
|
+
@proxy.set(:attribute, 'value')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should set that value" do
|
79
|
+
@instance.should have_received.method_missing(:attribute=, 'value')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "when getting an attribute" do
|
84
|
+
before do
|
85
|
+
@result = @proxy.get(:attribute)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should ask the built class for the value" do
|
89
|
+
@instance.should have_received.attribute
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return the value for that attribute" do
|
93
|
+
@result.should == 'value'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
@@ -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
|