Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #15 from myronmarston/allow_unloaded_class_and_ins…

…tance_doubling

Allow both instance and class doubles for unloaded classes.
  • Loading branch information...
commit d691b0a30ad154e234f76051ed9e79dc10d16b60 2 parents ee88a5d + 02f3150
@xaviershay authored
Showing with 31 additions and 5 deletions.
  1. +9 −5 lib/rspec/fire.rb
  2. +22 −0 spec/fire_double_spec.rb
View
14 lib/rspec/fire.rb
@@ -135,9 +135,12 @@ def stub!(method_name)
end
def with_doubled_class
- if original_stubbed_const_value = ConstantStubber.original_value_for(@__doubled_class_name)
- yield original_stubbed_const_value
- elsif recursive_const_defined?(@__doubled_class_name)
+ ConstantStubber.find_original_value_for(@__doubled_class_name) do |value|
+ yield value if value
+ return
+ end
+
+ if recursive_const_defined?(@__doubled_class_name)
yield recursive_const_get(@__doubled_class_name)
end
end
@@ -390,9 +393,10 @@ def self.stubbers
@stubbers ||= []
end
- def self.original_value_for(constant_name)
+ def self.find_original_value_for(constant_name)
stubber = stubbers.find { |s| s.full_constant_name == constant_name }
- stubber.original_value if stubber
+ yield stubber.original_value if stubber
+ self
end
end
View
22 spec/fire_double_spec.rb
@@ -270,6 +270,28 @@ def use_doubles(class_double, instance_double)
double.should_receive(:foo).with("a").and_return(:bar)
A::B::C.foo("a").should eq(:bar)
end
+
+ def use_doubles(class_double, instance_double)
+ instance_double.should_receive(:undefined_method).and_return(3)
+ class_double.should_receive(:undefined_method).and_return(4)
+
+ instance_double.undefined_method.should eq(3)
+ class_double.undefined_method.should eq(4)
+ end
+
+ it 'can be used after a declared fire_double for the same class' do
+ instance_double = fire_double("A::B::C")
+ class_double = fire_replaced_class_double("A::B::C")
+
+ use_doubles class_double, instance_double
+ end
+
+ it 'can be used before a declared fire_double for the same class' do
+ class_double = fire_replaced_class_double("A::B::C")
+ instance_double = fire_double("A::B::C")
+
+ use_doubles class_double, instance_double
+ end
end
shared_examples_for "loaded constant stubbing" do |const_name|

0 comments on commit d691b0a

Please sign in to comment.
Something went wrong with that request. Please try again.