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.
Files changed (44) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +4 -5
  2. data/README.rdoc +6 -6
  3. data/Rakefile +11 -14
  4. data/lib/factory_girl/attribute.rb +3 -2
  5. data/lib/factory_girl/attribute/dynamic.rb +5 -2
  6. data/lib/factory_girl/proxy/stub.rb +33 -12
  7. data/lib/factory_girl/sequence.rb +3 -0
  8. data/spec/factory_girl/aliases_spec.rb +29 -0
  9. data/spec/factory_girl/attribute/association_spec.rb +25 -0
  10. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  11. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  12. data/spec/factory_girl/attribute_spec.rb +30 -0
  13. data/spec/factory_girl/factory_spec.rb +490 -0
  14. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  15. data/spec/factory_girl/proxy/build_spec.rb +73 -0
  16. data/spec/factory_girl/proxy/create_spec.rb +83 -0
  17. data/spec/factory_girl/proxy/stub_spec.rb +69 -0
  18. data/spec/factory_girl/proxy_spec.rb +28 -0
  19. data/spec/factory_girl/sequence_spec.rb +66 -0
  20. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  21. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  22. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  23. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  24. data/spec/integration_spec.rb +265 -0
  25. data/{test → spec}/models.rb +0 -0
  26. data/{test/test_helper.rb → spec/spec_helper.rb} +12 -5
  27. metadata +44 -42
  28. data/test/aliases_test.rb +0 -29
  29. data/test/association_attribute_test.rb +0 -31
  30. data/test/attribute_test.rb +0 -32
  31. data/test/attributes_for_strategy_test.rb +0 -55
  32. data/test/build_strategy_test.rb +0 -79
  33. data/test/create_strategy_test.rb +0 -90
  34. data/test/dynamic_attribute_test.rb +0 -41
  35. data/test/factory_test.rb +0 -513
  36. data/test/integration_test.rb +0 -235
  37. data/test/sequence_test.rb +0 -76
  38. data/test/static_attribute_test.rb +0 -33
  39. data/test/strategy_test.rb +0 -33
  40. data/test/stub_strategy_test.rb +0 -61
  41. data/test/syntax/blueprint_test.rb +0 -39
  42. data/test/syntax/generate_test.rb +0 -63
  43. data/test/syntax/make_test.rb +0 -39
  44. data/test/syntax/sham_test.rb +0 -40
