factory_girl_kibiz0r 2.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/Appraisals +12 -0
  2. data/CONTRIBUTION_GUIDELINES.md +9 -0
  3. data/Changelog +29 -0
  4. data/GETTING_STARTED.md +246 -0
  5. data/Gemfile +12 -0
  6. data/Gemfile.lock +62 -0
  7. data/LICENSE +19 -0
  8. data/README.md +64 -0
  9. data/Rakefile +54 -0
  10. data/features/factory_girl_steps.feature +151 -0
  11. data/features/step_definitions/database_steps.rb +20 -0
  12. data/features/support/env.rb +6 -0
  13. data/features/support/factories.rb +84 -0
  14. data/lib/factory_girl/aliases.rb +21 -0
  15. data/lib/factory_girl/attribute/association.rb +24 -0
  16. data/lib/factory_girl/attribute/callback.rb +16 -0
  17. data/lib/factory_girl/attribute/dynamic.rb +21 -0
  18. data/lib/factory_girl/attribute/implicit.rb +36 -0
  19. data/lib/factory_girl/attribute/list.rb +19 -0
  20. data/lib/factory_girl/attribute/sequence.rb +16 -0
  21. data/lib/factory_girl/attribute/static.rb +18 -0
  22. data/lib/factory_girl/attribute.rb +42 -0
  23. data/lib/factory_girl/definition_proxy.rb +147 -0
  24. data/lib/factory_girl/deprecated.rb +18 -0
  25. data/lib/factory_girl/factory.rb +196 -0
  26. data/lib/factory_girl/find_definitions.rb +23 -0
  27. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  28. data/lib/factory_girl/proxy/build.rb +36 -0
  29. data/lib/factory_girl/proxy/create.rb +16 -0
  30. data/lib/factory_girl/proxy/stub.rb +64 -0
  31. data/lib/factory_girl/proxy.rb +87 -0
  32. data/lib/factory_girl/rails2.rb +1 -0
  33. data/lib/factory_girl/registry.rb +45 -0
  34. data/lib/factory_girl/sequence.rb +31 -0
  35. data/lib/factory_girl/step_definitions.rb +60 -0
  36. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  37. data/lib/factory_girl/syntax/default.rb +33 -0
  38. data/lib/factory_girl/syntax/generate.rb +73 -0
  39. data/lib/factory_girl/syntax/make.rb +41 -0
  40. data/lib/factory_girl/syntax/methods.rb +86 -0
  41. data/lib/factory_girl/syntax/sham.rb +45 -0
  42. data/lib/factory_girl/syntax/vintage.rb +152 -0
  43. data/lib/factory_girl/syntax.rb +12 -0
  44. data/lib/factory_girl/version.rb +4 -0
  45. data/lib/factory_girl.rb +54 -0
  46. data/spec/acceptance/acceptance_helper.rb +11 -0
  47. data/spec/acceptance/attribute_aliases_spec.rb +26 -0
  48. data/spec/acceptance/attributes_for_spec.rb +48 -0
  49. data/spec/acceptance/build_spec.rb +35 -0
  50. data/spec/acceptance/build_stubbed_spec.rb +79 -0
  51. data/spec/acceptance/callbacks_spec.rb +53 -0
  52. data/spec/acceptance/create_spec.rb +67 -0
  53. data/spec/acceptance/default_strategy_spec.rb +27 -0
  54. data/spec/acceptance/definition_spec.rb +28 -0
  55. data/spec/acceptance/parent_spec.rb +39 -0
  56. data/spec/acceptance/sequence_spec.rb +34 -0
  57. data/spec/acceptance/syntax/blueprint_spec.rb +32 -0
  58. data/spec/acceptance/syntax/generate_spec.rb +60 -0
  59. data/spec/acceptance/syntax/make_spec.rb +35 -0
  60. data/spec/acceptance/syntax/sham_spec.rb +44 -0
  61. data/spec/acceptance/syntax/vintage_spec.rb +224 -0
  62. data/spec/factory_girl/aliases_spec.rb +33 -0
  63. data/spec/factory_girl/attribute/association_spec.rb +33 -0
  64. data/spec/factory_girl/attribute/callback_spec.rb +23 -0
  65. data/spec/factory_girl/attribute/dynamic_spec.rb +60 -0
  66. data/spec/factory_girl/attribute/implicit_spec.rb +50 -0
  67. data/spec/factory_girl/attribute/sequence_spec.rb +21 -0
  68. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  69. data/spec/factory_girl/attribute_spec.rb +39 -0
  70. data/spec/factory_girl/definition_proxy_spec.rb +129 -0
  71. data/spec/factory_girl/deprecated_spec.rb +66 -0
  72. data/spec/factory_girl/factory_spec.rb +374 -0
  73. data/spec/factory_girl/find_definitions_spec.rb +100 -0
  74. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  75. data/spec/factory_girl/proxy/build_spec.rb +86 -0
  76. data/spec/factory_girl/proxy/create_spec.rb +107 -0
  77. data/spec/factory_girl/proxy/stub_spec.rb +80 -0
  78. data/spec/factory_girl/proxy_spec.rb +102 -0
  79. data/spec/factory_girl/registry_spec.rb +83 -0
  80. data/spec/factory_girl/sequence_spec.rb +96 -0
  81. data/spec/factory_girl_spec.rb +17 -0
  82. data/spec/spec_helper.rb +93 -0
  83. metadata +294 -0
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ require 'factory_girl/syntax/blueprint'
5
+
6
+ describe "a blueprint" do
7
+ before do
8
+ define_model('User', :first_name => :string, :last_name => :string, :email => :string)
9
+
10
+ Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
11
+ User.blueprint do
12
+ first_name { 'Bill' }
13
+ last_name { 'Nye' }
14
+ email { FactoryGirl.generate(:email) }
15
+ end
16
+ end
17
+
18
+ describe "after making an instance" do
19
+ before do
20
+ @instance = FactoryGirl.create(:user, :last_name => 'Rye')
21
+ end
22
+
23
+ it "should use attributes from the blueprint" do
24
+ @instance.first_name.should == 'Bill'
25
+ end
26
+
27
+ it "should evaluate attribute blocks for each instance" do
28
+ @instance.email.should =~ /somebody\d+@example.com/
29
+ FactoryGirl.create(:user).email.should_not == @instance.email
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ require 'factory_girl/syntax/generate'
5
+
6
+ describe "a factory using generate syntax" do
7
+ before do
8
+ define_model('User', :first_name => :string, :last_name => :string, :email => :string) do
9
+ validates_presence_of :first_name
10
+ end
11
+
12
+ FactoryGirl.define do
13
+ factory :user do
14
+ first_name 'Bill'
15
+ last_name 'Nye'
16
+ email 'science@guys.net'
17
+ end
18
+ end
19
+ end
20
+
21
+ it "should not raise an error when generating an invalid instance" do
22
+ lambda { User.generate(:first_name => nil) }.should_not raise_error
23
+ end
24
+
25
+ it "should raise an error when forcefully generating an invalid instance" do
26
+ lambda { User.generate!(:first_name => nil) }.should raise_error(ActiveRecord::RecordInvalid)
27
+ end
28
+
29
+ %w(generate generate! spawn).each do |method|
30
+ it "should yield a generated instance when using #{method} with a block" do
31
+ saved_instance = nil
32
+ User.send(method) {|instance| saved_instance = instance }
33
+ saved_instance.should be_kind_of(User)
34
+ end
35
+
36
+ describe "after generating an instance using #{method}" do
37
+ before do
38
+ @instance = User.send(method, :last_name => 'Rye')
39
+ end
40
+
41
+ it "should use attributes from the factory" do
42
+ @instance.first_name.should == 'Bill'
43
+ end
44
+
45
+ it "should use attributes passed to generate" do
46
+ @instance.last_name.should == 'Rye'
47
+ end
48
+
49
+ if method == 'spawn'
50
+ it "should not save the record" do
51
+ @instance.should be_new_record
52
+ end
53
+ else
54
+ it "should save the record" do
55
+ @instance.should_not be_new_record
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ require 'factory_girl/syntax/make'
5
+
6
+ describe "a factory using make syntax" do
7
+ before do
8
+ define_model('User', :first_name => :string, :last_name => :string)
9
+
10
+ FactoryGirl.define do
11
+ factory :user do
12
+ first_name 'Bill'
13
+ last_name 'Nye'
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "after making an instance" do
19
+ before do
20
+ @instance = User.make(:last_name => 'Rye')
21
+ end
22
+
23
+ it "should use attributes from the factory" do
24
+ @instance.first_name.should == 'Bill'
25
+ end
26
+
27
+ it "should use attributes passed to make" do
28
+ @instance.last_name.should == 'Rye'
29
+ end
30
+
31
+ it "should save the record" do
32
+ @instance.should_not be_new_record
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ require 'factory_girl/syntax/sham'
5
+
6
+ describe "a factory using sham syntax" do
7
+ before do
8
+ define_model('User', :first_name => :string,
9
+ :last_name => :string,
10
+ :email => :string,
11
+ :username => :string)
12
+
13
+ Sham.name { "Name" }
14
+ Sham.email { "somebody#{rand(5)}@example.com" }
15
+ Sham.username("FOO") { |c| "User-#{c}" }
16
+
17
+ FactoryGirl.define do
18
+ factory :user do
19
+ first_name { Sham.name }
20
+ last_name { Sham.name }
21
+ email { Sham.email }
22
+ username { Sham.username }
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "after making an instance" do
28
+ before do
29
+ @instance = FactoryGirl.create(:user, :last_name => 'Rye')
30
+ end
31
+
32
+ it "should support a sham called 'name'" do
33
+ @instance.first_name.should == 'Name'
34
+ end
35
+
36
+ it "should support shams with starting values" do
37
+ @instance.username.should == 'User-FOO'
38
+ end
39
+
40
+ it "should use the sham for the email" do
41
+ @instance.email.should =~ /somebody\d@example.com/
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,224 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/acceptance_helper'
3
+
4
+ describe "vintage syntax" do
5
+ before do
6
+ define_model('User', :first_name => :string,
7
+ :last_name => :string,
8
+ :email => :string)
9
+
10
+ Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
11
+ Factory.define :user do |factory|
12
+ factory.first_name { 'Bill' }
13
+ factory.last_name { 'Nye' }
14
+ factory.email { Factory.next(:email) }
15
+ end
16
+ end
17
+
18
+ describe "after making an instance" do
19
+ before do
20
+ @instance = Factory(:user, :last_name => 'Rye')
21
+ end
22
+
23
+ it "should use attributes from the definition" do
24
+ @instance.first_name.should == 'Bill'
25
+ end
26
+
27
+ it "should evaluate attribute blocks for each instance" do
28
+ @instance.email.should =~ /somebody\d+@example.com/
29
+ Factory(:user).email.should_not == @instance.email
30
+ end
31
+ end
32
+
33
+ it "should raise an ArgumentError when trying to use a non-existent strategy" do
34
+ lambda {
35
+ Factory.define(:object, :default_strategy => :nonexistent) {}
36
+ }.should raise_error(ArgumentError)
37
+ end
38
+
39
+ it "should raise Factory::SequenceAbuseError" do
40
+ Factory.define :sequence_abuser, :class => User do |factory|
41
+ factory.first_name { Factory.sequence(:name) }
42
+ end
43
+
44
+ lambda {
45
+ Factory(:sequence_abuser)
46
+ }.should raise_error(FactoryGirl::SequenceAbuseError)
47
+ end
48
+ end
49
+
50
+ describe Factory, "given a parent factory" do
51
+ before do
52
+ @parent = FactoryGirl::Factory.new(:object)
53
+ @parent.define_attribute(FactoryGirl::Attribute::Static.new(:name, 'value'))
54
+ FactoryGirl.register_factory(@parent)
55
+ end
56
+
57
+ it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
58
+ lambda {
59
+ Factory.define(:child, :parent => :nonexsitent) {}
60
+ }.should raise_error(ArgumentError)
61
+ end
62
+ end
63
+
64
+ describe "defining a factory" do
65
+ before do
66
+ @name = :user
67
+ @factory = "factory"
68
+ @proxy = "proxy"
69
+ stub(@factory).names { [@name] }
70
+ @options = { :class => 'magic' }
71
+ stub(FactoryGirl::Factory).new { @factory }
72
+ stub(FactoryGirl::DefinitionProxy).new { @proxy }
73
+ end
74
+
75
+ it "should create a new factory using the specified name and options" do
76
+ mock(FactoryGirl::Factory).new(@name, @options) { @factory }
77
+ Factory.define(@name, @options) {|f| }
78
+ end
79
+
80
+ it "should pass the factory do the block" do
81
+ yielded = nil
82
+ Factory.define(@name) do |y|
83
+ yielded = y
84
+ end
85
+ yielded.should == @proxy
86
+ end
87
+
88
+ it "should add the factory to the list of factories" do
89
+ Factory.define(@name) {|f| }
90
+ @factory.should == FactoryGirl.factory_by_name(@name)
91
+ end
92
+ end
93
+
94
+ describe "after defining a factory" do
95
+ before do
96
+ @name = :user
97
+ @factory = FactoryGirl::Factory.new(@name)
98
+
99
+ FactoryGirl.register_factory(@factory)
100
+ end
101
+
102
+ it "should use Proxy::AttributesFor for Factory.attributes_for" do
103
+ mock(@factory).run(FactoryGirl::Proxy::AttributesFor, :attr => 'value') { 'result' }
104
+ Factory.attributes_for(@name, :attr => 'value').should == 'result'
105
+ end
106
+
107
+ it "should use Proxy::Build for Factory.build" do
108
+ mock(@factory).run(FactoryGirl::Proxy::Build, :attr => 'value') { 'result' }
109
+ Factory.build(@name, :attr => 'value').should == 'result'
110
+ end
111
+
112
+ it "should use Proxy::Create for Factory.create" do
113
+ mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
114
+ Factory.create(@name, :attr => 'value').should == 'result'
115
+ end
116
+
117
+ it "should use Proxy::Stub for Factory.stub" do
118
+ mock(@factory).run(FactoryGirl::Proxy::Stub, :attr => 'value') { 'result' }
119
+ Factory.stub(@name, :attr => 'value').should == 'result'
120
+ end
121
+
122
+ it "should use default strategy option as Factory.default_strategy" do
123
+ stub(@factory).default_strategy { :create }
124
+ mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
125
+ Factory.default_strategy(@name, :attr => 'value').should == 'result'
126
+ end
127
+
128
+ it "should use the default strategy for the global Factory method" do
129
+ stub(@factory).default_strategy { :create }
130
+ mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
131
+ Factory(@name, :attr => 'value').should == 'result'
132
+ end
133
+
134
+ [:build, :create, :attributes_for, :stub].each do |method|
135
+ it "should raise an ArgumentError on #{method} with a nonexistant factory" do
136
+ lambda { Factory.send(method, :bogus) }.should raise_error(ArgumentError)
137
+ end
138
+
139
+ it "should recognize either 'name' or :name for Factory.#{method}" do
140
+ stub(@factory).run
141
+ lambda { Factory.send(method, @name.to_s) }.should_not raise_error
142
+ lambda { Factory.send(method, @name.to_sym) }.should_not raise_error
143
+ end
144
+ end
145
+ end
146
+
147
+ describe "defining a sequence" do
148
+ before do
149
+ @name = :count
150
+ @sequence = FactoryGirl::Sequence.new(@name) {}
151
+ stub(FactoryGirl::Sequence).new { @sequence }
152
+ end
153
+
154
+ it "should create a new sequence" do
155
+ mock(FactoryGirl::Sequence).new(@name, 1) { @sequence }
156
+ Factory.sequence(@name)
157
+ end
158
+
159
+ it "should use the supplied block as the sequence generator" do
160
+ stub(FactoryGirl::Sequence).new { @sequence }.yields(1)
161
+ yielded = false
162
+ Factory.sequence(@name) {|n| yielded = true }
163
+ (yielded).should be
164
+ end
165
+
166
+ it "should use the supplied start_value as the sequence start_value" do
167
+ mock(FactoryGirl::Sequence).new(@name, "A") { @sequence }
168
+ Factory.sequence(@name, "A")
169
+ end
170
+ end
171
+
172
+ describe "after defining a sequence" do
173
+ before do
174
+ @name = :test
175
+ @sequence = FactoryGirl::Sequence.new(@name) {}
176
+ @value = '1 2 5'
177
+
178
+ stub(@sequence).next { @value }
179
+ stub(FactoryGirl::Sequence).new { @sequence }
180
+
181
+ Factory.sequence(@name) {}
182
+ end
183
+
184
+ it "should call next on the sequence when sent next" do
185
+ mock(@sequence).next
186
+
187
+ Factory.next(@name)
188
+ end
189
+
190
+ it "should return the value from the sequence" do
191
+ Factory.next(@name).should == @value
192
+ end
193
+ end
194
+
195
+ describe "an attribute generated by an in-line sequence" do
196
+ before do
197
+ define_model('User', :username => :string)
198
+
199
+ Factory.define :user do |factory|
200
+ factory.sequence(:username) { |n| "username#{n}" }
201
+ end
202
+
203
+ @username = Factory.attributes_for(:user)[:username]
204
+ end
205
+
206
+ it "should match the correct format" do
207
+ @username.should =~ /^username\d+$/
208
+ end
209
+
210
+ describe "after the attribute has already been generated once" do
211
+ before do
212
+ @another_username = Factory.attributes_for(:user)[:username]
213
+ end
214
+
215
+ it "should match the correct format" do
216
+ @username.should =~ /^username\d+$/
217
+ end
218
+
219
+ it "should not be the same as the first generated value" do
220
+ @another_username.should_not == @username
221
+ end
222
+ end
223
+ end
224
+
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Factory, "aliases" do
4
+
5
+ it "should include an attribute as an alias for itself by default" do
6
+ FactoryGirl.aliases_for(:test).should include(:test)
7
+ end
8
+
9
+ it "should include the root of a foreign key as an alias by default" do
10
+ FactoryGirl.aliases_for(:test_id).should include(:test)
11
+ end
12
+
13
+ it "should include an attribute's foreign key as an alias by default" do
14
+ FactoryGirl.aliases_for(:test).should include(:test_id)
15
+ end
16
+
17
+ it "should NOT include an attribute as an alias when it starts with underscore" do
18
+ FactoryGirl.aliases_for(:_id).should_not include(:id)
19
+ end
20
+
21
+ describe "after adding an alias" do
22
+
23
+ before do
24
+ Factory.alias(/(.*)_suffix/, '\1')
25
+ end
26
+
27
+ it "should return the alias in the aliases list" do
28
+ FactoryGirl.aliases_for(:test_suffix).should include(:test)
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Association do
4
+ before do
5
+ @name = :author
6
+ @factory = :user
7
+ @overrides = { :first_name => 'John' }
8
+ @attr = FactoryGirl::Attribute::Association.new(@name, @factory, @overrides)
9
+ end
10
+
11
+ it "should have a name" do
12
+ @attr.name.should == @name
13
+ end
14
+
15
+ it "is an association" do
16
+ @attr.should be_association
17
+ end
18
+
19
+ it "should have a factory" do
20
+ @attr.factory.should == @factory
21
+ end
22
+
23
+ it "should tell the proxy to associate when being added to a proxy" do
24
+ proxy = "proxy"
25
+ stub(proxy).associate
26
+ @attr.add_to(proxy)
27
+ proxy.should have_received.associate(@name, @factory, @overrides)
28
+ end
29
+
30
+ it "should convert names to symbols" do
31
+ FactoryGirl::Attribute::Association.new('name', :user, {}).name.should == :name
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Callback do
4
+ before do
5
+ @name = :after_create
6
+ @block = proc{ 'block' }
7
+ @attr = FactoryGirl::Attribute::Callback.new(@name, @block)
8
+ end
9
+
10
+ it "should have a name" do
11
+ @attr.name.should == @name
12
+ end
13
+
14
+ it "should set its callback on a proxy" do
15
+ @proxy = "proxy"
16
+ mock(@proxy).add_callback(@name, @block)
17
+ @attr.add_to(@proxy)
18
+ end
19
+
20
+ it "should convert names to symbols" do
21
+ FactoryGirl::Attribute::Callback.new('name', nil).name.should == :name
22
+ end
23
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Dynamic do
4
+ before do
5
+ @name = :first_name
6
+ @block = lambda { 'value' }
7
+ @attr = FactoryGirl::Attribute::Dynamic.new(@name, @block)
8
+ end
9
+
10
+ it "should have a name" do
11
+ @attr.name.should == @name
12
+ end
13
+
14
+ it "should call the block to set a value" do
15
+ @proxy = "proxy"
16
+ stub(@proxy).set
17
+ @attr.add_to(@proxy)
18
+ @proxy.should have_received.set(@name, 'value')
19
+ end
20
+
21
+ it "should yield the proxy to the block when adding its value to a proxy" do
22
+ @block = lambda {|a| a }
23
+ @attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
24
+ @proxy = "proxy"
25
+ stub(@proxy).set
26
+ @attr.add_to(@proxy)
27
+ @proxy.should have_received.set(:user, @proxy)
28
+ end
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
+
41
+ it "should raise an error when defining an attribute writer" do
42
+ lambda {
43
+ FactoryGirl::Attribute::Dynamic.new('test=', nil)
44
+ }.should raise_error(FactoryGirl::AttributeDefinitionError)
45
+ end
46
+
47
+ it "should raise an error when returning a sequence" do
48
+ stub(Factory).sequence { FactoryGirl::Sequence.new(:email) }
49
+ block = lambda { Factory.sequence(:email) }
50
+ attr = FactoryGirl::Attribute::Dynamic.new(:email, block)
51
+ proxy = stub!.set.subject
52
+ lambda {
53
+ attr.add_to(proxy)
54
+ }.should raise_error(FactoryGirl::SequenceAbuseError)
55
+ end
56
+
57
+ it "should convert names to symbols" do
58
+ FactoryGirl::Attribute::Dynamic.new('name', nil).name.should == :name
59
+ end
60
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Implicit do
4
+ before do
5
+ @name = :author
6
+ @attr = FactoryGirl::Attribute::Implicit.new(@name)
7
+ end
8
+
9
+ it "has a name" do
10
+ @attr.name.should == @name
11
+ end
12
+
13
+ context "with a known factory" do
14
+ before do
15
+ stub(FactoryGirl.factories).registered? { true }
16
+ end
17
+
18
+ it "associates the factory" do
19
+ proxy = "proxy"
20
+ stub(proxy).associate
21
+ @attr.add_to(proxy)
22
+ proxy.should have_received.associate(@name, @name, {})
23
+ end
24
+
25
+ it "is an association" do
26
+ @attr.should be_association
27
+ end
28
+
29
+ it "has a factory" do
30
+ @attr.factory.should == @name
31
+ end
32
+ end
33
+
34
+ context "with a known sequence" do
35
+ before do
36
+ FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@name, 1) { "magic" })
37
+ end
38
+
39
+ it "generates the sequence" do
40
+ proxy = "proxy"
41
+ stub(proxy).set
42
+ @attr.add_to(proxy)
43
+ proxy.should have_received.set(@name, "magic")
44
+ end
45
+
46
+ it "isn't an association" do
47
+ @attr.should_not be_association
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Sequence do
4
+ before do
5
+ @name = :first_name
6
+ @sequence = :name
7
+ FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@sequence, 5) { |n| "Name #{n}" })
8
+ @attr = FactoryGirl::Attribute::Sequence.new(@name, @sequence)
9
+ end
10
+
11
+ it "should have a name" do
12
+ @attr.name.should == @name
13
+ end
14
+
15
+ it "assigns the next value in the sequence" do
16
+ proxy = "proxy"
17
+ stub(proxy).set
18
+ @attr.add_to(proxy)
19
+ proxy.should have_received.set(@name, "Name 5")
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute::Static do
4
+ before do
5
+ @name = :first_name
6
+ @value = 'John'
7
+ @attr = FactoryGirl::Attribute::Static.new(@name, @value)
8
+ end
9
+
10
+ it "should have a name" do
11
+ @attr.name.should == @name
12
+ end
13
+
14
+ it "should set its static value on a proxy" do
15
+ @proxy = "proxy"
16
+ mock(@proxy).set(@name, @value)
17
+ @attr.add_to(@proxy)
18
+ end
19
+
20
+ it "should raise an error when defining an attribute writer" do
21
+ lambda {
22
+ FactoryGirl::Attribute::Static.new('test=', nil)
23
+ }.should raise_error(FactoryGirl::AttributeDefinitionError)
24
+ end
25
+
26
+ it "should convert names to symbols" do
27
+ FactoryGirl::Attribute::Static.new('name', nil).name.should == :name
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirl::Attribute do
4
+ before do
5
+ @name = :user
6
+ @attr = FactoryGirl::Attribute.new(@name)
7
+ end
8
+
9
+ it "should have a name" do
10
+ @attr.name.should == @name
11
+ end
12
+
13
+ it "isn't an association" do
14
+ @attr.should_not be_association
15
+ end
16
+
17
+ it "should do nothing when being added to a proxy" do
18
+ @proxy = "proxy"
19
+ stub(@proxy).set
20
+ @attr.add_to(@proxy)
21
+ @proxy.should have_received.set.never
22
+ end
23
+
24
+ it "should raise an error when defining an attribute writer" do
25
+ error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
26
+ lambda {
27
+ FactoryGirl::Attribute.new('test=')
28
+ }.should raise_error(FactoryGirl::AttributeDefinitionError, error_message)
29
+ end
30
+
31
+ it "should convert names to symbols" do
32
+ FactoryGirl::Attribute.new('name').name.should == :name
33
+ end
34
+
35
+ it "should be ignorable" do
36
+ @attr.ignore
37
+ @attr.should be_ignored
38
+ end
39
+ end