Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Merged
merged 1 commit into from

2 participants

@myronmarston
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?

@xaviershay
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.

@xaviershay xaviershay merged commit 308ef46 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 30 additions and 26 deletions.
  1. +30 −26 lib/rspec/fire.rb
View
56 lib/rspec/fire.rb
@@ -271,39 +271,43 @@ def initialize(doubled_class, *args)
end
end
- class FireClassDoubleBuilder
- def self.build(doubled_class, stubs = {})
- Module.new do
- extend FireDoublable
+ class FireClassDouble < Module
+ include FireDoublable
- @__doubled_class_name = doubled_class
- @__checked_methods = :public_methods
- @__method_finder = :method
+ def initialize(doubled_class, stubs = {})
+ @__doubled_class_name = doubled_class
+ @__checked_methods = :public_methods
+ @__method_finder = :method
- verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
+ verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
- ::RSpec::Mocks::TestDouble.extend_onto self,
- doubled_class, stubs.merge(:__declared_as => "FireClassDouble")
+ ::RSpec::Mocks::TestDouble.extend_onto self,
+ doubled_class, stubs.merge(:__declared_as => "FireClassDouble")
- def self.as_replaced_constant(options = {})
- RSpec::Mocks::ConstantStubber.stub(@__doubled_class_name, self, options)
- @__original_class = RSpec::Mocks::Constant.original(@__doubled_class_name).original_value
+ # This needs to come after `::RSpec::Mocks::TestDouble.extend_onto`
+ # so that it gets precedence...
+ extend StringRepresentations
+ end
- extend AsReplacedConstant
- self
- end
+ def as_replaced_constant(options = {})
+ RSpec::Mocks::ConstantStubber.stub(@__doubled_class_name, self, options)
+ @__original_class = RSpec::Mocks::Constant.original(@__doubled_class_name).original_value
- def self.to_s
- @__doubled_class_name + " (fire double)"
- end
+ extend AsReplacedConstant
+ self
+ end
- def self.inspect
- to_s
- end
+ def name
+ @__doubled_class_name
+ end
- def self.name
- @__doubled_class_name
- end
+ module StringRepresentations
+ def to_s
+ @__doubled_class_name + " (fire double)"
+ end
+
+ def inspect
+ to_s
end
end
@@ -324,7 +328,7 @@ def fire_double(*args)
end
def fire_class_double(*args)
- FireClassDoubleBuilder.build(*args)
+ FireClassDouble.new(*args)
end
def fire_replaced_class_double(*args)
Something went wrong with that request. Please try again.