File: test_constant_spying.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 (58 lines) | stat: -rw-r--r-- 1,161 bytes parent folder | download
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
48
49
50
51
52
53
54
55
56
57
58
require 'test_helper'

class TestConstantSpying < Minitest::Test

  class Foo
    HELLO = "hello world"

    def self.hello
      HELLO
    end

    module Bar
      def self.hello
        HELLO
      end
    end
  end

  class ChildFoo < Foo
    def self.hello
      HELLO
    end
  end

  def teardown
    Spy::Agency.instance.dissolve!
  end

  def test_spy_on_constant
    assert_equal "hello world", Foo.hello

    spy = Spy.on_const(Foo, :HELLO)
    assert_nil Foo.hello
    spy.and_return("awesome")
    assert_equal "awesome", Foo.hello

    Spy.off_const(Foo, :HELLO)
    assert_equal "hello world", Foo.hello

    assert_equal "hello world", Foo::Bar.hello
    spy = Spy.on_const(Foo, :HELLO)
    assert_nil Foo::Bar.hello
    spy.and_return("awesome")
    assert_equal "awesome", Foo::Bar.hello

    Spy.off_const(Foo, :HELLO)
    assert_equal "hello world", Foo::Bar.hello

    assert_equal "hello world", ChildFoo.hello
    spy = Spy.on_const(Foo, :HELLO)
    assert_nil ChildFoo.hello
    spy.and_return("awesome")
    assert_equal "awesome", ChildFoo.hello

    Spy.off_const(Foo, :HELLO)
    assert_equal "hello world", ChildFoo.hello
  end
end