Sha256: 645d548ed18a66900e0055e314d7ed232fcc7de6a5acb24b445a94f855bbf0bc

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'spec_helper'

describe StashedInstanceMethod 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 = StashedInstanceMethod.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 = StashedInstanceMethod.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 = StashedInstanceMethod.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

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/rspec-mocks-2.11.3/spec/rspec/mocks/stashed_instance_method_spec.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/rspec-mocks-2.11.3/spec/rspec/mocks/stashed_instance_method_spec.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/rspec-mocks-2.11.3/spec/rspec/mocks/stashed_instance_method_spec.rb
rspec-mocks-2.11.3 spec/rspec/mocks/stashed_instance_method_spec.rb