Sha256: bc97d453d253f01e76cd0cf55b100ebb8ea9de30ce9c4f97091f533bf0572cdf

Contents?: true

Size: 1.66 KB

Versions: 51

Compression:

Stored size: 1.66 KB

Contents

require 'spec_helper'

module RSpec
  module Mocks
    describe InstanceMethodStasher do
      class ExampleClass
        def hello
          :hello_defined_on_class
        end
      end

      def singleton_class_for(obj)
        class << obj; self; end
      end

      it "stashes the current implementation of an instance method so it can be temporarily replaced" do
        obj = Object.new
        def obj.hello; :hello_defined_on_singleton_class; end;

        stashed_method = InstanceMethodStasher.new(singleton_class_for(obj), :hello)
        stashed_method.stash

        def obj.hello; :overridden_hello; end
        expect(obj.hello).to eql :overridden_hello

        stashed_method.restore
        expect(obj.hello).to eql :hello_defined_on_singleton_class
      end

      it "stashes private instance methods" do
        obj = Object.new
        def obj.hello; :hello_defined_on_singleton_class; end;
        singleton_class_for(obj).__send__(:private, :hello)

        stashed_method = InstanceMethodStasher.new(singleton_class_for(obj), :hello)
        stashed_method.stash

        def obj.hello; :overridden_hello; end
        stashed_method.restore
        expect(obj.send(:hello)).to eql :hello_defined_on_singleton_class
      end

      it "only stashes methods directly defined on the given class, not its ancestors" do
        obj = ExampleClass.new

        stashed_method = InstanceMethodStasher.new(singleton_class_for(obj), :hello)
        stashed_method.stash

        def obj.hello; :overridden_hello; end;
        expect(obj.hello).to eql :overridden_hello

        stashed_method.restore
        expect(obj.hello).to eql :overridden_hello
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 13 rubygems

Version Path
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-unbundled-1.8.1.1 vendor/bundle/ruby/2.3.0/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.99.4 spec/rspec/mocks/instance_method_stasher_spec.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/rspec-mocks-2.14.6/spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.99.3 spec/rspec/mocks/instance_method_stasher_spec.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/rspec-mocks-2.13.0/spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.99.2 spec/rspec/mocks/instance_method_stasher_spec.rb
dxruby_rp5-0.0.4 spec/vendor/rspec-mocks-2.14.4/spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.99.1 spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.99.0 spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.99.0.rc1 spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.14.6 spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.99.0.beta2 spec/rspec/mocks/instance_method_stasher_spec.rb
rspec-mocks-2.14.5 spec/rspec/mocks/instance_method_stasher_spec.rb