Sha256: be76d2e92d3b53f1ddaf220779ae597f8a9063ad68ca031b5aa3616fc68570dc

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spy-1.0.5 test/integration/test_constant_spying.rb
spy-1.0.4 test/integration/test_constant_spying.rb
spy-1.0.3 test/integration/test_constant_spying.rb
spy-1.0.2 test/integration/test_constant_spying.rb
spy-1.0.1 test/integration/test_constant_spying.rb