0

我正在尝试覆盖/修改 Test::Unit::TestCase 测试的拆解功能。在测试拆解期间(完成后),我想做一些额外的事情。

我试过这个,但它不起作用(继续执行原来的拆解):

module Test
  module Unit
    class TestCase
        def teardown_modified
          # do modifications
          teardown_original
        end

        alias teardown_original teardown
        alias teardown teardown_modified
      end
  end
end
4

2 回答 2

1

你想要它在一个 TestCase 中还是全部?

如果您需要更改所有测试用例:

gem 'test-unit'
require 'test/unit'

module Test
  module Unit
    module Fixture
        alias :run_teardown_old :run_teardown
        def run_teardown
          # do modifications
          puts "In modified teardown"
          run_teardown_old
        end #def run_teardown
      end #module Fixture
  end #module Unit
end #module Test

class MyTest < Test::Unit::TestCase
  def teardown
    puts "In teardown"
  end

  def test_4()
    assert_equal(2,1+1)
  end
end
于 2011-08-05T21:04:22.907 回答
1

您可能会发现 usingalias_method_chain会产生更好的结果:

class Test::Unit::TestCase
  def teardown_with_hacks
    teardown_without_hacks
  end
  alias_method_chain :teardown, :hacks
end

这会自动为您设置很多东西。

于 2011-08-05T21:06:05.947 回答