@@ -1,235 +0,0 @@
1
- require 'test_helper'
2
-
3
- class IntegrationTest < Test::Unit::TestCase
4
-
5
- def setup
6
- Factory.define :user, :class => 'user' do |f|
7
- f.first_name 'Jimi'
8
- f.last_name 'Hendrix'
9
- f.admin false
10
- f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
11
- end
12
-
13
- Factory.define Post, :default_strategy => :attributes_for do |f|
14
- f.name 'Test Post'
15
- f.association :author, :factory => :user
16
- end
17
-
18
- Factory.define :admin, :class => User do |f|
19
- f.first_name 'Ben'
20
- f.last_name 'Stein'
21
- f.admin true
22
- f.sequence(:username) { |n| "username#{n}" }
23
- f.email { Factory.next(:email) }
24
- end
25
-
26
- Factory.define :guest, :parent => :user do |f|
27
- f.last_name 'Anonymous'
28
- f.username 'GuestUser'
29
- end
30
-
31
- Factory.sequence :email do |n|
32
- "somebody#{n}@example.com"
33
- end
34
- end
35
-
36
- def teardown
37
- Factory.factories.clear
38
- end
39
-
40
- context "a generated attributes hash" do
41
-
42
- setup do
43
- @attrs = Factory.attributes_for(:user, :first_name => 'Bill')
44
- end
45
-
46
- should "assign all attributes" do
47
- assert_equal [:admin, :email, :first_name, :last_name],
48
- @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
49
- end
50
-
51
- should "correctly assign lazy, dependent attributes" do
52
- assert_equal "bill.hendrix@example.com", @attrs[:email]
53
- end
54
-
55
- should "override attrbutes" do
56
- assert_equal 'Bill', @attrs[:first_name]
57
- end
58
-
59
- should "not assign associations" do
60
- assert_nil Factory.attributes_for(:post)[:author]
61
- end
62
-
63
- end
64
-
65
- context "a built instance" do
66
-
67
- setup do
68
- @instance = Factory.build(:post)
69
- end
70
-
71
- should "not be saved" do
72
- assert @instance.new_record?
73
- end
74
-
75
- should "assign associations" do
76
- assert_kind_of User, @instance.author
77
- end
78
-
79
- should "save associations" do
80
- assert !@instance.author.new_record?
81
- end
82
-
83
- should "not assign both an association and its foreign key" do
84
- assert_equal 1, Factory.build(:post, :author_id => 1).author_id
85
- end
86
-
87
- end
88
-
89
- context "a created instance" do
90
-
91
- setup do
92
- @instance = Factory.create('post')
93
- end
94
-
95
- should "be saved" do
96
- assert !@instance.new_record?
97
- end
98
-
99
- should "assign associations" do
100
- assert_kind_of User, @instance.author
101
- end
102
-
103
- should "save associations" do
104
- assert !@instance.author.new_record?
105
- end
106
-
107
- end
108
-
109
- context "a generated mock instance" do
110
-
111
- setup do
112
- @stub = Factory.stub(:user, :first_name => 'Bill')
113
- end
114
-
115
- should "assign all attributes" do
116
- [:admin, :email, :first_name, :last_name].each do |attr|
117
- assert_not_nil @stub.send(attr)
118
- end
119
- end
120
-
121
- should "correctly assign lazy, dependent attributes" do
122
- assert_equal "bill.hendrix@example.com", @stub.email
123
- end
124
-
125
- should "override attrbutes" do
126
- assert_equal 'Bill', @stub.first_name
127
- end
128
-
129
- should "not assign associations" do
130
- assert_nil Factory.stub(:post).author
131
- end
132
-
133
- end
134
-
135
- context "an instance generated by a factory with a custom class name" do
136
-
137
- setup do
138
- @instance = Factory.create(:admin)
139
- end
140
-
141
- should "use the correct class name" do
142
- assert_kind_of User, @instance
143
- end
144
-
145
- should "use the correct factory definition" do
146
- assert @instance.admin?
147
- end
148
-
149
- end
150
-
151
- context "an instance generated by a factory that inherits from another factory" do
152
- setup do
153
- @instance = Factory.create(:guest)
154
- end
155
-
156
- should "use the class name of the parent factory" do
157
- assert_kind_of User, @instance
158
- end
159
-
160
- should "have attributes of the parent" do
161
- assert_equal 'Jimi', @instance.first_name
162
- end
163
-
164
- should "have attributes defined in the factory itself" do
165
- assert_equal 'GuestUser', @instance.username
166
- end
167
-
168
- should "have attributes that have been overriden" do
169
- assert_equal 'Anonymous', @instance.last_name
170
- end
171
- end
172
-
173
- context "an attribute generated by a sequence" do
174
-
175
- setup do
176
- @email = Factory.attributes_for(:admin)[:email]
177
- end
178
-
179
- should "match the correct format" do
180
- assert_match /^somebody\d+@example\.com$/, @email
181
- end
182
-
183
- context "after the attribute has already been generated once" do
184
-
185
- setup do
186
- @another_email = Factory.attributes_for(:admin)[:email]
187
- end
188
-
189
- should "match the correct format" do
190
- assert_match /^somebody\d+@example\.com$/, @email
191
- end
192
-
193
- should "not be the same as the first generated value" do
194
- assert_not_equal @email, @another_email
195
- end
196
-
197
- end
198
-
199
- end
200
-
201
- context "an attribute generated by an in-line sequence" do
202
-
203
- setup do
204
- @username = Factory.attributes_for(:admin)[:username]
205
- end
206
-
207
- should "match the correct format" do
208
- assert_match /^username\d+$/, @username
209
- end
210
-
211
- context "after the attribute has already been generated once" do
212
-
213
- setup do
214
- @another_username = Factory.attributes_for(:admin)[:username]
215
- end
216
-
217
- should "match the correct format" do
218
- assert_match /^username\d+$/, @username
219
- end
220
-
221
- should "not be the same as the first generated value" do
222
- assert_not_equal @username, @another_username
223
- end
224
-
225
- end
226
-
227
- end
228
-
229
- context "a factory with a default strategy specified" do
230
- should "generate instances according to the strategy" do
231
- assert_kind_of Hash, Factory(:post)
232
- end
233
- end
234
-
235
- end
@@ -1,76 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SequenceTest < Test::Unit::TestCase
4
-
5
- context "a sequence" do
6
-
7
- setup do
8
- @sequence = Factory::Sequence.new {|n| "=#{n}" }
9
- end
10
-
11
- should "start with a value of 1" do
12
- assert_equal "=1", @sequence.next
13
- end
14
-
15
- context "after being called" do
16
-
17
- setup do
18
- @sequence.next
19
- end
20
-
21
- should "use the next value" do
22
- assert_equal "=2", @sequence.next
23
- end
24
-
25
- end
26
-
27
- end
28
-
29
- context "defining a sequence" do
30
-
31
- setup do
32
- @sequence = mock('sequence')
33
- @name = :count
34
- Factory::Sequence.stubs(:new).returns(@sequence)
35
- end
36
-
37
- should "create a new sequence" do
38
- Factory::Sequence.expects(:new).with().returns(@sequence)
39
- Factory.sequence(@name)
40
- end
41
-
42
- should "use the supplied block as the sequence generator" do
43
- Factory::Sequence.stubs(:new).yields(1)
44
- yielded = false
45
- Factory.sequence(@name) {|n| yielded = true }
46
- assert yielded
47
- end
48
-
49
- end
50
-
51
- context "after defining a sequence" do
52
-
53
- setup do
54
- @sequence = mock('sequence')
55
- @name = :test
56
- @value = '1 2 5'
57
-
58
- @sequence. stubs(:next).returns(@value)
59
- Factory::Sequence.stubs(:new). returns(@sequence)
60
-
61
- Factory.sequence(@name) {}
62
- end
63
-
64
- should "call next on the sequence when sent next" do
65
- @sequence.expects(:next)
66
-
67
- Factory.next(@name)
68
- end
69
-
70
- should "return the value from the sequence" do
71
- assert_equal @value, Factory.next(@name)
72
- end
73
-
74
- end
75
-
76
- end
@@ -1,33 +0,0 @@
1
- require 'test_helper'
2
-
3
- class StaticAttributeTest < Test::Unit::TestCase
4
-
5
- context "a static attribute" do
6
- setup do
7
- @name = :first_name
8
- @value = 'John'
9
- @attr = Factory::Attribute::Static.new(@name, @value)
10
- end
11
-
12
- should "have a name" do
13
- assert_equal @name, @attr.name
14
- end
15
-
16
- should "set its static value on a proxy" do
17
- @proxy = mock('proxy')
18
- @proxy.expects(:set).with(@name, @value)
19
- @attr.add_to(@proxy)
20
- end
21
- end
22
-
23
- should "raise an error when defining an attribute writer" do
24
- assert_raise Factory::AttributeDefinitionError do
25
- Factory::Attribute::Static.new('test=', nil)
26
- end
27
- end
28
-
29
- should "convert names to symbols" do
30
- assert_equal :name, Factory::Attribute::Static.new('name', nil).name
31
- end
32
-
33
- end
@@ -1,33 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Proxy < Test::Unit::TestCase
4
-
5
- context "a proxy" do
6
- setup do
7
- @proxy = Factory::Proxy.new(Class.new)
8
- end
9
-
10
- should "do nothing when asked to set an attribute to a value" do
11
- assert_nothing_raised { @proxy.set(:name, 'a name') }
12
- end
13
-
14
- should "return nil when asked for an attribute" do
15
- assert_nil @proxy.get(:name)
16
- end
17
-
18
- should "call get for a missing method" do
19
- @proxy.expects(:get).with(:name).returns("it's a name")
20
- assert_equal "it's a name", @proxy.name
21
- end
22
-
23
- should "do nothing when asked to associate with another factory" do
24
- assert_nothing_raised { @proxy.associate(:owner, :user, {}) }
25
- end
26
-
27
- should "raise an error when asked for the result" do
28
- assert_raise(NotImplementedError) { @proxy.result }
29
- end
30
- end
31
-
32
- end
33
-
@@ -1,61 +0,0 @@
1
- require 'test_helper'
2
-
3
- class StubProxyTest < Test::Unit::TestCase
4
- context "the stub proxy" do
5
- setup do
6
- @proxy = Factory::Proxy::Stub.new(@class)
7
- end
8
-
9
- context "when asked to associate with another factory" do
10
- setup do
11
- Factory.stubs(:create)
12
- @proxy.associate(:owner, :user, {})
13
- end
14
-
15
- should "not set a value for the association" do
16
- assert_nil @proxy.result.owner
17
- end
18
- end
19
-
20
- should "return nil when building an association" do
21
- assert_nil @proxy.association(:user)
22
- end
23
-
24
- should "not call Factory.create when building an association" do
25
- Factory.expects(:create).never
26
- assert_nil @proxy.association(:user)
27
- end
28
-
29
- should "always return nil when building an association" do
30
- @proxy.set(:association, 'x')
31
- assert_nil @proxy.association(:user)
32
- end
33
-
34
- should "return a mock object when asked for the result" do
35
- assert_kind_of Object, @proxy.result
36
- end
37
-
38
- context "setting an attribute" do
39
- should "define attributes even if attribute= is defined" do
40
- @proxy.set('attribute', nil)
41
- assert_nothing_raised do
42
- @proxy.set('age', 18)
43
- end
44
- end
45
- end
46
-
47
- context "after setting an attribute" do
48
- setup do
49
- @proxy.set(:attribute, 'value')
50
- end
51
-
52
- should "add a stub to the resulting object" do
53
- assert_equal 'value', @proxy.attribute
54
- end
55
-
56
- should "return that value when asked for that attribute" do
57
- assert_equal 'value', @proxy.get(:attribute)
58
- end
59
- end
60
- end
61
- end
@@ -1,39 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'factory_girl/syntax/blueprint'
4
-
5
- class BlueprintSyntaxTest < Test::Unit::TestCase
6
-
7
- context "a blueprint" do
8
- setup do
9
- Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
10
- User.blueprint do
11
- first_name { 'Bill' }
12
- last_name { 'Nye' }
13
- email { Factory.next(:email) }
14
- end
15
- end
16
-
17
- teardown do
18
- Factory.factories.clear
19
- Factory.sequences.clear
20
- end
21
-
22
- context "after making an instance" do
23
- setup do
24
- @instance = Factory(:user, :last_name => 'Rye')
25
- end
26
-
27
- should "use attributes from the blueprint" do
28
- assert_equal 'Bill', @instance.first_name
29
- end
30
-
31
- should "evaluate attribute blocks for each instance" do
32
- assert_match /somebody\d+@example.com/, @instance.email
33
- assert_not_equal @instance.email, Factory(:user).email
34
- end
35
- end
36
- end
37
-
38
- end
39
-