public
Description: Who's your daddy? Kill Rails fixtures, Don't Repeat Yourself, reduce the complexity of your tests.
Homepage: http://tasks.ogtastic.com/projects/show/object-daddy
Clone URL: git://github.com/flogic/object_daddy.git
Fixed major bug with inheritance.

The only check for whether or not to re-load the exemplars was in .spawn, but 
.gather_exemplars would go up the inheritance chain to get generators from 
parent classes. This caused some problems if a parent class had already gathered 
exemplars and then a child class caused it to re-gather them. (It was fine if 
the child class happened first.)

Moved the check to .gather_exemplars, added/moved specs, un-protected 
.gather_exemplars.
ymendel (author)
Thu Apr 17 13:43:56 -0700 2008
commit  582aca6712f34ca045a19dc573accee08d433962
tree    140e7c89702a7de156089858015624c08b567b42
parent  d048dbc9a56056bb1084e0aa52e8932121696457
...
19
20
21
22
 
23
24
25
...
88
89
90
91
92
93
 
94
95
96
...
100
101
102
103
 
 
 
104
105
106
...
19
20
21
 
22
23
24
25
...
88
89
90
 
 
91
92
93
94
95
...
99
100
101
 
102
103
104
105
106
107
0
@@ -19,7 +19,7 @@ module ObjectDaddy
0
     
0
     # create a valid instance of this class, using any known generators
0
     def spawn(args = {})
0
-      gather_exemplars unless exemplars_generated
0
+      gather_exemplars
0
       (generators || {}).each_pair do |handle, gen_data|
0
         next if args[handle]
0
         generator = gen_data[:generator]
0
@@ -88,9 +88,8 @@ module ObjectDaddy
0
       end
0
     end
0
     
0
-  protected
0
-    
0
     def gather_exemplars
0
+      return if exemplars_generated
0
       if superclass.respond_to?(:gather_exemplars)
0
         superclass.gather_exemplars
0
         self.generators = (superclass.generators || {}).dup
0
@@ -100,7 +99,9 @@ module ObjectDaddy
0
       load(path) if File.exists?(path)
0
       self.exemplars_generated = true
0
     end
0
-    
0
+  
0
+  protected
0
+  
0
     # we define an underscore helper ourselves since the ActiveSupport isn't available if we're not using Rails
0
     def underscore(string)
0
       string.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
...
148
149
150
151
152
 
 
153
154
155
...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
181
182
183
184
185
186
187
188
189
190
191
192
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
195
196
197
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
200
201
...
148
149
150
 
 
151
152
153
154
155
...
158
159
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
 
 
 
 
 
 
 
 
 
 
 
 
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
0
@@ -148,8 +148,8 @@ describe ObjectDaddy, 'recording the registration of a generator method' do
0
   end
0
 end
0
 
0
-describe ObjectDaddy, "when spawning a class instance" do
0
-  before(:each) do
0
+describe ObjectDaddy, 'when registering exemplars' do
0
+  before :each do
0
     @class = Class.new(OpenStruct)
0
     @class.send(:include, ObjectDaddy)
0
     @file_path = File.join(File.dirname(__FILE__), 'tmp')
0
@@ -158,44 +158,97 @@ describe ObjectDaddy, "when spawning a class instance" do
0
     @class.stubs(:name).returns('Widget')
0
   end
0
   
