Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Subclass Module rather than having a giant builder method. #26

Merged
merged 1 commit into from almost 2 years ago

2 participants

Myron Marston Xavier Shay
Myron Marston
Collaborator

The old way I implemented fire_class_double used Module.new with a giant block that defined lots of singleton methods. @avdi's talk at GoGaRuCo inspired me to subclass Module instead.

I find this cleaner, although it is a bit more code.

Thoughts?

Xavier Shay
Owner

On the one hand I'm a little worried about doing something so non-standard, but on the other hand this code is totally nuts anyway :D Merging.

Xavier Shay xaviershay merged commit 308ef46 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 27, 2012
Myron Marston myronmarston Subclass Module rather than having a giant builder method. 5292734
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 30 additions and 26 deletions. Show diff stats Hide diff stats

  1. +30 26 lib/rspec/fire.rb
56 lib/rspec/fire.rb
@@ -271,39 +271,43 @@ def initialize(doubled_class, *args)
271 271 end
272 272 end
273 273
274   - class FireClassDoubleBuilder
275   - def self.build(doubled_class, stubs = {})
276   - Module.new do
277   - extend FireDoublable
  274 + class FireClassDouble < Module
  275 + include FireDoublable
278 276
279   - @__doubled_class_name = doubled_class
280   - @__checked_methods = :public_methods
281   - @__method_finder = :method
  277 + def initialize(doubled_class, stubs = {})
  278 + @__doubled_class_name = doubled_class
  279 + @__checked_methods = :public_methods
  280 + @__method_finder = :method
282 281
283   - verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
  282 + verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
284 283
285   - ::RSpec::Mocks::TestDouble.extend_onto self,
286   - doubled_class, stubs.merge(:__declared_as => "FireClassDouble")
  284 + ::RSpec::Mocks::TestDouble.extend_onto self,
  285 + doubled_class, stubs.merge(:__declared_as => "FireClassDouble")
287 286
288   - def self.as_replaced_constant(options = {})
289   - RSpec::Mocks::ConstantStubber.stub(@__doubled_class_name, self, options)
290   - @__original_class = RSpec::Mocks::Constant.original(@__doubled_class_name).original_value
  287 + # This needs to come after `::RSpec::Mocks::TestDouble.extend_onto`
  288 + # so that it gets precedence...
  289 + extend StringRepresentations
  290 + end
291 291
292   - extend AsReplacedConstant
293   - self
294   - end
  292 + def as_replaced_constant(options = {})
  293 + RSpec::Mocks::ConstantStubber.stub(@__doubled_class_name, self, options)
  294 + @__original_class = RSpec::Mocks::Constant.original(@__doubled_class_name).original_value
295 295
296   - def self.to_s
297   - @__doubled_class_name + " (fire double)"
298   - end
  296 + extend AsReplacedConstant
  297 + self
  298 + end
299 299
300   - def self.inspect
301   - to_s
302   - end
  300 + def name
  301 + @__doubled_class_name
  302 + end
303 303
304   - def self.name
305   - @__doubled_class_name
306   - end
  304 + module StringRepresentations
  305 + def to_s
  306 + @__doubled_class_name + " (fire double)"
  307 + end
  308 +
  309 + def inspect
  310 + to_s
307 311 end
308 312 end
309 313
@@ -324,7 +328,7 @@ def fire_double(*args)
324 328 end
325 329
326 330 def fire_class_double(*args)
327   - FireClassDoubleBuilder.build(*args)
  331 + FireClassDouble.new(*args)
328 332 end
329 333
330 334 def fire_replaced_class_double(*args)

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.