Permalink
Browse files

Decide whether to stash/restore a method based on existence of the

method, not the result of respond_to? (more reliable).
  • Loading branch information...
1 parent 37864c1 commit b599a42e68738c1b7e04e855735cd368283775f6 @dchelimsky dchelimsky committed Jul 5, 2010
Showing with 82 additions and 56 deletions.
  1. +5 −7 lib/rspec/mocks/method_double.rb
  2. +77 −49 spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb
@@ -64,14 +64,12 @@ def configure_method
end
def stash_original_method
- if object_responds_to?(@method_name)
- stashed = stashed_method_name
- orig = @method_name
- object_singleton_class.class_eval do
- alias_method(stashed, orig) if method_defined?(orig) || private_method_defined?(orig)
- end
- @stashed = true
+ stashed = stashed_method_name
+ orig = @method_name
+ object_singleton_class.class_eval do
+ alias_method(stashed, orig) if method_defined?(orig) || private_method_defined?(orig)
end
+ @stashed = true
end
def define_proxy_method
@@ -1,66 +1,94 @@
require 'spec_helper'
-module RSpec
-module Mocks
-describe "PartialMockUsingMocksDirectly" do
- before(:each) do
-
- klass=Class.new
- klass.class_eval do
- def existing_method
- :original_value
+module RSpec::Mocks
+ describe "PartialMockUsingMocksDirectly" do
+ let(:klass) do
+ Class.new do
+ module MethodMissing
+ def method_missing(m, *a, &b)
+ if m == :captured_by_method_missing
+ "response generated by method missing"
+ else
+ super(m, *a, &b)
+ end
end
end
- @obj = klass.new
-
+
+ extend MethodMissing
+ include MethodMissing
+
+ def existing_method
+ :original_value
+ end
+
+ end
end
-
+
+ let(:obj) { klass.new }
+
# See http://rubyforge.org/tracker/index.php?func=detail&aid=10263&group_id=797&atid=3149
# specify "should clear expectations on verify" do
- # @obj.should_receive(:msg)
- # @obj.msg
- # @obj.rspec_verify
+ # obj.should_receive(:msg)
+ # obj.msg
+ # obj.rspec_verify
# lambda do
- # @obj.msg
+ # obj.msg
# end.should raise_error(NoMethodError)
#
# end
- it "should fail when expected message is not received" do
- @obj.should_receive(:msg)
- lambda do
- @obj.rspec_verify
- end.should raise_error(RSpec::Mocks::MockExpectationError)
-
+ it "fails when expected message is not received" do
+ obj.should_receive(:msg)
+ lambda do
+ obj.rspec_verify
+ end.should raise_error(RSpec::Mocks::MockExpectationError)
+ end
+
+ it "fails when message is received with incorrect args" do
+ obj.should_receive(:msg).with(:correct_arg)
+ lambda do
+ obj.msg(:incorrect_arg)
+ end.should raise_error(RSpec::Mocks::MockExpectationError)
+ obj.msg(:correct_arg)
end
- it "should fail when message is received with incorrect args" do
- @obj.should_receive(:msg).with(:correct_arg)
- lambda do
- @obj.msg(:incorrect_arg)
- end.should raise_error(RSpec::Mocks::MockExpectationError)
- @obj.msg(:correct_arg)
-
+
+ it "passes when expected message is received" do
+ obj.should_receive(:msg)
+ obj.msg
+ obj.rspec_verify
+ end
+
+ it "passes when message is received with correct args" do
+ obj.should_receive(:msg).with(:correct_arg)
+ obj.msg(:correct_arg)
+ obj.rspec_verify
end
- it "should pass when expected message is received" do
- @obj.should_receive(:msg)
- @obj.msg
- @obj.rspec_verify
-
+
+ it "should restore original method if existed" do
+ obj.existing_method.should equal(:original_value)
+ obj.should_receive(:existing_method).and_return(:mock_value)
+ obj.existing_method.should equal(:mock_value)
+ obj.rspec_verify
+ obj.existing_method.should equal(:original_value)
end
- it "should pass when message is received with correct args" do
- @obj.should_receive(:msg).with(:correct_arg)
- @obj.msg(:correct_arg)
- @obj.rspec_verify
-
+
+ context "with an instance method handled by method_missing" do
+ it "restores the original behavior" do
+ obj.captured_by_method_missing.should eq("response generated by method missing")
+ obj.stub(:captured_by_method_missing) { "foo" }
+ obj.captured_by_method_missing.should eq("foo")
+ obj.rspec_reset
+ obj.captured_by_method_missing.should eq("response generated by method missing")
+ end
end
- it "should revert to original method if existed" do
- @obj.existing_method.should equal(:original_value)
- @obj.should_receive(:existing_method).and_return(:mock_value)
- @obj.existing_method.should equal(:mock_value)
- @obj.rspec_verify
- @obj.existing_method.should equal(:original_value)
-
+
+ context "with a class method handled by method_missing" do
+ it "restores the original behavior" do
+ klass.captured_by_method_missing.should eq("response generated by method missing")
+ klass.stub(:captured_by_method_missing) { "foo" }
+ klass.captured_by_method_missing.should eq("foo")
+ klass.rspec_reset
+ klass.captured_by_method_missing.should eq("response generated by method missing")
+ end
end
-
-end
-end
+ end
end

0 comments on commit b599a42

Please sign in to comment.