challah 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.md +9 -2
  2. data/README.md +18 -18
  3. data/lib/challah/test.rb +22 -5
  4. data/lib/challah/version.rb +1 -1
  5. data/test/audit_test.rb +32 -32
  6. data/test/controller_test.rb +9 -9
  7. data/test/cookie_store_test.rb +25 -25
  8. data/test/factories.rb +7 -7
  9. data/test/helper.rb +14 -12
  10. data/test/permission_test.rb +14 -14
  11. data/test/restrictions_controller_test.rb +29 -30
  12. data/test/role_test.rb +26 -26
  13. data/test/session_test.rb +65 -65
  14. data/test/sessions_controller_test.rb +13 -13
  15. data/test/simple_cookie_store_test.rb +25 -25
  16. data/test/user_test.rb +79 -79
  17. data/vendor/bundle/cache/factory_girl-3.0.0.gem +0 -0
  18. data/vendor/bundle/gems/factory_girl-3.0.0/Appraisals +11 -0
  19. data/vendor/bundle/gems/factory_girl-3.0.0/CONTRIBUTION_GUIDELINES.md +10 -0
  20. data/vendor/bundle/gems/factory_girl-3.0.0/Changelog +168 -0
  21. data/vendor/bundle/gems/factory_girl-3.0.0/GETTING_STARTED.md +735 -0
  22. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile +5 -0
  23. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile.lock +95 -0
  24. data/vendor/bundle/gems/factory_girl-3.0.0/LICENSE +19 -0
  25. data/vendor/bundle/gems/factory_girl-3.0.0/README.md +70 -0
  26. data/vendor/bundle/gems/factory_girl-3.0.0/Rakefile +38 -0
  27. data/vendor/bundle/gems/factory_girl-3.0.0/cucumber.yml +1 -0
  28. data/vendor/bundle/gems/factory_girl-3.0.0/factory_girl.gemspec +38 -0
  29. data/vendor/bundle/gems/factory_girl-3.0.0/features/factory_girl_steps.feature +237 -0
  30. data/vendor/bundle/gems/factory_girl-3.0.0/features/find_definitions.feature +75 -0
  31. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/database_steps.rb +42 -0
  32. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/factory_girl_steps.rb +43 -0
  33. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/env.rb +11 -0
  34. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/factories.rb +119 -0
  35. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/2.3.gemfile +7 -0
  36. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile +7 -0
  37. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile.lock +86 -0
  38. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile +7 -0
  39. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile.lock +95 -0
  40. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile +7 -0
  41. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile.lock +93 -0
  42. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl.rb +69 -0
  43. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/aliases.rb +19 -0
  44. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute.rb +40 -0
  45. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/association.rb +23 -0
  46. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/dynamic.rb +20 -0
  47. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/sequence.rb +17 -0
  48. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/static.rb +15 -0
  49. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_assigner.rb +73 -0
  50. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb +54 -0
  51. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/callback.rb +33 -0
  52. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration.rb +22 -0
  53. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/association.rb +25 -0
  54. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/dynamic.rb +25 -0
  55. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/implicit.rb +32 -0
  56. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/static.rb +25 -0
  57. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration_list.rb +48 -0
  58. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition.rb +76 -0
  59. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition_proxy.rb +159 -0
  60. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/errors.rb +19 -0
  61. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator.rb +76 -0
  62. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator_class_definer.rb +34 -0
  63. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory.rb +145 -0
  64. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory_runner.rb +24 -0
  65. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/find_definitions.rb +25 -0
  66. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_factory.rb +15 -0
  67. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_object.rb +7 -0
  68. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/registry.rb +46 -0
  69. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/reload.rb +8 -0
  70. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/sequence.rb +24 -0
  71. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/step_definitions.rb +130 -0
  72. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy.rb +32 -0
  73. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/attributes_for.rb +12 -0
  74. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/build.rb +15 -0
  75. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/create.rb +17 -0
  76. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/stub.rb +61 -0
  77. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy_calculator.rb +29 -0
  78. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax.rb +16 -0
  79. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/blueprint.rb +42 -0
  80. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/default.rb +56 -0
  81. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/generate.rb +70 -0
  82. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/make.rb +45 -0
  83. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/methods.rb +136 -0
  84. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/sham.rb +45 -0
  85. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/vintage.rb +130 -0
  86. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/trait.rb +29 -0
  87. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/version.rb +4 -0
  88. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/aliases_spec.rb +19 -0
  89. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_aliases_spec.rb +45 -0
  90. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_existing_on_object_spec.rb +68 -0
  91. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_for_spec.rb +89 -0
  92. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_from_instance_spec.rb +53 -0
  93. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_ordered_spec.rb +51 -0
  94. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_list_spec.rb +41 -0
  95. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_spec.rb +89 -0
  96. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_stubbed_spec.rb +104 -0
  97. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/callbacks_spec.rb +47 -0
  98. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_list_spec.rb +82 -0
  99. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_spec.rb +117 -0
  100. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/define_child_before_parent_spec.rb +21 -0
  101. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_spec.rb +26 -0
  102. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_without_block_spec.rb +15 -0
  103. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/initialize_with_spec.rb +147 -0
  104. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_factories_spec.rb +184 -0
  105. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_inherited_spec.rb +52 -0
  106. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/nested_attributes_spec.rb +32 -0
  107. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/overrides_spec.rb +61 -0
  108. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/parent_spec.rb +90 -0
  109. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/sequence_spec.rb +33 -0
  110. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/stub_spec.rb +62 -0
  111. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/blueprint_spec.rb +34 -0
  112. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/generate_spec.rb +59 -0
  113. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/make_spec.rb +52 -0
  114. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/sham_spec.rb +43 -0
  115. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/vintage_spec.rb +217 -0
  116. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/traits_spec.rb +421 -0
  117. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/transient_attributes_spec.rb +124 -0
  118. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/aliases_spec.rb +31 -0
  119. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/association_spec.rb +28 -0
  120. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/dynamic_spec.rb +52 -0
  121. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/sequence_spec.rb +16 -0
  122. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/static_spec.rb +19 -0
  123. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_list_spec.rb +78 -0
  124. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_spec.rb +16 -0
  125. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/callback_spec.rb +41 -0
  126. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration/implicit_spec.rb +25 -0
  127. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration_list_spec.rb +71 -0
  128. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_proxy_spec.rb +197 -0
  129. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_spec.rb +104 -0
  130. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/evaluator_class_definer_spec.rb +54 -0
  131. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/factory_spec.rb +279 -0
  132. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/find_definitions_spec.rb +110 -0
  133. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_factory_spec.rb +13 -0
  134. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_object_spec.rb +8 -0
  135. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/registry_spec.rb +81 -0
  136. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/sequence_spec.rb +47 -0
  137. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/attributes_for_spec.rb +18 -0
  138. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/build_spec.rb +7 -0
  139. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/create_spec.rb +13 -0
  140. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/stub_spec.rb +40 -0
  141. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_calculator_spec.rb +33 -0
  142. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_spec.rb +21 -0
  143. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl_spec.rb +22 -0
  144. data/vendor/bundle/gems/factory_girl-3.0.0/spec/spec_helper.rb +26 -0
  145. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/macros/define_constant.rb +86 -0
  146. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/callback.rb +9 -0
  147. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/declaration.rb +71 -0
  148. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/delegate.rb +44 -0
  149. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/trait.rb +9 -0
  150. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/shared_examples/strategy.rb +104 -0
  151. data/vendor/bundle/specifications/factory_girl-3.0.0.gemspec +62 -0
  152. metadata +147 -12
