factory_girl 1.3.3 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +68 -60
- data/features/support/test.db +0 -0
- data/lib/factory_girl.rb +6 -12
- data/lib/factory_girl/aliases.rb +2 -31
- data/lib/factory_girl/attribute.rb +1 -1
- data/lib/factory_girl/attribute/association.rb +1 -1
- data/lib/factory_girl/attribute/callback.rb +1 -1
- data/lib/factory_girl/attribute/dynamic.rb +3 -3
- data/lib/factory_girl/attribute/static.rb +1 -1
- data/lib/factory_girl/definition_proxy.rb +180 -0
- data/lib/factory_girl/deprecated.rb +18 -0
- data/lib/factory_girl/factory.rb +120 -355
- data/lib/factory_girl/find_definitions.rb +25 -0
- data/lib/factory_girl/proxy.rb +4 -6
- data/lib/factory_girl/proxy/attributes_for.rb +1 -1
- data/lib/factory_girl/proxy/build.rb +7 -5
- data/lib/factory_girl/proxy/create.rb +1 -1
- data/lib/factory_girl/proxy/stub.rb +11 -5
- data/lib/factory_girl/rails2.rb +1 -1
- data/lib/factory_girl/sequence.rb +5 -40
- data/lib/factory_girl/step_definitions.rb +7 -7
- data/lib/factory_girl/syntax.rb +7 -7
- data/lib/factory_girl/syntax/blueprint.rb +5 -4
- data/lib/factory_girl/syntax/default.rb +31 -0
- data/lib/factory_girl/syntax/generate.rb +13 -8
- data/lib/factory_girl/syntax/make.rb +8 -6
- data/lib/factory_girl/syntax/sham.rb +11 -8
- data/lib/factory_girl/syntax/vintage.rb +196 -0
- data/lib/factory_girl/version.rb +4 -0
- data/spec/acceptance/acceptance_spec.rb +43 -60
- data/spec/acceptance/syntax/blueprint_spec.rb +1 -5
- data/spec/acceptance/syntax/generate_spec.rb +1 -4
- data/spec/acceptance/syntax/make_spec.rb +1 -4
- data/spec/acceptance/syntax/sham_spec.rb +9 -7
- data/spec/acceptance/syntax/vintage_spec.rb +184 -0
- data/spec/factory_girl/aliases_spec.rb +5 -5
- data/spec/factory_girl/attribute/association_spec.rb +3 -3
- data/spec/factory_girl/attribute/callback_spec.rb +3 -3
- data/spec/factory_girl/attribute/dynamic_spec.rb +20 -9
- data/spec/factory_girl/attribute/static_spec.rb +5 -5
- data/spec/factory_girl/attribute_spec.rb +5 -5
- data/spec/factory_girl/definition_proxy_spec.rb +138 -0
- data/spec/factory_girl/deprecated_spec.rb +66 -0
- data/spec/factory_girl/factory_spec.rb +283 -566
- data/spec/factory_girl/find_definitions_spec.rb +89 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +2 -2
- data/spec/factory_girl/proxy/build_spec.rb +17 -12
- data/spec/factory_girl/proxy/create_spec.rb +17 -12
- data/spec/factory_girl/proxy/stub_spec.rb +6 -5
- data/spec/factory_girl/proxy_spec.rb +2 -2
- data/spec/factory_girl/sequence_spec.rb +15 -38
- data/spec/spec_helper.rb +4 -0
- metadata +28 -11
@@ -1,24 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
2
3
|
|
3
4
|
require 'factory_girl/syntax/sham'
|
4
5
|
|
5
6
|
describe "a factory using sham syntax" do
|
6
7
|
before do
|
7
|
-
Sham.name
|
8
|
-
Sham.email
|
8
|
+
Sham.name { "Name" }
|
9
|
+
Sham.email { "somebody#{rand(5)}@example.com" }
|
10
|
+
Sham.user("FOO") { |c| "User-#{c}" }
|
9
11
|
|
10
12
|
Factory.define :user do |factory|
|
11
13
|
factory.first_name { Sham.name }
|
12
14
|
factory.last_name { Sham.name }
|
13
15
|
factory.email { Sham.email }
|
16
|
+
factory.username { Sham.user }
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
17
|
-
after do
|
18
|
-
Factory.factories.clear
|
19
|
-
Factory.sequences.clear
|
20
|
-
end
|
21
|
-
|
22
20
|
describe "after making an instance" do
|
23
21
|
before do
|
24
22
|
@instance = Factory(:user, :last_name => 'Rye')
|
@@ -28,6 +26,10 @@ describe "a factory using sham syntax" do
|
|
28
26
|
@instance.first_name.should == 'Name'
|
29
27
|
end
|
30
28
|
|
29
|
+
it "should support shams with starting values" do
|
30
|
+
@instance.username.should == 'User-FOO'
|
31
|
+
end
|
32
|
+
|
31
33
|
it "should use the sham for the email" do
|
32
34
|
@instance.email.should =~ /somebody\d@example.com/
|
33
35
|
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "default syntax" do
|
5
|
+
before do
|
6
|
+
Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
|
7
|
+
Factory.define :user do |factory|
|
8
|
+
factory.first_name { 'Bill' }
|
9
|
+
factory.last_name { 'Nye' }
|
10
|
+
factory.email { Factory.next(:email) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "after making an instance" do
|
15
|
+
before do
|
16
|
+
@instance = Factory(:user, :last_name => 'Rye')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should use attributes from the definition" do
|
20
|
+
@instance.first_name.should == 'Bill'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should evaluate attribute blocks for each instance" do
|
24
|
+
@instance.email.should =~ /somebody\d+@example.com/
|
25
|
+
Factory(:user).email.should_not == @instance.email
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise an ArgumentError when trying to use a non-existent strategy" do
|
30
|
+
lambda {
|
31
|
+
Factory.define(:object, :default_strategy => :nonexistent) {}
|
32
|
+
}.should raise_error(ArgumentError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe Factory, "given a parent factory" do
|
37
|
+
before do
|
38
|
+
@parent = FactoryGirl::Factory.new(:object)
|
39
|
+
@parent.define_attribute(FactoryGirl::Attribute::Static.new(:name, 'value'))
|
40
|
+
FactoryGirl.register_factory(@parent)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
|
44
|
+
lambda {
|
45
|
+
Factory.define(:child, :parent => :nonexsitent) {}
|
46
|
+
}.should raise_error(ArgumentError)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "defining a factory" do
|
51
|
+
before do
|
52
|
+
@name = :user
|
53
|
+
@factory = "factory"
|
54
|
+
@proxy = "proxy"
|
55
|
+
stub(@factory).name { @name }
|
56
|
+
@options = { :class => 'magic' }
|
57
|
+
stub(FactoryGirl::Factory).new { @factory }
|
58
|
+
stub(FactoryGirl::DefinitionProxy).new { @proxy }
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should create a new factory using the specified name and options" do
|
62
|
+
mock(FactoryGirl::Factory).new(@name, @options) { @factory }
|
63
|
+
Factory.define(@name, @options) {|f| }
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should pass the factory do the block" do
|
67
|
+
yielded = nil
|
68
|
+
Factory.define(@name) do |y|
|
69
|
+
yielded = y
|
70
|
+
end
|
71
|
+
yielded.should == @proxy
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should add the factory to the list of factories" do
|
75
|
+
Factory.define(@name) {|f| }
|
76
|
+
@factory.should == FactoryGirl.factories[@name]
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should allow a factory to be found by name" do
|
80
|
+
Factory.define(@name) {|f| }
|
81
|
+
FactoryGirl.factory_by_name(@name).should == @factory
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "after defining a factory" do
|
86
|
+
before do
|
87
|
+
@name = :user
|
88
|
+
@factory = "factory"
|
89
|
+
|
90
|
+
FactoryGirl.factories[@name] = @factory
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should use Proxy::AttributesFor for Factory.attributes_for" do
|
94
|
+
mock(@factory).run(FactoryGirl::Proxy::AttributesFor, :attr => 'value') { 'result' }
|
95
|
+
Factory.attributes_for(@name, :attr => 'value').should == 'result'
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should use Proxy::Build for Factory.build" do
|
99
|
+
mock(@factory).run(FactoryGirl::Proxy::Build, :attr => 'value') { 'result' }
|
100
|
+
Factory.build(@name, :attr => 'value').should == 'result'
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should use Proxy::Create for Factory.create" do
|
104
|
+
mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
|
105
|
+
Factory.create(@name, :attr => 'value').should == 'result'
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should use Proxy::Stub for Factory.stub" do
|
109
|
+
mock(@factory).run(FactoryGirl::Proxy::Stub, :attr => 'value') { 'result' }
|
110
|
+
Factory.stub(@name, :attr => 'value').should == 'result'
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should use default strategy option as Factory.default_strategy" do
|
114
|
+
stub(@factory).default_strategy { :create }
|
115
|
+
mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
|
116
|
+
Factory.default_strategy(@name, :attr => 'value').should == 'result'
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should use the default strategy for the global Factory method" do
|
120
|
+
stub(@factory).default_strategy { :create }
|
121
|
+
mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
|
122
|
+
Factory(@name, :attr => 'value').should == 'result'
|
123
|
+
end
|
124
|
+
|
125
|
+
[:build, :create, :attributes_for, :stub].each do |method|
|
126
|
+
it "should raise an ArgumentError on #{method} with a nonexistant factory" do
|
127
|
+
lambda { Factory.send(method, :bogus) }.should raise_error(ArgumentError)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should recognize either 'name' or :name for Factory.#{method}" do
|
131
|
+
stub(@factory).run
|
132
|
+
lambda { Factory.send(method, @name.to_s) }.should_not raise_error
|
133
|
+
lambda { Factory.send(method, @name.to_sym) }.should_not raise_error
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "defining a sequence" do
|
139
|
+
before do
|
140
|
+
@sequence = "sequence"
|
141
|
+
@name = :count
|
142
|
+
stub(FactoryGirl::Sequence).new { @sequence }
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should create a new sequence" do
|
146
|
+
mock(FactoryGirl::Sequence).new(1) { @sequence }
|
147
|
+
Factory.sequence(@name)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should use the supplied block as the sequence generator" do
|
151
|
+
stub(FactoryGirl::Sequence).new.yields(1)
|
152
|
+
yielded = false
|
153
|
+
Factory.sequence(@name) {|n| yielded = true }
|
154
|
+
(yielded).should be
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should use the supplied start_value as the sequence start_value" do
|
158
|
+
mock(FactoryGirl::Sequence).new("A") { @sequence }
|
159
|
+
Factory.sequence(@name, "A")
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "after defining a sequence" do
|
164
|
+
before do
|
165
|
+
@sequence = "sequence"
|
166
|
+
@name = :test
|
167
|
+
@value = '1 2 5'
|
168
|
+
|
169
|
+
stub(@sequence).next { @value }
|
170
|
+
stub(FactoryGirl::Sequence).new { @sequence }
|
171
|
+
|
172
|
+
Factory.sequence(@name) {}
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should call next on the sequence when sent next" do
|
176
|
+
mock(@sequence).next
|
177
|
+
|
178
|
+
Factory.next(@name)
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should return the value from the sequence" do
|
182
|
+
Factory.next(@name).should == @value
|
183
|
+
end
|
184
|
+
end
|
@@ -3,19 +3,19 @@ require 'spec_helper'
|
|
3
3
|
describe Factory, "aliases" do
|
4
4
|
|
5
5
|
it "should include an attribute as an alias for itself by default" do
|
6
|
-
|
6
|
+
FactoryGirl.aliases_for(:test).should include(:test)
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should include the root of a foreign key as an alias by default" do
|
10
|
-
|
10
|
+
FactoryGirl.aliases_for(:test_id).should include(:test)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should include an attribute's foreign key as an alias by default" do
|
14
|
-
|
14
|
+
FactoryGirl.aliases_for(:test).should include(:test_id)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should NOT include an attribute as an alias when it starts with underscore" do
|
18
|
-
|
18
|
+
FactoryGirl.aliases_for(:_id).should_not include(:id)
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "after adding an alias" do
|
@@ -25,7 +25,7 @@ describe Factory, "aliases" do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should return the alias in the aliases list" do
|
28
|
-
|
28
|
+
FactoryGirl.aliases_for(:test_suffix).should include(:test)
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Attribute::Association do
|
4
4
|
before do
|
5
5
|
@name = :author
|
6
6
|
@factory = :user
|
7
7
|
@overrides = { :first_name => 'John' }
|
8
|
-
@attr =
|
8
|
+
@attr = FactoryGirl::Attribute::Association.new(@name, @factory, @overrides)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should have a name" do
|
@@ -24,6 +24,6 @@ describe Factory::Attribute::Association do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should convert names to symbols" do
|
27
|
-
|
27
|
+
FactoryGirl::Attribute::Association.new('name', :user, {}).name.should == :name
|
28
28
|
end
|
29
29
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Attribute::Callback do
|
4
4
|
before do
|
5
5
|
@name = :after_create
|
6
6
|
@block = proc{ 'block' }
|
7
|
-
@attr =
|
7
|
+
@attr = FactoryGirl::Attribute::Callback.new(@name, @block)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should have a name" do
|
@@ -18,6 +18,6 @@ describe Factory::Attribute::Callback do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should convert names to symbols" do
|
21
|
-
|
21
|
+
FactoryGirl::Attribute::Callback.new('name', nil).name.should == :name
|
22
22
|
end
|
23
23
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Attribute::Dynamic do
|
4
4
|
before do
|
5
5
|
@name = :first_name
|
6
6
|
@block = lambda { 'value' }
|
7
|
-
@attr =
|
7
|
+
@attr = FactoryGirl::Attribute::Dynamic.new(@name, @block)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should have a name" do
|
@@ -20,30 +20,41 @@ describe Factory::Attribute::Dynamic do
|
|
20
20
|
|
21
21
|
it "should yield the proxy to the block when adding its value to a proxy" do
|
22
22
|
@block = lambda {|a| a }
|
23
|
-
@attr =
|
23
|
+
@attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
|
24
24
|
@proxy = "proxy"
|
25
25
|
stub(@proxy).set
|
26
26
|
@attr.add_to(@proxy)
|
27
27
|
@proxy.should have_received.set(:user, @proxy)
|
28
28
|
end
|
29
29
|
|
30
|
+
it "evaluates the block with in the context of the proxy without an argument" do
|
31
|
+
result = 'other attribute value'
|
32
|
+
@block = lambda { other_attribute }
|
33
|
+
@attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
|
34
|
+
@proxy = "proxy"
|
35
|
+
stub(@proxy).set
|
36
|
+
stub(@proxy).other_attribute { result }
|
37
|
+
@attr.add_to(@proxy)
|
38
|
+
@proxy.should have_received.set(:user, result)
|
39
|
+
end
|
40
|
+
|
30
41
|
it "should raise an error when defining an attribute writer" do
|
31
42
|
lambda {
|
32
|
-
|
33
|
-
}.should raise_error(
|
43
|
+
FactoryGirl::Attribute::Dynamic.new('test=', nil)
|
44
|
+
}.should raise_error(FactoryGirl::AttributeDefinitionError)
|
34
45
|
end
|
35
46
|
|
36
47
|
it "should raise an error when returning a sequence" do
|
37
|
-
stub(Factory).sequence {
|
48
|
+
stub(Factory).sequence { FactoryGirl::Sequence.new }
|
38
49
|
block = lambda { Factory.sequence(:email) }
|
39
|
-
attr =
|
50
|
+
attr = FactoryGirl::Attribute::Dynamic.new(:email, block)
|
40
51
|
proxy = stub!.set.subject
|
41
52
|
lambda {
|
42
53
|
attr.add_to(proxy)
|
43
|
-
}.should raise_error(
|
54
|
+
}.should raise_error(FactoryGirl::SequenceAbuseError)
|
44
55
|
end
|
45
56
|
|
46
57
|
it "should convert names to symbols" do
|
47
|
-
|
58
|
+
FactoryGirl::Attribute::Dynamic.new('name', nil).name.should == :name
|
48
59
|
end
|
49
60
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Attribute::Static do
|
4
4
|
before do
|
5
5
|
@name = :first_name
|
6
6
|
@value = 'John'
|
7
|
-
@attr =
|
7
|
+
@attr = FactoryGirl::Attribute::Static.new(@name, @value)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should have a name" do
|
@@ -19,11 +19,11 @@ describe Factory::Attribute::Static do
|
|
19
19
|
|
20
20
|
it "should raise an error when defining an attribute writer" do
|
21
21
|
lambda {
|
22
|
-
|
23
|
-
}.should raise_error(
|
22
|
+
FactoryGirl::Attribute::Static.new('test=', nil)
|
23
|
+
}.should raise_error(FactoryGirl::AttributeDefinitionError)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should convert names to symbols" do
|
27
|
-
|
27
|
+
FactoryGirl::Attribute::Static.new('name', nil).name.should == :name
|
28
28
|
end
|
29
29
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe FactoryGirl::Attribute do
|
4
4
|
before do
|
5
5
|
@name = :user
|
6
|
-
@attr =
|
6
|
+
@attr = FactoryGirl::Attribute.new(@name)
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should have a name" do
|
@@ -20,11 +20,11 @@ describe Factory::Attribute do
|
|
20
20
|
it "should raise an error when defining an attribute writer" do
|
21
21
|
error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
|
22
22
|
lambda {
|
23
|
-
|
24
|
-
}.should raise_error(
|
23
|
+
FactoryGirl::Attribute.new('test=')
|
24
|
+
}.should raise_error(FactoryGirl::AttributeDefinitionError, error_message)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should convert names to symbols" do
|
28
|
-
|
28
|
+
FactoryGirl::Attribute.new('name').name.should == :name
|
29
29
|
end
|
30
30
|
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FactoryGirl::DefinitionProxy do
|
4
|
+
let(:factory) { FactoryGirl::Factory.new(:object) }
|
5
|
+
subject { FactoryGirl::DefinitionProxy.new(factory) }
|
6
|
+
|
7
|
+
it "should add a static attribute for type" do
|
8
|
+
subject.type 'value'
|
9
|
+
factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should add a static attribute for id" do
|
13
|
+
subject.id 'value'
|
14
|
+
factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should add a static attribute when an attribute is defined with a value" do
|
18
|
+
attribute = 'attribute'
|
19
|
+
stub(attribute).name { :name }
|
20
|
+
mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
|
21
|
+
mock(factory).define_attribute(attribute)
|
22
|
+
subject.add_attribute(:name, 'value')
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should add a dynamic attribute when an attribute is defined with a block" do
|
26
|
+
attribute = 'attribute'
|
27
|
+
stub(attribute).name { :name }
|
28
|
+
block = lambda {}
|
29
|
+
mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
|
30
|
+
mock(factory).define_attribute(attribute)
|
31
|
+
subject.add_attribute(:name, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should raise for an attribute with a value and a block" do
|
35
|
+
lambda {
|
36
|
+
subject.add_attribute(:name, 'value') {}
|
37
|
+
}.should raise_error(FactoryGirl::AttributeDefinitionError)
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "adding an attribute using a in-line sequence" do
|
41
|
+
it "should create the sequence" do
|
42
|
+
mock(FactoryGirl::Sequence).new(1)
|
43
|
+
subject.sequence(:name) {}
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should create the sequence with a custom default value" do
|
47
|
+
mock(FactoryGirl::Sequence).new("A")
|
48
|
+
subject.sequence(:name, "A") {}
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should add a dynamic attribute" do
|
52
|
+
attribute = 'attribute'
|
53
|
+
stub(attribute).name { :name }
|
54
|
+
mock(FactoryGirl::Attribute::Dynamic).new(:name, is_a(Proc)) { attribute }
|
55
|
+
subject.sequence(:name) {}
|
56
|
+
factory.attributes.should include(attribute)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should add a callback attribute when the after_build attribute is defined" do
|
61
|
+
mock(FactoryGirl::Attribute::Callback).new(:after_build, is_a(Proc)) { 'after_build callback' }
|
62
|
+
subject.after_build {}
|
63
|
+
factory.attributes.should include('after_build callback')
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should add a callback attribute when the after_create attribute is defined" do
|
67
|
+
mock(FactoryGirl::Attribute::Callback).new(:after_create, is_a(Proc)) { 'after_create callback' }
|
68
|
+
subject.after_create {}
|
69
|
+
factory.attributes.should include('after_create callback')
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should add a callback attribute when the after_stub attribute is defined" do
|
73
|
+
mock(FactoryGirl::Attribute::Callback).new(:after_stub, is_a(Proc)) { 'after_stub callback' }
|
74
|
+
subject.after_stub {}
|
75
|
+
factory.attributes.should include('after_stub callback')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should add an association without a factory name or overrides" do
|
79
|
+
name = :user
|
80
|
+
attr = 'attribute'
|
81
|
+
stub(attr).name { name }
|
82
|
+
mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
|
83
|
+
subject.association(name)
|
84
|
+
factory.attributes.should include(attr)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should add an association with overrides" do
|
88
|
+
name = :user
|
89
|
+
attr = 'attribute'
|
90
|
+
overrides = { :first_name => 'Ben' }
|
91
|
+
stub(attr).name { name }
|
92
|
+
mock(FactoryGirl::Attribute::Association).new(name, name, overrides) { attr }
|
93
|
+
subject.association(name, overrides)
|
94
|
+
factory.attributes.should include(attr)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should add an attribute using the method name when passed an undefined method" do
|
98
|
+
attribute = 'attribute'
|
99
|
+
stub(attribute).name { :name }
|
100
|
+
mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
|
101
|
+
subject.send(:name, 'value')
|
102
|
+
factory.attributes.should include(attribute)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "adds an attribute using when passed an undefined method and block" do
|
106
|
+
attribute = 'attribute'
|
107
|
+
stub(attribute).name { :name }
|
108
|
+
block = lambda {}
|
109
|
+
mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
|
110
|
+
subject.send(:name, &block)
|
111
|
+
factory.attributes.should include(attribute)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "adds an association when passed an undefined method without arguments or a block" do
|
115
|
+
name = :user
|
116
|
+
attr = 'attribute'
|
117
|
+
stub(attr).name { name }
|
118
|
+
mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
|
119
|
+
subject.send(name)
|
120
|
+
factory.attributes.should include(attr)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "adds a sequence when passed an undefined method without arguments or a block" do
|
124
|
+
name = :airport
|
125
|
+
proxy = 'proxy'
|
126
|
+
FactoryGirl.sequences[name] = FactoryGirl::Sequence.new { |value| "expected" }
|
127
|
+
subject.send(name)
|
128
|
+
stub(proxy).set
|
129
|
+
factory.attributes.last.add_to(proxy)
|
130
|
+
proxy.should have_received.set(name, 'expected')
|
131
|
+
end
|
132
|
+
|
133
|
+
it "registers its factory for an alias" do
|
134
|
+
aliased_name = :guest
|
135
|
+
mock(FactoryGirl).register_factory(factory, :as => aliased_name)
|
136
|
+
subject.aliased_as aliased_name
|
137
|
+
end
|
138
|
+
end
|