File: stash_spec.rb

package info (click to toggle)
ruby-spy 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 360 kB
  • sloc: ruby: 3,101; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,117 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require 'spec_helper'

module Spy
  describe "only stashing the original method" do
    let(:klass) do
      Class.new do
        def self.foo(arg)
          :original_value
        end
      end
    end

    it "keeps the original method intact after multiple expectations are added on the same method" do
      spy = Spy.on(klass, :foo)
      klass.foo(:bazbar)
      expect(spy).to have_been_called
      Spy.off(klass, :foo)

      expect(klass.foo(:yeah)).to equal(:original_value)
    end
  end

  describe "when a class method is aliased on a subclass and the method is mocked" do
    let(:klass) do
      Class.new do
        class << self
          alias alternate_new new
        end
      end
    end

    it "restores the original aliased public method" do
      klass = Class.new do
        class << self
          alias alternate_new new
        end
      end

      spy = Spy.on(klass, :alternate_new)
      expect(klass.alternate_new).to be_nil
      expect(spy).to have_been_called

      Spy.off(klass, :alternate_new)
      expect(klass.alternate_new).to be_an_instance_of(klass)
    end
  end
end