@@ -0,0 +1,217 @@
1
+ require 'spec_helper'
2
+
3
+ describe "vintage syntax" do
4
+ before do
5
+ define_model('User', first_name: :string,
6
+ last_name: :string,
7
+ email: :string)
8
+
9
+ Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
10
+ Factory.define :user do |factory|
11
+ factory.first_name { 'Bill' }
12
+ factory.last_name { 'Nye' }
13
+ factory.email { Factory.next(:email) }
14
+ end
15
+ end
16
+
17
+ describe "after making an instance" do
18
+ before do
19
+ @instance = Factory(:user, last_name: 'Rye')
20
+ end
21
+
22
+ it "should use attributes from the definition" do
23
+ @instance.first_name.should == 'Bill'
24
+ end
25
+
26
+ it "should evaluate attribute blocks for each instance" do
27
+ @instance.email.should =~ /somebody\d+@example.com/
28
+ Factory(:user).email.should_not == @instance.email
29
+ end
30
+ end
31
+
32
+ it "raises Factory::SequenceAbuseError" do
33
+ Factory.define :sequence_abuser, class: User do |factory|
34
+ factory.first_name { Factory.sequence(:name) }
35
+ end
36
+
37
+ expect {
38
+ Factory(:sequence_abuser)
39
+ }.to raise_error(FactoryGirl::SequenceAbuseError)
40
+ end
41
+ end
42
+
43
+ describe Factory, "referencing a nonexistent factory as a parent" do
44
+ it "raises an ArgumentError when trying to use a non-existent factory as parent" do
45
+ expect {
46
+ Factory.define(:child, parent: :nonexsitent) {}
47
+ Factory.build(:child)
48
+ }.to raise_error(ArgumentError)
49
+ end
50
+ end
51
+
52
+ describe "defining a factory" do
53
+ before do
54
+ @name = :user
55
+ @factory = stub("factory", names: [@name])
56
+ @proxy = "proxy"
57
+ @options = { class: 'magic' }
58
+ FactoryGirl::Factory.stubs(new: @factory)
59
+ FactoryGirl::DefinitionProxy.stubs(new: @proxy)
60
+ end
61
+
62
+ it "creates a new factory using the specified name and options" do
63
+ FactoryGirl::Factory.stubs(new: @factory)
64
+ Factory.define(@name, @options) {|f| }
65
+ FactoryGirl::Factory.should have_received(:new).with(@name, @options)
66
+ end
67
+
68
+ it "passes the factory do the block" do
69
+ yielded = nil
70
+ Factory.define(@name) do |y|
71
+ yielded = y
72
+ end
73
+ yielded.should == @proxy
74
+ end
75
+
76
+ it "adds the factory to the list of factories" do
77
+ Factory.define(@name) {|f| }
78
+ @factory.should == FactoryGirl.factory_by_name(@name)
79
+ end
80
+ end
81
+
82
+ describe "after defining a factory" do
83
+ before do
84
+ @name = :user
85
+ @factory = FactoryGirl::Factory.new(@name)
86
+
87
+ FactoryGirl.register_factory(@factory)
88
+ end
89
+
90
+ it "uses Strategy::AttributesFor for Factory.attributes_for" do
91
+ @factory.stubs(run: "result")
92
+ Factory.attributes_for(@name, attr: 'value').should == 'result'
93
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::AttributesFor, attr: 'value')
94
+ end
95
+
96
+ it "uses Strategy::Build for Factory.build" do
97
+ @factory.stubs(run: "result")
98
+ Factory.build(@name, attr: 'value').should == 'result'
99
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Build, attr: 'value')
100
+ end
101
+
102
+ it "uses Strategy::Create for Factory.create" do
103
+ @factory.stubs(run: "result")
104
+ Factory.create(@name, attr: 'value').should == 'result'
105
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Create, attr: 'value')
106
+ end
107
+
108
+ it "uses Strategy::Stub for Factory.stub" do
109
+ @factory.stubs(run: "result")
110
+ Factory.stub(@name, attr: 'value').should == 'result'
111
+ @factory.should have_received(:run).with(FactoryGirl::Strategy::Stub, attr: 'value')
112
+ end
113
+
114
+ [:build, :create, :attributes_for, :stub].each do |method|
115
+ it "raises an ArgumentError on #{method} with a nonexistent factory" do
116
+ expect { Factory.send(method, :bogus) }.to raise_error(ArgumentError)
117
+ end
118
+
119
+ it "recognizes either 'name' or :name for Factory.#{method}" do
120
+ @factory.stubs(:run)
121
+ lambda { Factory.send(method, @name.to_s) }.should_not raise_error
122
+ lambda { Factory.send(method, @name.to_sym) }.should_not raise_error
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "defining a sequence" do
128
+ before do
129
+ @name = :count
130
+ end
131
+
132
+ it "creates a new sequence" do
133
+ Factory.sequence(@name)
134
+ Factory.next(@name).should == 1
135
+ end
136
+
137
+ it "uses the supplied block as the sequence generator" do
138
+ Factory.sequence(@name) {|n| "user-#{n}" }
139
+ Factory.next(@name).should == "user-1"
140
+ end
141
+
142
+ it "uses the supplied start_value as the sequence start_value" do
143
+ Factory.sequence(@name, "A")
144
+ Factory.next(@name).should == "A"
145
+ end
146
+ end
147
+
148
+ describe "after defining a sequence" do
149
+ before do
150
+ @name = :test
151
+ @sequence = FactoryGirl::Sequence.new(@name) {}
152
+ @value = '1 2 5'
153
+
154
+ @sequence.stubs(next: @value)
155
+ FactoryGirl::Sequence.stubs(new: @sequence)
156
+
157
+ Factory.sequence(@name) {}
158
+ end
159
+
160
+ it "calls next on the sequence when sent next" do
161
+ Factory.next(@name)
162
+ @sequence.should have_received(:next)
163
+ end
164
+
165
+ it "returns the value from the sequence" do
166
+ Factory.next(@name).should == @value
167
+ end
168
+ end
169
+
170
+ describe "an attribute generated by an in-line sequence" do
171
+ before do
172
+ define_model('User', username: :string)
173
+
174
+ Factory.define :user do |factory|
175
+ factory.sequence(:username) { |n| "username#{n}" }
176
+ end
177
+
178
+ @username = Factory.attributes_for(:user)[:username]
179
+ end
180
+
181
+ it "matches the correct format" do
182
+ @username.should =~ /^username\d+$/
183
+ end
184
+
185
+ describe "after the attribute has already been generated once" do
186
+ before do
187
+ @another_username = Factory.attributes_for(:user)[:username]
188
+ end
189
+
190
+ it "matches the correct format" do
191
+ @username.should =~ /^username\d+$/
192
+ end
193
+
194
+ it "is not the same as the first generated value" do
195
+ @another_username.should_not == @username
196
+ end
197
+ end
198
+ end
199
+
200
+
201
+ describe "a factory with a parent" do
202
+ before do
203
+ define_model("User", username: :string)
204
+
205
+ Factory.define(:user) do |factory|
206
+ factory.username "awesome_username"
207
+ end
208
+
209
+ Factory.define(:boring_user, parent: :user) do |factory|
210
+ factory.username "boring_username"
211
+ end
212
+ end
213
+
214
+ it "supports defining parents" do
215
+ Factory.build(:boring_user).username.should == "boring_username"
216
+ end
217
+ end
@@ -0,0 +1,421 @@
1
+ require "spec_helper"
2
+
3
+ describe "an instance generated by a factory with multiple traits" do
4
+ before do
5
+ define_model("User",
6
+ name: :string,
7
+ admin: :boolean,
8
+ gender: :string,
9
+ email: :string,
10
+ date_of_birth: :date,
11
+ great: :string)
12
+
13
+ FactoryGirl.define do
14
+ factory :user_without_admin_scoping, class: User do
15
+ admin_trait
16
+ end
17
+
18
+ factory :user do
19
+ name "John"
20
+
21
+ trait :great do
22
+ great "GREAT!!!"
23
+ end
24
+
25
+ trait :admin do
26
+ admin true
27
+ end
28
+
29
+ trait :admin_trait do
30
+ admin true
31
+ end
32
+
33
+ trait :male do
34
+ name "Joe"
35
+ gender "Male"
36
+ end
37
+
38
+ trait :female do
39
+ name "Jane"
40
+ gender "Female"
41
+ end
42
+
43
+ factory :great_user do
44
+ great
45
+ end
46
+
47
+ factory :admin, traits: [:admin]
48
+
49
+ factory :male_user do
50
+ male
51
+
52
+ factory :child_male_user do
53
+ date_of_birth { Date.parse("1/1/2000") }
54
+ end
55
+ end
56
+
57
+ factory :female, traits: [:female] do
58
+ trait :admin do
59
+ admin true
60
+ name "Judy"
61
+ end
62
+
63
+ factory :female_great_user do
64
+ great
65
+ end
66
+
67
+ factory :female_admin_judy, traits: [:admin]
68
+ end
69
+
70
+ factory :female_admin, traits: [:female, :admin]
71
+ factory :female_after_male_admin, traits: [:male, :female, :admin]
72
+ factory :male_after_female_admin, traits: [:female, :male, :admin]
73
+ end
74
+
75
+ trait :email do
76
+ email { "#{name}@example.com" }
77
+ end
78
+
79
+ factory :user_with_email, class: User, traits: [:email] do
80
+ name "Bill"
81
+ end
82
+ end
83
+ end
84
+
85
+ context "the parent class" do
86
+ subject { FactoryGirl.create(:user) }
87
+ its(:name) { should == "John" }
88
+ its(:gender) { should be_nil }
89
+ it { should_not be_admin }
90
+ end
91
+
92
+ context "the child class with one trait" do
93
+ subject { FactoryGirl.create(:admin) }
94
+ its(:name) { should == "John" }
95
+ its(:gender) { should be_nil }
96
+ it { should be_admin }
97
+ end
98
+
99
+ context "the other child class with one trait" do
100
+ subject { FactoryGirl.create(:female) }
101
+ its(:name) { should == "Jane" }
102
+ its(:gender) { should == "Female" }
103
+ it { should_not be_admin }
104
+ end
105
+
106
+ context "the child with multiple traits" do
107
+ subject { FactoryGirl.create(:female_admin) }
108
+ its(:name) { should == "Jane" }
109
+ its(:gender) { should == "Female" }
110
+ it { should be_admin }
111
+ end
112
+
113
+ context "the child with multiple traits and overridden attributes" do
114
+ subject { FactoryGirl.create(:female_admin, name: "Jill", gender: nil) }
115
+ its(:name) { should == "Jill" }
116
+ its(:gender) { should be_nil }
117
+ it { should be_admin }
118
+ end
119
+
120
+ context "the child with multiple traits who override the same attribute" do
121
+ context "when the male assigns name after female" do
122
+ subject { FactoryGirl.create(:male_after_female_admin) }
123
+ its(:name) { should == "Joe" }
124
+ its(:gender) { should == "Male" }
125
+ it { should be_admin }
126
+ end
127
+
128
+ context "when the female assigns name after male" do
129
+ subject { FactoryGirl.create(:female_after_male_admin) }
130
+ its(:name) { should == "Jane" }
131
+ its(:gender) { should == "Female" }
132
+ it { should be_admin }
133
+ end
134
+ end
135
+
136
+ context "child class with scoped trait and inherited trait" do
137
+ subject { FactoryGirl.create(:female_admin_judy) }
138
+ its(:name) { should == "Judy" }
139
+ its(:gender) { should == "Female" }
140
+ it { should be_admin }
141
+ end
142
+
143
+ context "factory using global trait" do
144
+ subject { FactoryGirl.create(:user_with_email) }
145
+ its(:name) { should == "Bill" }
146
+ its(:email) { should == "Bill@example.com"}
147
+ end
148
+
149
+ context "factory created with alternate syntax for specifying trait" do
150
+ subject { FactoryGirl.create(:male_user) }
151
+ its(:gender) { should == "Male" }
152
+ end
153
+
154
+ context "factory created with alternate syntax where trait name and attribute are the same" do
155
+ subject { FactoryGirl.create(:great_user) }
156
+ its(:great) { should == "GREAT!!!" }
157
+ end
158
+
159
+ context "factory created with alternate syntax where trait name and attribute are the same and attribute is overridden" do
160
+ subject { FactoryGirl.create(:great_user, great: "SORT OF!!!") }
161
+ its(:great) { should == "SORT OF!!!" }
162
+ end
163
+
164
+ context "child factory created where trait attributes are inherited" do
165
+ subject { FactoryGirl.create(:child_male_user) }
166
+ its(:gender) { should == "Male" }
167
+ its(:date_of_birth) { should == Date.parse("1/1/2000") }
168
+ end
169
+
170
+ context "factory outside of scope" do
171
+ subject { FactoryGirl.create(:user_without_admin_scoping) }
172
+ it { expect { subject }.to raise_error(ArgumentError, "Trait not registered: admin_trait") }
173
+ end
174
+
175
+ context "child factory using grandparents' trait" do
176
+ subject { FactoryGirl.create(:female_great_user) }
177
+ its(:great) { should == "GREAT!!!" }
178
+ end
179
+ end
180
+
181
+ describe "traits with callbacks" do
182
+ before do
183
+ define_model("User", name: :string)
184
+
185
+ FactoryGirl.define do
186
+ factory :user do
187
+ name "John"
188
+
189
+ trait :great do
190
+ after_create {|user| user.name.upcase! }
191
+ end
192
+
193
+ trait :awesome do
194
+ after_create {|user| user.name = "awesome" }
195
+ end
196
+
197
+ factory :caps_user, traits: [:great]
198
+ factory :awesome_user, traits: [:great, :awesome]
199
+
200
+ factory :caps_user_implicit_trait do
201
+ great
202
+ end
203
+ end
204
+ end
205
+ end
206
+
207
+ context "when the factory has a trait passed via arguments" do
208
+ subject { FactoryGirl.create(:caps_user) }
209
+ its(:name) { should == "JOHN" }
210
+ end
211
+
212
+ context "when the factory has an implicit trait" do
213
+ subject { FactoryGirl.create(:caps_user_implicit_trait) }
214
+ its(:name) { should == "JOHN" }
215
+ end
216
+
217
+ it "executes callbacks in the order assigned" do
218
+ FactoryGirl.create(:awesome_user).name.should == "awesome"
219
+ end
220
+ end
221
+
222
+ describe "traits added via strategy" do
223
+ before do
224
+ define_model("User", name: :string, admin: :boolean)
225
+
226
+ FactoryGirl.define do
227
+ factory :user do
228
+ name "John"
229
+
230
+ trait :admin do
231
+ admin true
232
+ end
233
+
234
+ trait :great do
235
+ after_create {|user| user.name.upcase! }
236
+ end
237
+ end
238
+ end
239
+ end
240
+
241
+ context "adding traits in create" do
242
+ subject { FactoryGirl.create(:user, :admin, :great, name: "Joe") }
243
+
244
+ its(:admin) { should be_true }
245
+ its(:name) { should == "JOE" }
246
+
247
+ it "doesn't modify the user factory" do
248
+ subject
249
+ FactoryGirl.create(:user).should_not be_admin
250
+ FactoryGirl.create(:user).name.should == "John"
251
+ end
252
+ end
253
+
254
+ context "adding traits in build" do
255
+ subject { FactoryGirl.build(:user, :admin, :great, name: "Joe") }
256
+
257
+ its(:admin) { should be_true }
258
+ its(:name) { should == "Joe" }
259
+ end
260
+
261
+ context "adding traits in attributes_for" do
262
+ subject { FactoryGirl.attributes_for(:user, :admin, :great) }
263
+
264
+ its([:admin]) { should be_true }
265
+ its([:name]) { should == "John" }
266
+ end
267
+
268
+ context "adding traits in build_stubbed" do
269
+ subject { FactoryGirl.build_stubbed(:user, :admin, :great, name: "Jack") }
270
+
271
+ its(:admin) { should be_true }
272
+ its(:name) { should == "Jack" }
273
+ end
274
+
275
+ context "adding traits in create_list" do
276
+ subject { FactoryGirl.create_list(:user, 2, :admin, :great, name: "Joe") }
277
+
278
+ its(:length) { should == 2 }
279
+
280
+ it "creates all the records" do
281
+ subject.each do |record|
282
+ record.admin.should be_true
283
+ record.name.should == "JOE"
284
+ end
285
+ end
286
+ end
287
+
288
+ context "adding traits in build_list" do
289
+ subject { FactoryGirl.build_list(:user, 2, :admin, :great, name: "Joe") }
290
+
291
+ its(:length) { should == 2 }
292
+
293
+ it "builds all the records" do
294
+ subject.each do |record|
295
+ record.admin.should be_true
296
+ record.name.should == "Joe"
297
+ end
298
+ end
299
+ end
300
+ end
301
+
302
+ describe "traits and dynamic attributes that are applied simultaneously" do
303
+ before do
304
+ define_model("User", name: :string, email: :string, combined: :string)
305
+
306
+ FactoryGirl.define do
307
+ trait :email do
308
+ email { "#{name}@example.com" }
309
+ end
310
+
311
+ factory :user do
312
+ name "John"
313
+ email
314
+ combined { "#{name} <#{email}>" }
315
+ end
316
+ end
317
+ end
318
+
319
+ subject { FactoryGirl.build(:user) }
320
+ its(:name) { should == "John" }
321
+ its(:email) { should == "John@example.com" }
322
+ its(:combined) { should == "John <John@example.com>" }
323
+ end
324
+
325
+ describe "applying inline traits" do
326
+ before do
327
+ define_model("User") do
328
+ has_many :posts
329
+ end
330
+
331
+ define_model("Post", user_id: :integer) do
332
+ belongs_to :user
333
+ end
334
+
335
+ FactoryGirl.define do
336
+ factory :user do
337
+ trait :with_post do
338
+ posts { [ Post.new ] }
339
+ end
340
+ end
341
+ end
342
+ end
343
+
344
+ it "applies traits only to the instance generated for that call" do
345
+ FactoryGirl.create(:user, :with_post).posts.should_not be_empty
346
+ FactoryGirl.create(:user).posts.should be_empty
347
+ FactoryGirl.create(:user, :with_post).posts.should_not be_empty
348
+ end
349
+ end
350
+
351
+ describe "inline traits overriding existing attributes" do
352
+ before do
353
+ define_model("User", status: :string)
354
+
355
+ FactoryGirl.define do
356
+ factory :user do
357
+ status "pending"
358
+
359
+ trait(:accepted) { status "accepted" }
360
+ trait(:declined) { status "declined" }
361
+
362
+ factory :declined_user, traits: [:declined]
363
+ factory :extended_declined_user, traits: [:declined] do
364
+ status "extended_declined"
365
+ end
366
+ end
367
+ end
368
+ end
369
+
370
+ it "returns the default status" do
371
+ FactoryGirl.build(:user).status.should == "pending"
372
+ end
373
+
374
+ it "prefers inline trait attributes over default attributes" do
375
+ FactoryGirl.build(:user, :accepted).status.should == "accepted"
376
+ end
377
+
378
+ it "prefers traits on a factory over default attributes" do
379
+ FactoryGirl.build(:declined_user).status.should == "declined"
380
+ end
381
+
382
+ it "prefers inline trait attributes over traits on a factory" do
383
+ FactoryGirl.build(:declined_user, :accepted).status.should == "accepted"
384
+ end
385
+
386
+ it "prefers attributes on factories over attributes from non-inline traits" do
387
+ FactoryGirl.build(:extended_declined_user).status.should == "extended_declined"
388
+ end
389
+
390
+ it "prefers inline traits over attributes on factories" do
391
+ FactoryGirl.build(:extended_declined_user, :accepted).status.should == "accepted"
392
+ end
393
+
394
+ it "prefers overridden attributes over attributes from traits, inline traits, or attributes on factories" do
395
+ FactoryGirl.build(:extended_declined_user, :accepted, status: "completely overridden").status.should == "completely overridden"
396
+ end
397
+ end
398
+
399
+ describe "making sure the factory is properly compiled the first time we want to instantiate it" do
400
+ before do
401
+ define_model("User", role: :string, gender: :string, age: :integer)
402
+
403
+ FactoryGirl.define do
404
+ factory :user do
405
+ trait(:female) { gender "female" }
406
+ trait(:admin) { role "admin" }
407
+
408
+ factory :female_user do
409
+ female
410
+ end
411
+ end
412
+ end
413
+ end
414
+
415
+ it "can honor traits on the very first call" do
416
+ user = FactoryGirl.build(:female_user, :admin, age: 30)
417
+ user.gender.should == 'female'
418
+ user.age.should == 30
419
+ user.role.should == 'admin'
420
+ end
421
+ end