开发者

Overwriting Rails Test Test::Unit::TestCase

I am trying to overwrite/modify the teardown function of a Test::Unit::TestCase test. During the teardown of t开发者_如何学Gohe test (after it has finished), I want to do some extra stuff.

I tried this, but it doesn't work (keeps executing the original teardown):

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


Do you want it in one TestCase or in all?

If you need a change for all TestCases:

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


You might find that using alias_method_chain produces better results:

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

This sets up a lot of the stuff for you automatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