0
-  it "should register exemplars for the target class on the first attempt" do
0
-    @class.expects(:gather_exemplars)
0
-    @class.spawn
0
-  end
0
-  
0
-  it "should not register exemplars for the target class after the first attempt" do
0
-    @class.spawn
0
-    @class.expects(:gather_exemplars).never
0
-    @class.spawn
0
-  end
0
-
0
-  it "should look for exemplars for the target class in the standard exemplar path" do
0
-    @class.expects(:exemplar_path).returns(@file_path)
0
-    @class.spawn
0
-  end
0
-  
0
-  it "should look for an exemplar for the target class, based on the class's name" do
0
-    @class.expects(:name).returns('Widget')
0
-    @class.spawn
0
+  describe 'before exemplars have been registered' do
0
+    before :each do
0
+      @class.stubs(:exemplars_generated).returns(false)
0
+    end
0
+    
0
+    it "should look for exemplars for the target class in the standard exemplar path" do
0
+      @class.expects(:exemplar_path).returns(@file_path)
0
+      @class.gather_exemplars
0
+    end
0
+    
0
+    it "should look for an exemplar for the target class, based on the class's name" do
0
+      @class.expects(:name).returns('Widget')
0
+      @class.gather_exemplars
0
+    end
0
+    
0
+    it "should register any generators found in the exemplar for the target class" do 
0
+      # we are using the concrete Widget class here because otherwise it's difficult to have our exemplar file work in our class
0
+      begin
0
+        # a dummy class, useful for testing the actual loading of exemplar files
0
+        Widget = Class.new(OpenStruct) { include ObjectDaddy }
0
+        File.open(@file_name, 'w') {|f| f.puts "class Widget\ngenerator_for :foo\nend\n"}
0
+        Widget.stubs(:exemplar_path).returns(@file_path)
0
+        Widget.expects(:generator_for)
0
+        Widget.gather_exemplars
0
+      ensure
0
+        # clean up test data file
0
+        File.unlink(@file_name) if File.exists?(@file_name)
0
+      end
0
+    end
0
+    
0
+    it 'should record that exemplars have been registered' do
0
+      @class.expects(:exemplars_generated=).with(true)
0
+      @class.gather_exemplars
0
+    end
0
   end
0
   
0
-  it "should register any generators found in the exemplar for the target class" do 
0
-    # we are using the concrete Widget class here because otherwise it's difficult to have our exemplar file work in our class
0
-    begin
0
-      # a dummy class, useful for testing the actual loading of exemplar files
0
-      Widget = Class.new(OpenStruct) { include ObjectDaddy }
0
-      File.open(@file_name, 'w') {|f| f.puts "class Widget\ngenerator_for :foo\nend\n"}
0
-      Widget.stubs(:exemplar_path).returns(@file_path)
0
-      Widget.expects(:generator_for)
0
-      Widget.spawn
0
-    ensure
0
-      # clean up test data file
0
-      File.unlink(@file_name) if File.exists?(@file_name)
0
+  describe 'after exemplars have been registered' do
0
+    before :each do
0
+      @class.stubs(:exemplars_generated).returns(true)
0
+    end
0
+    
0
+    it "should not look for exemplars for the target class in the standard exemplar path" do
0
+      @class.expects(:exemplar_path).never
0
+      @class.gather_exemplars
0
+    end
0
+    
0
+    it "should not look for an exemplar for the target class, based on the class's name" do
0
+      @class.expects(:name).never
0
+      @class.gather_exemplars
0
+    end
0
+    
0
+    it 'should register no generators' do
0
+      # we are using the concrete Widget class here because otherwise it's difficult to have our exemplar file work in our class
0
+      begin
0
+        # a dummy class, useful for testing the actual loading of exemplar files
0
+        Widget = Class.new(OpenStruct) { include ObjectDaddy }
0
+        File.open(@file_name, 'w') {|f| f.puts "class Widget\ngenerator_for :foo\nend\n"}
0
+        Widget.stubs(:exemplar_path).returns(@file_path)
0
+        Widget.stubs(:exemplars_generated).returns(true)
0
+        Widget.expects(:generator_for).never
0
+        Widget.gather_exemplars
0
+      ensure
0
+        # clean up test data file
0
+        File.unlink(@file_name) if File.exists?(@file_name)
0
+      end
0
+    end
0
+    
0
+    it 'should not record that exemplars have been registered' do
0
+      @class.expects(:exemplars_generated=).never
0
+      @class.gather_exemplars
0
     end
0
   end
0
   
0
   it "should register no generators if no exemplar for the target class is available" do
0
     @class.expects(:generator_for).never
0
+    @class.gather_exemplars
0
+  end
0
+end
0
+
0
+describe ObjectDaddy, "when spawning a class instance" do
0
+  before(:each) do
0
+    @class = Class.new(OpenStruct)
0
+    @class.send(:include, ObjectDaddy)
0
+    @file_path = File.join(File.dirname(__FILE__), 'tmp')
0
+    @file_name = File.join(@file_path, 'widget_exemplar.rb')
0
+    @class.stubs(:exemplar_path).returns(@file_path)
0
+    @class.stubs(:name).returns('Widget')
0
+  end
0
+  
0
+  it "should register exemplars for the target class" do
0
+    @class.expects(:gather_exemplars)
0
     @class.spawn
0
   end
0
   

Comments