factory_girl 3.6.2 → 4.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'factory_girl/syntax/blueprint'
4
-
5
- describe "a blueprint" do
6
- before do
7
- ActiveSupport::Deprecation.silenced = true
8
-
9
- define_model('User', first_name: :string, last_name: :string, email: :string)
10
-
11
- FactoryGirl.define do
12
- sequence(:email) { |n| "somebody#{n}@example.com" }
13
- end
14
-
15
- User.blueprint do
16
- first_name { 'Bill' }
17
- last_name { 'Nye' }
18
- email { FactoryGirl.generate(:email) }
19
- end
20
- end
21
-
22
- describe "after making an instance" do
23
- before do
24
- @instance = FactoryGirl.create(:user, last_name: 'Rye')
25
- end
26
-
27
- it "uses attributes from the blueprint" do
28
- @instance.first_name.should == 'Bill'
29
- end
30
-
31
- it "evaluates attribute blocks for each instance" do
32
- @instance.email.should =~ /somebody\d+@example.com/
33
- FactoryGirl.create(:user).email.should_not == @instance.email
34
- end
35
- end
36
- end
@@ -1,61 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'factory_girl/syntax/generate'
4
-
5
- describe "a factory using generate syntax" do
6
- before do
7
- ActiveSupport::Deprecation.silenced = true
8
-
9
- define_model('User', first_name: :string, last_name: :string, email: :string) do
10
- validates_presence_of :first_name
11
- end
12
-
13
- FactoryGirl.define do
14
- factory :user do
15
- first_name 'Bill'
16
- last_name 'Nye'
17
- email 'science@guys.net'
18
- end
19
- end
20
- end
21
-
22
- it "does not raise an error when generating an invalid instance" do
23
- expect { User.generate(first_name: nil) }.to_not raise_error
24
- end
25
-
26
- it "raises an error when forcefully generating an invalid instance" do
27
- expect { User.generate!(first_name: nil) }.to raise_error(ActiveRecord::RecordInvalid)
28
- end
29
-
30
- %w(generate generate! spawn).each do |method|
31
- it "yields a generated instance when using #{method} with a block" do
32
- saved_instance = nil
33
- User.send(method) {|instance| saved_instance = instance }
34
- saved_instance.should be_kind_of(User)
35
- end
36
-
37
- describe "after generating an instance using #{method}" do
38
- before do
39
- @instance = User.send(method, last_name: 'Rye')
40
- end
41
-
42
- it "uses attributes from the factory" do
43
- @instance.first_name.should == 'Bill'
44
- end
45
-
46
- it "uses attributes passed to generate" do
47
- @instance.last_name.should == 'Rye'
48
- end
49
-
50
- if method == 'spawn'
51
- it "does not save the record" do
52
- @instance.should be_new_record
53
- end
54
- else
55
- it "does save the record" do
56
- @instance.should_not be_new_record
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'factory_girl/syntax/make'
4
-
5
- describe "a factory using make syntax" do
6
- before do
7
- ActiveSupport::Deprecation.silenced = true
8
-
9
- define_model('User', first_name: :string, last_name: :string)
10
-
11
- FactoryGirl.define do
12
- factory :user do
13
- first_name 'Bill'
14
- last_name 'Nye'
15
- end
16
- end
17
- end
18
-
19
- describe "after make" do
20
- before do
21
- @instance = User.make(last_name: 'Rye')
22
- end
23
-
24
- it "uses attributes from the factory" do
25
- @instance.first_name.should == 'Bill'
26
- end
27
-
28
- it "uses attributes passed to make" do
29
- @instance.last_name.should == 'Rye'
30
- end
31
-
32
- it "builds the record" do
33
- @instance.should be_new_record
34
- end
35
- end
36
-
37
- describe "after make!" do
38
- before do
39
- @instance = User.make!(last_name: 'Rye')
40
- end
41
-
42
- it "uses attributes from the factory" do
43
- @instance.first_name.should == 'Bill'
44
- end
45
-
46
- it "uses attributes passed to make" do
47
- @instance.last_name.should == 'Rye'
48
- end
49
-
50
- it "saves the record" do
51
- @instance.should_not be_new_record
52
- end
53
- end
54
- end
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'factory_girl/syntax/sham'
4
-
5
- describe "a factory using sham syntax" do
6
- before do
7
- define_model('User', first_name: :string,
8
- last_name: :string,
9
- email: :string,
10
- username: :string)
11
-
12
- Sham.name { "Name" }
13
- Sham.email { "somebody#{rand(5)}@example.com" }
14
- Sham.username("FOO") { |c| "User-#{c}" }
15
-
16
- FactoryGirl.define do
17
- factory :user do
18
- first_name { Sham.name }
19
- last_name { Sham.name }
20
- email { Sham.email }
21
- username { Sham.username }
22
- end
23
- end
24
- end
25
-
26
- describe "after making an instance" do
27
- before do
28
- @instance = FactoryGirl.create(:user, last_name: 'Rye')
29
- end
30
-
31
- it "supports a sham called 'name'" do
32
- @instance.first_name.should == 'Name'
33
- end
34
-
35
- it "supports shams with starting values" do
36
- @instance.username.should == 'User-FOO'
37
- end
38
-
39
- it "uses the sham for the email" do
40
- @instance.email.should =~ /somebody\d@example.com/
41
- end
42
- end
43
- end
@@ -1,219 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "vintage syntax" do
4
- before do
5
- ActiveSupport::Deprecation.silenced = true
6
-
7
- define_model('User', first_name: :string,
8
- last_name: :string,
9
- email: :string)
10
-
11
- Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
12
- Factory.define :user do |factory|
13
- factory.first_name { 'Bill' }
14
- factory.last_name { 'Nye' }
15
- factory.email { Factory.next(:email) }
16
- end
17
- end
18
-
19
- describe "after making an instance" do
20
- before do
21
- @instance = Factory(:user, last_name: 'Rye')
22
- end
23
-
24
- it "should use attributes from the definition" do
25
- @instance.first_name.should == 'Bill'
26
- end
27
-
28
- it "should evaluate attribute blocks for each instance" do
29
- @instance.email.should =~ /somebody\d+@example.com/
30
- Factory(:user).email.should_not == @instance.email
31
- end
32
- end
33
-
34
- it "raises Factory::SequenceAbuseError" do
35
- Factory.define :sequence_abuser, class: User do |factory|
36
- factory.first_name { Factory.sequence(:name) }
37
- end
38
-
39
- expect {
40
- Factory(:sequence_abuser)
41
- }.to raise_error(FactoryGirl::SequenceAbuseError)
42
- end
43
- end
44
-
45
- describe Factory, "referencing a nonexistent factory as a parent" do
46
- it "raises an ArgumentError when trying to use a non-existent factory as parent" do
47
- expect {
48
- Factory.define(:child, parent: :nonexsitent) {}
49
- Factory.build(:child)
50
- }.to raise_error(ArgumentError)
51
- end
52
- end
53
-
54
- describe "defining a factory" do
55
- before do
56
- @name = :user
57
- @factory = stub("factory", names: [@name])
58
- @proxy = "proxy"
59
- @options = { class: 'magic' }
60
- FactoryGirl::Factory.stubs(new: @factory)
61
- FactoryGirl::DefinitionProxy.stubs(new: @proxy)
62
- end
63
-
64
- it "creates a new factory using the specified name and options" do
65
- FactoryGirl::Factory.stubs(new: @factory)
66
- Factory.define(@name, @options) {|f| }
67
- FactoryGirl::Factory.should have_received(:new).with(@name, @options)
68
- end
69
-
70
- it "passes the factory do the block" do
71
- yielded = nil
72
- Factory.define(@name) do |y|
73
- yielded = y
74
- end
75
- yielded.should == @proxy
76
- end
77
-
78
- it "adds the factory to the list of factories" do
79
- Factory.define(@name) {|f| }
80
- @factory.should == FactoryGirl.factory_by_name(@name)
81
- end
82
- end
83
-
84
- describe "after defining a factory" do
85
- before do
86
- @name = :user
87
- @factory = FactoryGirl::Factory.new(@name)
88
-
89
- FactoryGirl.register_factory(@factory)
90
- end
91
-
92
- it "uses the attributes_for strategy for Factory.attributes_for" do
93
- @factory.stubs(run: "result")
94
- Factory.attributes_for(@name, attr: 'value').should == 'result'
95
- @factory.should have_received(:run).with(:attributes_for, attr: 'value')
96
- end
97
-
98
- it "uses the build strategy for Factory.build" do
99
- @factory.stubs(run: "result")
100
- Factory.build(@name, attr: 'value').should == 'result'
101
- @factory.should have_received(:run).with(:build, attr: 'value')
102
- end
103
-
104
- it "uses the create strategy for Factory.create" do
105
- @factory.stubs(run: "result")
106
- Factory.create(@name, attr: 'value').should == 'result'
107
- @factory.should have_received(:run).with(:create, attr: 'value')
108
- end
109
-
110
- it "uses the build_stubbed strategy for Factory.stub" do
111
- @factory.stubs(run: "result")
112
- Factory.stub(@name, attr: 'value').should == 'result'
113
- @factory.should have_received(:run).with(:build_stubbed, attr: 'value')
114
- end
115
-
116
- [:build, :create, :attributes_for, :stub].each do |method|
117
- it "raises an ArgumentError on #{method} with a nonexistent factory" do
118
- expect { Factory.send(method, :bogus) }.to raise_error(ArgumentError)
119
- end
120
-
121
- it "recognizes either 'name' or :name for Factory.#{method}" do
122
- @factory.stubs(:run)
123
- expect { Factory.send(method, @name.to_s) }.to_not raise_error
124
- expect { Factory.send(method, @name.to_sym) }.to_not raise_error
125
- end
126
- end
127
- end
128
-
129
- describe "defining a sequence" do
130
- before do
131
- @name = :count
132
- end
133
-
134
- it "creates a new sequence" do
135
- Factory.sequence(@name)
136
- Factory.next(@name).should == 1
137
- end
138
-
139
- it "uses the supplied block as the sequence generator" do
140
- Factory.sequence(@name) {|n| "user-#{n}" }
141
- Factory.next(@name).should == "user-1"
142
- end
143
-
144
- it "uses the supplied start_value as the sequence start_value" do
145
- Factory.sequence(@name, "A")
146
- Factory.next(@name).should == "A"
147
- end
148
- end
149
-
150
- describe "after defining a sequence" do
151
- before do
152
- @name = :test
153
- @sequence = FactoryGirl::Sequence.new(@name) {}
154
- @value = '1 2 5'
155
-
156
- @sequence.stubs(next: @value)
157
- FactoryGirl::Sequence.stubs(new: @sequence)
158
-
159
- Factory.sequence(@name) {}
160
- end
161
-
162
- it "calls next on the sequence when sent next" do
163
- Factory.next(@name)
164
- @sequence.should have_received(:next)
165
- end
166
-
167
- it "returns the value from the sequence" do
168
- Factory.next(@name).should == @value
169
- end
170
- end
171
-
172
- describe "an attribute generated by an in-line sequence" do
173
- before do
174
- define_model('User', username: :string)
175
-
176
- Factory.define :user do |factory|
177
- factory.sequence(:username) { |n| "username#{n}" }
178
- end
179
-
180
- @username = Factory.attributes_for(:user)[:username]
181
- end
182
-
183
- it "matches the correct format" do
184
- @username.should =~ /^username\d+$/
185
- end
186
-
187
- describe "after the attribute has already been generated once" do
188
- before do
189
- @another_username = Factory.attributes_for(:user)[:username]
190
- end
191
-
192
- it "matches the correct format" do
193
- @username.should =~ /^username\d+$/
194
- end
195
-
196
- it "is not the same as the first generated value" do
197
- @another_username.should_not == @username
198
- end
199
- end
200
- end
201
-
202
-
203
- describe "a factory with a parent" do
204
- before do
205
- define_model("User", username: :string)
206
-
207
- Factory.define(:user) do |factory|
208
- factory.username "awesome_username"
209
- end
210
-
211
- Factory.define(:boring_user, parent: :user) do |factory|
212
- factory.username "boring_username"
213
- end
214
- end
215
-
216
- it "supports defining parents" do
217
- Factory.build(:boring_user).username.should == "boring_username"
218
- end
219
- end