thoughtbot-factory_girl 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CONTRIBUTION_GUIDELINES.rdoc +4 -5
- data/README.rdoc +6 -6
- data/Rakefile +11 -14
- data/lib/factory_girl/attribute.rb +3 -2
- data/lib/factory_girl/attribute/dynamic.rb +5 -2
- data/lib/factory_girl/proxy/stub.rb +33 -12
- data/lib/factory_girl/sequence.rb +3 -0
- data/spec/factory_girl/aliases_spec.rb +29 -0
- data/spec/factory_girl/attribute/association_spec.rb +25 -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 +490 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
- data/spec/factory_girl/proxy/build_spec.rb +73 -0
- data/spec/factory_girl/proxy/create_spec.rb +83 -0
- data/spec/factory_girl/proxy/stub_spec.rb +69 -0
- data/spec/factory_girl/proxy_spec.rb +28 -0
- data/spec/factory_girl/sequence_spec.rb +66 -0
- data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
- data/spec/factory_girl/syntax/generate_spec.rb +57 -0
- data/spec/factory_girl/syntax/make_spec.rb +35 -0
- data/spec/factory_girl/syntax/sham_spec.rb +35 -0
- data/spec/integration_spec.rb +265 -0
- data/{test → spec}/models.rb +0 -0
- data/{test/test_helper.rb → spec/spec_helper.rb} +12 -5
- metadata +42 -43
- data/test/aliases_test.rb +0 -29
- data/test/association_attribute_test.rb +0 -31
- data/test/attribute_test.rb +0 -32
- data/test/attributes_for_strategy_test.rb +0 -55
- data/test/build_strategy_test.rb +0 -79
- data/test/create_strategy_test.rb +0 -90
- data/test/dynamic_attribute_test.rb +0 -41
- data/test/factory_test.rb +0 -513
- data/test/integration_test.rb +0 -235
- data/test/sequence_test.rb +0 -76
- data/test/static_attribute_test.rb +0 -33
- data/test/strategy_test.rb +0 -33
- data/test/stub_strategy_test.rb +0 -61
- data/test/syntax/blueprint_test.rb +0 -39
- data/test/syntax/generate_test.rb +0 -63
- data/test/syntax/make_test.rb +0 -39
- data/test/syntax/sham_test.rb +0 -40
data/test/aliases_test.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class AliasesTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
should "include an attribute as an alias for itself by default" do
|
6
|
-
assert Factory.aliases_for(:test).include?(:test)
|
7
|
-
end
|
8
|
-
|
9
|
-
should "include the root of a foreign key as an alias by default" do
|
10
|
-
assert Factory.aliases_for(:test_id).include?(:test)
|
11
|
-
end
|
12
|
-
|
13
|
-
should "include an attribute's foreign key as an alias by default" do
|
14
|
-
assert Factory.aliases_for(:test).include?(:test_id)
|
15
|
-
end
|
16
|
-
|
17
|
-
context "after adding an alias" do
|
18
|
-
|
19
|
-
setup do
|
20
|
-
Factory.alias(/(.*)_suffix/, '\1')
|
21
|
-
end
|
22
|
-
|
23
|
-
should "return the alias in the aliases list" do
|
24
|
-
assert Factory.aliases_for(:test_suffix).include?(:test)
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class AssociationAttributeTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "an association" do
|
6
|
-
setup do
|
7
|
-
@name = :author
|
8
|
-
@factory = :user
|
9
|
-
@overrides = { :first_name => 'John' }
|
10
|
-
@attr = Factory::Attribute::Association.new(@name,
|
11
|
-
@factory,
|
12
|
-
@overrides)
|
13
|
-
end
|
14
|
-
|
15
|
-
should "have a name" do
|
16
|
-
assert_equal @name, @attr.name
|
17
|
-
end
|
18
|
-
|
19
|
-
should "tell the proxy to associate when being added to a proxy" do
|
20
|
-
proxy = mock('proxy')
|
21
|
-
proxy.expects(:associate).with(@name, @factory, @overrides)
|
22
|
-
@attr.add_to(proxy)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
should "convert names to symbols" do
|
27
|
-
assert_equal :name,
|
28
|
-
Factory::Attribute::Association.new('name', :user, {}).name
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
data/test/attribute_test.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class AttributeTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "an attribute" do
|
6
|
-
setup do
|
7
|
-
@name = :user
|
8
|
-
@attr = Factory::Attribute.new(@name)
|
9
|
-
end
|
10
|
-
|
11
|
-
should "have a name" do
|
12
|
-
assert_equal @name, @attr.name
|
13
|
-
end
|
14
|
-
|
15
|
-
should "do nothing when being added to a proxy" do
|
16
|
-
@proxy = mock('proxy')
|
17
|
-
@proxy.expects(:set).never
|
18
|
-
@attr.add_to(@proxy)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
should "raise an error when defining an attribute writer" do
|
23
|
-
assert_raise Factory::AttributeDefinitionError do
|
24
|
-
Factory::Attribute.new('test=')
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
should "convert names to symbols" do
|
29
|
-
assert_equal :name, Factory::Attribute.new('name').name
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class AttributesForProxyTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "the attributes_for proxy" do
|
6
|
-
setup do
|
7
|
-
@proxy = Factory::Proxy::AttributesFor.new(@class)
|
8
|
-
end
|
9
|
-
|
10
|
-
context "when asked to associate with another factory" do
|
11
|
-
setup do
|
12
|
-
Factory.stubs(:create)
|
13
|
-
@proxy.associate(:owner, :user, {})
|
14
|
-
end
|
15
|
-
|
16
|
-
should "not set a value for the association" do
|
17
|
-
assert !@proxy.result.key?(:owner)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
should "return nil when building an association" do
|
22
|
-
assert_nil @proxy.association(:user)
|
23
|
-
end
|
24
|
-
|
25
|
-
should "not call Factory.create when building an association" do
|
26
|
-
Factory.expects(:create).never
|
27
|
-
assert_nil @proxy.association(:user)
|
28
|
-
end
|
29
|
-
|
30
|
-
should "always return nil when building an association" do
|
31
|
-
@proxy.set(:association, 'x')
|
32
|
-
assert_nil @proxy.association(:user)
|
33
|
-
end
|
34
|
-
|
35
|
-
should "return a hash when asked for the result" do
|
36
|
-
assert_kind_of Hash, @proxy.result
|
37
|
-
end
|
38
|
-
|
39
|
-
context "after setting an attribute" do
|
40
|
-
setup do
|
41
|
-
@proxy.set(:attribute, 'value')
|
42
|
-
end
|
43
|
-
|
44
|
-
should "set that value in the resulting hash" do
|
45
|
-
assert_equal 'value', @proxy.result[:attribute]
|
46
|
-
end
|
47
|
-
|
48
|
-
should "return that value when asked for that attribute" do
|
49
|
-
assert_equal 'value', @proxy.get(:attribute)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
data/test/build_strategy_test.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class BuildProxyTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "with a class to build" do
|
6
|
-
setup do
|
7
|
-
@class = Class.new
|
8
|
-
@instance = mock('built-instance')
|
9
|
-
@association = mock('associated-instance')
|
10
|
-
|
11
|
-
@class. stubs(:new). returns(@instance)
|
12
|
-
@instance.stubs(:attribute).returns('value')
|
13
|
-
Factory. stubs(:create). returns(@association)
|
14
|
-
@instance.stubs(:attribute=)
|
15
|
-
@instance.stubs(:owner=)
|
16
|
-
end
|
17
|
-
|
18
|
-
context "the build proxy" do
|
19
|
-
setup do
|
20
|
-
@proxy = Factory::Proxy::Build.new(@class)
|
21
|
-
end
|
22
|
-
|
23
|
-
before_should "instantiate the class" do
|
24
|
-
@class.expects(:new).with().returns(@instance)
|
25
|
-
end
|
26
|
-
|
27
|
-
context "when asked to associate with another factory" do
|
28
|
-
setup do
|
29
|
-
@proxy.associate(:owner, :user, {})
|
30
|
-
end
|
31
|
-
|
32
|
-
before_should "create the associated instance" do
|
33
|
-
Factory.expects(:create).with(:user, {}).returns(@association)
|
34
|
-
end
|
35
|
-
|
36
|
-
before_should "set the associated instance" do
|
37
|
-
@instance.expects(:owner=).with(@association)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
should "call Factory.create when building an association" do
|
42
|
-
association = 'association'
|
43
|
-
attribs = { :first_name => 'Billy' }
|
44
|
-
Factory.expects(:create).with(:user, attribs).returns(association)
|
45
|
-
assert_equal association, @proxy.association(:user, attribs)
|
46
|
-
end
|
47
|
-
|
48
|
-
should "return the built instance when asked for the result" do
|
49
|
-
assert_equal @instance, @proxy.result
|
50
|
-
end
|
51
|
-
|
52
|
-
context "when setting an attribute" do
|
53
|
-
setup do
|
54
|
-
@proxy.set(:attribute, 'value')
|
55
|
-
end
|
56
|
-
|
57
|
-
before_should "set that value" do
|
58
|
-
@instance.expects(:attribute=).with('value')
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "when getting an attribute" do
|
63
|
-
setup do
|
64
|
-
@result = @proxy.get(:attribute)
|
65
|
-
end
|
66
|
-
|
67
|
-
before_should "ask the built class for the value" do
|
68
|
-
@instance.expects(:attribute).with().returns('value')
|
69
|
-
end
|
70
|
-
|
71
|
-
should "return the value for that attribute" do
|
72
|
-
assert_equal 'value', @result
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class CreateProxyTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "with a class to build" do
|
6
|
-
setup do
|
7
|
-
@class = Class.new
|
8
|
-
@instance = mock('built-instance')
|
9
|
-
@association = mock('associated-instance')
|
10
|
-
|
11
|
-
@class. stubs(:new). returns(@instance)
|
12
|
-
Factory. stubs(:create). returns(@association)
|
13
|
-
@instance.stubs(:attribute).returns('value')
|
14
|
-
@instance.stubs(:attribute=)
|
15
|
-
@instance.stubs(:owner=)
|
16
|
-
@instance.stubs(:save!)
|
17
|
-
end
|
18
|
-
|
19
|
-
context "the create proxy" do
|
20
|
-
setup do
|
21
|
-
@proxy = Factory::Proxy::Create.new(@class)
|
22
|
-
end
|
23
|
-
|
24
|
-
before_should "instantiate the class" do
|
25
|
-
@class.expects(:new).with().returns(@instance)
|
26
|
-
end
|
27
|
-
|
28
|
-
context "when asked to associate with another factory" do
|
29
|
-
setup do
|
30
|
-
@proxy.associate(:owner, :user, {})
|
31
|
-
end
|
32
|
-
|
33
|
-
before_should "create the associated instance" do
|
34
|
-
Factory.expects(:create).with(:user, {}).returns(@association)
|
35
|
-
end
|
36
|
-
|
37
|
-
before_should "set the associated instance" do
|
38
|
-
@instance.expects(:owner=).with(@association)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
should "call Factory.create when building an association" do
|
43
|
-
association = 'association'
|
44
|
-
attribs = { :first_name => 'Billy' }
|
45
|
-
Factory.expects(:create).with(:user, attribs).returns(association)
|
46
|
-
assert_equal association, @proxy.association(:user, attribs)
|
47
|
-
end
|
48
|
-
|
49
|
-
context "when asked for the result" do
|
50
|
-
setup do
|
51
|
-
@result = @proxy.result
|
52
|
-
end
|
53
|
-
|
54
|
-
before_should "save the instance" do
|
55
|
-
@instance.expects(:save!).with()
|
56
|
-
end
|
57
|
-
|
58
|
-
should "return the built instance" do
|
59
|
-
assert_equal @instance, @result
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context "when setting an attribute" do
|
64
|
-
setup do
|
65
|
-
@proxy.set(:attribute, 'value')
|
66
|
-
end
|
67
|
-
|
68
|
-
before_should "set that value" do
|
69
|
-
@instance.expects(:attribute=).with('value')
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
context "when getting an attribute" do
|
74
|
-
setup do
|
75
|
-
@result = @proxy.get(:attribute)
|
76
|
-
end
|
77
|
-
|
78
|
-
before_should "ask the built class for the value" do
|
79
|
-
@instance.expects(:attribute).with().returns('value')
|
80
|
-
end
|
81
|
-
|
82
|
-
should "return the value for that attribute" do
|
83
|
-
assert_equal 'value', @result
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class DynamicAttributeTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "a static attribute" do
|
6
|
-
setup do
|
7
|
-
@name = :first_name
|
8
|
-
@block = lambda { 'value' }
|
9
|
-
@attr = Factory::Attribute::Dynamic.new(@name, @block)
|
10
|
-
end
|
11
|
-
|
12
|
-
should "have a name" do
|
13
|
-
assert_equal @name, @attr.name
|
14
|
-
end
|
15
|
-
|
16
|
-
should "call the block to set a value" do
|
17
|
-
@proxy = mock('proxy')
|
18
|
-
@proxy.expects(:set).with(@name, 'value')
|
19
|
-
@attr.add_to(@proxy)
|
20
|
-
end
|
21
|
-
|
22
|
-
should "yield the proxy to the block when adding its value to a proxy" do
|
23
|
-
@block = lambda {|a| a }
|
24
|
-
@attr = Factory::Attribute::Dynamic.new(:user, @block)
|
25
|
-
@proxy = mock('proxy')
|
26
|
-
@proxy.expects(:set).with(:user, @proxy)
|
27
|
-
@attr.add_to(@proxy)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
should "raise an error when defining an attribute writer" do
|
32
|
-
assert_raise Factory::AttributeDefinitionError do
|
33
|
-
Factory::Attribute::Dynamic.new('test=', nil)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
should "convert names to symbols" do
|
38
|
-
assert_equal :name, Factory::Attribute::Dynamic.new('name', nil).name
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
data/test/factory_test.rb
DELETED
@@ -1,513 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class FactoryTest < Test::Unit::TestCase
|
4
|
-
factory = Factory.new(:post)
|
5
|
-
context "defining a factory" do
|
6
|
-
setup do
|
7
|
-
@name = :user
|
8
|
-
@factory = mock('factory')
|
9
|
-
@factory.stubs(:factory_name).returns(@name)
|
10
|
-
@options = { :class => 'magic' }
|
11
|
-
Factory.stubs(:new).returns(@factory)
|
12
|
-
end
|
13
|
-
|
14
|
-
should "create a new factory using the specified name and options" do
|
15
|
-
Factory.expects(:new).with(@name, @options).returns(@factory)
|
16
|
-
Factory.define(@name, @options) {|f| }
|
17
|
-
end
|
18
|
-
|
19
|
-
should "pass the factory do the block" do
|
20
|
-
yielded = nil
|
21
|
-
Factory.define(@name) do |y|
|
22
|
-
yielded = y
|
23
|
-
end
|
24
|
-
assert_equal @factory, yielded
|
25
|
-
end
|
26
|
-
|
27
|
-
should "add the factory to the list of factories" do
|
28
|
-
Factory.define(@name) {|f| }
|
29
|
-
assert_equal Factory.factories[@name],
|
30
|
-
@factory,
|
31
|
-
"Factories: #{Factory.factories.inspect}"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "a factory" do
|
36
|
-
setup do
|
37
|
-
@name = :user
|
38
|
-
@class = User
|
39
|
-
@factory = Factory.new(@name)
|
40
|
-
end
|
41
|
-
|
42
|
-
should "have a factory name" do
|
43
|
-
assert_equal @name, @factory.factory_name
|
44
|
-
end
|
45
|
-
|
46
|
-
should "have a build class" do
|
47
|
-
assert_equal @class, @factory.build_class
|
48
|
-
end
|
49
|
-
|
50
|
-
should "have a default strategy" do
|
51
|
-
assert_equal :create, @factory.default_strategy
|
52
|
-
end
|
53
|
-
|
54
|
-
should "not allow the same attribute to be added twice" do
|
55
|
-
assert_raise(Factory::AttributeDefinitionError) do
|
56
|
-
2.times { @factory.add_attribute :first_name }
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
should "add a static attribute when an attribute is defined with a value" do
|
61
|
-
attribute = mock('attribute', :name => :name)
|
62
|
-
Factory::Attribute::Static.
|
63
|
-
expects(:new).
|
64
|
-
with(:name, 'value').
|
65
|
-
returns(attribute)
|
66
|
-
@factory.add_attribute(:name, 'value')
|
67
|
-
end
|
68
|
-
|
69
|
-
should "add a dynamic attribute when an attribute is defined with a block" do
|
70
|
-
attribute = mock('attribute', :name => :name)
|
71
|
-
block = lambda {}
|
72
|
-
Factory::Attribute::Dynamic.
|
73
|
-
expects(:new).
|
74
|
-
with(:name, block).
|
75
|
-
returns(attribute)
|
76
|
-
@factory.add_attribute(:name, &block)
|
77
|
-
end
|
78
|
-
|
79
|
-
should "raise for an attribute with a value and a block" do
|
80
|
-
assert_raise(Factory::AttributeDefinitionError) do
|
81
|
-
@factory.add_attribute(:name, 'value') {}
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context "adding an attribute using a in-line sequence" do
|
86
|
-
should 'create the sequence' do
|
87
|
-
Factory::Sequence.
|
88
|
-
expects(:new)
|
89
|
-
@factory.sequence(:name) {}
|
90
|
-
end
|
91
|
-
|
92
|
-
should 'add a dynamic attribute' do
|
93
|
-
attr = mock('attribute', :name => :name)
|
94
|
-
Factory::Attribute::Dynamic.
|
95
|
-
expects(:new).
|
96
|
-
with(:name, instance_of(Proc)).
|
97
|
-
returns(attr)
|
98
|
-
@factory.sequence(:name) {}
|
99
|
-
assert @factory.attributes.include?(attr)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context "after adding an attribute" do
|
104
|
-
setup do
|
105
|
-
@attribute = mock('attribute')
|
106
|
-
@proxy = mock('proxy')
|
107
|
-
|
108
|
-
@attribute. stubs(:name). returns(:name)
|
109
|
-
@attribute. stubs(:add_to)
|
110
|
-
@proxy. stubs(:set)
|
111
|
-
@proxy. stubs(:result).returns('result')
|
112
|
-
Factory::Attribute::Static.stubs(:new). returns(@attribute)
|
113
|
-
Factory::Proxy::Build. stubs(:new). returns(@proxy)
|
114
|
-
|
115
|
-
@factory.add_attribute(:name, 'value')
|
116
|
-
end
|
117
|
-
|
118
|
-
should "create the right proxy using the build class when running" do
|
119
|
-
Factory::Proxy::Build.
|
120
|
-
expects(:new).
|
121
|
-
with(@factory.build_class).
|
122
|
-
returns(@proxy)
|
123
|
-
@factory.run(Factory::Proxy::Build, {})
|
124
|
-
end
|
125
|
-
|
126
|
-
should "add the attribute to the proxy when running" do
|
127
|
-
@attribute.expects(:add_to).with(@proxy)
|
128
|
-
@factory.run(Factory::Proxy::Build, {})
|
129
|
-
end
|
130
|
-
|
131
|
-
should "return the result from the proxy when running" do
|
132
|
-
@proxy.expects(:result).with().returns('result')
|
133
|
-
assert_equal 'result',
|
134
|
-
@factory.run(Factory::Proxy::Build, {})
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
should "add an association without a factory name or overrides" do
|
139
|
-
factory = Factory.new(:post)
|
140
|
-
name = :user
|
141
|
-
attr = 'attribute'
|
142
|
-
Factory::Attribute::Association.
|
143
|
-
expects(:new).
|
144
|
-
with(name, name, {}).
|
145
|
-
returns(attr)
|
146
|
-
factory.association(name)
|
147
|
-
assert factory.attributes.include?(attr)
|
148
|
-
end
|
149
|
-
|
150
|
-
should "add an association with overrides" do
|
151
|
-
factory = Factory.new(:post)
|
152
|
-
name = :user
|
153
|
-
attr = 'attribute'
|
154
|
-
overrides = { :first_name => 'Ben' }
|
155
|
-
Factory::Attribute::Association.
|
156
|
-
expects(:new).
|
157
|
-
with(name, name, overrides).
|
158
|
-
returns(attr)
|
159
|
-
factory.association(name, overrides)
|
160
|
-
assert factory.attributes.include?(attr)
|
161
|
-
end
|
162
|
-
|
163
|
-
should "add an association with a factory name" do
|
164
|
-
factory = Factory.new(:post)
|
165
|
-
attr = 'attribute'
|
166
|
-
Factory::Attribute::Association.
|
167
|
-
expects(:new).
|
168
|
-
with(:author, :user, {}).
|
169
|
-
returns(attr)
|
170
|
-
factory.association(:author, :factory => :user)
|
171
|
-
assert factory.attributes.include?(attr)
|
172
|
-
end
|
173
|
-
|
174
|
-
should "add an association with a factory name and overrides" do
|
175
|
-
factory = Factory.new(:post)
|
176
|
-
attr = 'attribute'
|
177
|
-
Factory::Attribute::Association.
|
178
|
-
expects(:new).
|
179
|
-
with(:author, :user, :first_name => 'Ben').
|
180
|
-
returns(attr)
|
181
|
-
factory.association(:author, :factory => :user, :first_name => 'Ben')
|
182
|
-
assert factory.attributes.include?(attr)
|
183
|
-
end
|
184
|
-
|
185
|
-
should "raise for a self referencing association" do
|
186
|
-
factory = Factory.new(:post)
|
187
|
-
assert_raise(Factory::AssociationDefinitionError) do
|
188
|
-
factory.association(:parent, :factory => :post)
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
should "add an attribute using the method name when passed an undefined method" do
|
193
|
-
attr = mock('attribute', :name => :name)
|
194
|
-
block = lambda {}
|
195
|
-
Factory::Attribute::Static.
|
196
|
-
expects(:new).
|
197
|
-
with(:name, 'value').
|
198
|
-
returns(attr)
|
199
|
-
@factory.send(:name, 'value')
|
200
|
-
assert @factory.attributes.include?(attr)
|
201
|
-
end
|
202
|
-
|
203
|
-
context "when overriding generated attributes with a hash" do
|
204
|
-
setup do
|
205
|
-
@attr = :name
|
206
|
-
@value = 'The price is right!'
|
207
|
-
@hash = { @attr => @value }
|
208
|
-
end
|
209
|
-
|
210
|
-
should "return the overridden value in the generated attributes" do
|
211
|
-
@factory.add_attribute(@attr, 'The price is wrong, Bob!')
|
212
|
-
result = @factory.run(Factory::Proxy::AttributesFor, @hash)
|
213
|
-
assert_equal @value, result[@attr]
|
214
|
-
end
|
215
|
-
|
216
|
-
should "not call a lazy attribute block for an overridden attribute" do
|
217
|
-
@factory.add_attribute(@attr) { flunk }
|
218
|
-
result = @factory.run(Factory::Proxy::AttributesFor, @hash)
|
219
|
-
end
|
220
|
-
|
221
|
-
should "override a symbol parameter with a string parameter" do
|
222
|
-
@factory.add_attribute(@attr, 'The price is wrong, Bob!')
|
223
|
-
@hash = { @attr.to_s => @value }
|
224
|
-
result = @factory.run(Factory::Proxy::AttributesFor, @hash)
|
225
|
-
assert_equal @value, result[@attr]
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
context "overriding an attribute with an alias" do
|
230
|
-
setup do
|
231
|
-
@factory.add_attribute(:test, 'original')
|
232
|
-
Factory.alias(/(.*)_alias/, '\1')
|
233
|
-
@result = @factory.run(Factory::Proxy::AttributesFor,
|
234
|
-
:test_alias => 'new')
|
235
|
-
end
|
236
|
-
|
237
|
-
should "use the passed in value for the alias" do
|
238
|
-
assert_equal 'new', @result[:test_alias]
|
239
|
-
end
|
240
|
-
|
241
|
-
should "discard the predefined value for the attribute" do
|
242
|
-
assert_nil @result[:test]
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
should "guess the build class from the factory name" do
|
247
|
-
assert_equal User, @factory.build_class
|
248
|
-
end
|
249
|
-
|
250
|
-
context "when defined with a custom class" do
|
251
|
-
setup do
|
252
|
-
@class = User
|
253
|
-
@factory = Factory.new(:author, :class => @class)
|
254
|
-
end
|
255
|
-
|
256
|
-
should "use the specified class as the build class" do
|
257
|
-
assert_equal @class, @factory.build_class
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
context "when defined with a class instead of a name" do
|
262
|
-
setup do
|
263
|
-
@class = ArgumentError
|
264
|
-
@name = :argument_error
|
265
|
-
@factory = Factory.new(@class)
|
266
|
-
end
|
267
|
-
|
268
|
-
should "guess the name from the class" do
|
269
|
-
assert_equal @name, @factory.factory_name
|
270
|
-
end
|
271
|
-
|
272
|
-
should "use the class as the build class" do
|
273
|
-
assert_equal @class, @factory.build_class
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
context "when defined with a custom class name" do
|
278
|
-
setup do
|
279
|
-
@class = ArgumentError
|
280
|
-
@factory = Factory.new(:author, :class => :argument_error)
|
281
|
-
end
|
282
|
-
|
283
|
-
should "use the specified class as the build class" do
|
284
|
-
assert_equal @class, @factory.build_class
|
285
|
-
end
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
context "a factory with a name ending in s" do
|
290
|
-
setup do
|
291
|
-
@name = :business
|
292
|
-
@class = Business
|
293
|
-
@factory = Factory.new(@name)
|
294
|
-
end
|
295
|
-
|
296
|
-
should "have a factory name" do
|
297
|
-
assert_equal @name, @factory.factory_name
|
298
|
-
end
|
299
|
-
|
300
|
-
should "have a build class" do
|
301
|
-
assert_equal @class, @factory.build_class
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
|
-
context "a factory with a string for a name" do
|
306
|
-
setup do
|
307
|
-
@name = :user
|
308
|
-
@factory = Factory.new(@name.to_s) {}
|
309
|
-
end
|
310
|
-
|
311
|
-
should "convert the string to a symbol" do
|
312
|
-
assert_equal @name, @factory.factory_name
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
|
-
context "a factory defined with a string name" do
|
317
|
-
setup do
|
318
|
-
Factory.factories = {}
|
319
|
-
@name = :user
|
320
|
-
@factory = Factory.define(@name.to_s) {}
|
321
|
-
end
|
322
|
-
|
323
|
-
should "store the factory using a symbol" do
|
324
|
-
assert_equal @factory, Factory.factories[@name]
|
325
|
-
end
|
326
|
-
end
|
327
|
-
|
328
|
-
context "after defining a factory" do
|
329
|
-
setup do
|
330
|
-
@name = :user
|
331
|
-
@factory = mock('factory')
|
332
|
-
|
333
|
-
Factory.factories[@name] = @factory
|
334
|
-
end
|
335
|
-
|
336
|
-
teardown { Factory.factories.clear }
|
337
|
-
|
338
|
-
should "use Proxy::AttributesFor for Factory.attributes_for" do
|
339
|
-
@factory.
|
340
|
-
expects(:run).
|
341
|
-
with(Factory::Proxy::AttributesFor, :attr => 'value').
|
342
|
-
returns('result')
|
343
|
-
assert_equal 'result', Factory.attributes_for(@name, :attr => 'value')
|
344
|
-
end
|
345
|
-
|
346
|
-
should "use Proxy::Build for Factory.build" do
|
347
|
-
@factory.
|
348
|
-
expects(:run).
|
349
|
-
with(Factory::Proxy::Build, :attr => 'value').
|
350
|
-
returns('result')
|
351
|
-
assert_equal 'result', Factory.build(@name, :attr => 'value')
|
352
|
-
end
|
353
|
-
|
354
|
-
should "use Proxy::Create for Factory.create" do
|
355
|
-
@factory.
|
356
|
-
expects(:run).
|
357
|
-
with(Factory::Proxy::Create, :attr => 'value').
|
358
|
-
returns('result')
|
359
|
-
assert_equal 'result', Factory.create(@name, :attr => 'value')
|
360
|
-
end
|
361
|
-
|
362
|
-
should "use Proxy::Stub for Factory.stub" do
|
363
|
-
@factory.
|
364
|
-
expects(:run).
|
365
|
-
with(Factory::Proxy::Stub, :attr => 'value').
|
366
|
-
returns('result')
|
367
|
-
assert_equal 'result', Factory.stub(@name, :attr => 'value')
|
368
|
-
end
|
369
|
-
|
370
|
-
should "use default strategy option as Factory.default_strategy" do
|
371
|
-
@factory.stubs(:default_strategy).returns(:create)
|
372
|
-
@factory.
|
373
|
-
expects(:run).
|
374
|
-
with(Factory::Proxy::Create, :attr => 'value').
|
375
|
-
returns('result')
|
376
|
-
assert_equal 'result', Factory.default_strategy(@name, :attr => 'value')
|
377
|
-
end
|
378
|
-
|
379
|
-
should "use the default strategy for the global Factory method" do
|
380
|
-
@factory.stubs(:default_strategy).returns(:create)
|
381
|
-
@factory.
|
382
|
-
expects(:run).
|
383
|
-
with(Factory::Proxy::Create, :attr => 'value').
|
384
|
-
returns('result')
|
385
|
-
assert_equal 'result', Factory(@name, :attr => 'value')
|
386
|
-
end
|
387
|
-
|
388
|
-
[:build, :create, :attributes_for, :stub].each do |method|
|
389
|
-
should "raise an ArgumentError on #{method} with a nonexistant factory" do
|
390
|
-
assert_raise(ArgumentError) { Factory.send(method, :bogus) }
|
391
|
-
end
|
392
|
-
|
393
|
-
should "recognize either 'name' or :name for Factory.#{method}" do
|
394
|
-
@factory.stubs(:run)
|
395
|
-
assert_nothing_raised { Factory.send(method, @name.to_s) }
|
396
|
-
assert_nothing_raised { Factory.send(method, @name.to_sym) }
|
397
|
-
end
|
398
|
-
end
|
399
|
-
end
|
400
|
-
|
401
|
-
context 'defining a factory with a parent parameter' do
|
402
|
-
setup do
|
403
|
-
@parent = Factory.define :object do |f|
|
404
|
-
f.name 'Name'
|
405
|
-
end
|
406
|
-
end
|
407
|
-
|
408
|
-
should 'raise an ArgumentError when trying to use a non-existent factory as parent' do
|
409
|
-
assert_raise(ArgumentError) { Factory.define(:child, :parent => :nonexsitent) {} }
|
410
|
-
end
|
411
|
-
|
412
|
-
should 'create a new factory using the class of the parent' do
|
413
|
-
child = Factory.define(:child, :parent => :object) {}
|
414
|
-
assert_equal @parent.build_class, child.build_class
|
415
|
-
end
|
416
|
-
|
417
|
-
should 'create a new factory while overriding the parent class' do
|
418
|
-
class Other; end
|
419
|
-
|
420
|
-
child = Factory.define(:child, :parent => :object, :class => Other) {}
|
421
|
-
assert_equal Other, child.build_class
|
422
|
-
end
|
423
|
-
|
424
|
-
should 'create a new factory with attributes of the parent' do
|
425
|
-
child = Factory.define(:child, :parent => :object) {}
|
426
|
-
assert_equal 1, child.attributes.size
|
427
|
-
assert_equal :name, child.attributes.first.name
|
428
|
-
end
|
429
|
-
|
430
|
-
should 'allow to define additional attributes' do
|
431
|
-
child = Factory.define(:child, :parent => :object) do |f|
|
432
|
-
f.email 'person@somebody.com'
|
433
|
-
end
|
434
|
-
assert_equal 2, child.attributes.size
|
435
|
-
end
|
436
|
-
|
437
|
-
should 'allow to override parent attributes' do
|
438
|
-
child = Factory.define(:child, :parent => :object) do |f|
|
439
|
-
f.name { 'Child Name' }
|
440
|
-
end
|
441
|
-
assert_equal 1, child.attributes.size
|
442
|
-
assert_kind_of Factory::Attribute::Dynamic, child.attributes.first
|
443
|
-
end
|
444
|
-
end
|
445
|
-
|
446
|
-
context 'defining a factory with a default strategy parameter' do
|
447
|
-
should 'raise an ArgumentError when trying to use a non-existent factory' do
|
448
|
-
assert_raise(ArgumentError) { Factory.define(:object, :default_strategy => :nonexistent) {} }
|
449
|
-
end
|
450
|
-
|
451
|
-
should 'create a new factory with a specified default strategy' do
|
452
|
-
factory = Factory.define(:object, :default_strategy => :stub) {}
|
453
|
-
assert_equal :stub, factory.default_strategy
|
454
|
-
end
|
455
|
-
end
|
456
|
-
|
457
|
-
def self.context_in_directory_with_files(*files)
|
458
|
-
context "in a directory with #{files.to_sentence}" do
|
459
|
-
setup do
|
460
|
-
@pwd = Dir.pwd
|
461
|
-
@tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
|
462
|
-
FileUtils.mkdir_p @tmp_dir
|
463
|
-
Dir.chdir(@tmp_dir)
|
464
|
-
|
465
|
-
files.each do |file|
|
466
|
-
FileUtils.mkdir_p File.dirname(file)
|
467
|
-
FileUtils.touch file
|
468
|
-
Factory.stubs(:require).with(file)
|
469
|
-
end
|
470
|
-
end
|
471
|
-
|
472
|
-
teardown do
|
473
|
-
Dir.chdir(@pwd)
|
474
|
-
FileUtils.rm_rf(@tmp_dir)
|
475
|
-
end
|
476
|
-
|
477
|
-
yield
|
478
|
-
end
|
479
|
-
end
|
480
|
-
|
481
|
-
def self.should_require_definitions_from(file)
|
482
|
-
should "load definitions from #{file}" do
|
483
|
-
Factory.expects(:require).with(file)
|
484
|
-
Factory.find_definitions
|
485
|
-
end
|
486
|
-
end
|
487
|
-
|
488
|
-
context_in_directory_with_files 'factories.rb' do
|
489
|
-
should_require_definitions_from 'factories.rb'
|
490
|
-
end
|
491
|
-
|
492
|
-
%w(spec test).each do |dir|
|
493
|
-
context_in_directory_with_files File.join(dir, 'factories.rb') do
|
494
|
-
should_require_definitions_from "#{dir}/factories.rb"
|
495
|
-
end
|
496
|
-
|
497
|
-
context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb') do
|
498
|
-
should_require_definitions_from "#{dir}/factories/post_factory.rb"
|
499
|
-
end
|
500
|
-
|
501
|
-
context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do
|
502
|
-
should_require_definitions_from "#{dir}/factories/post_factory.rb"
|
503
|
-
should_require_definitions_from "#{dir}/factories/person_factory.rb"
|
504
|
-
end
|
505
|
-
|
506
|
-
context_in_directory_with_files File.join(dir, 'factories.rb'), File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do
|
507
|
-
should_require_definitions_from "#{dir}/factories.rb"
|
508
|
-
should_require_definitions_from "#{dir}/factories/post_factory.rb"
|
509
|
-
should_require_definitions_from "#{dir}/factories/person_factory.rb"
|
510
|
-
end
|
511
|
-
end
|
512
|
-
|
513
|
-
end
|