Sha256: 5ecb85eae73fb4e6e9394d05467a5f7886a85089525c18e1c35723a6ce9324d8

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

require 'test_helper'

class TestConstantSpying < MiniTest::Unit::TestCase

  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_equal 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_equal 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_equal 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

9 entries across 9 versions & 1 rubygems

Version Path
spy-0.4.1 test/integration/test_constant_spying.rb
spy-0.4.0 test/integration/test_constant_spying.rb
spy-0.3.1 test/integration/test_constant_spying.rb
spy-0.3.0 test/integration/test_constant_spying.rb
spy-0.2.5 test/integration/test_constant_spying.rb
spy-0.2.4 test/integration/test_constant_spying.rb
spy-0.2.3 test/integration/test_constant_spying.rb
spy-0.2.2 test/integration/test_constant_spying.rb
spy-0.2.1 test/integration/test_constant_spying.rb