首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖Rails Test Test::Unit::TestCase

覆盖Rails Test Test::Unit::TestCase
EN

Stack Overflow用户
提问于 2011-08-06 04:30:20
回答 2查看 186关注 0票数 0

我正在尝试覆盖/修改Test::Unit::TestCase测试的teardown函数。在测试的拆卸过程中(在测试结束后),我想做一些额外的事情。

我试过了,但它不起作用(继续执行原始的teardown):

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-06 05:04:23

你想把它放在一个TestCase里还是放在所有地方?

如果您需要更改所有TestCases:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2011-08-06 05:06:06

您可能会发现使用alias_method_chain会产生更好的结果:

代码语言:javascript
复制
class Test::Unit::TestCase
  def teardown_with_hacks
    teardown_without_hacks
  end
  alias_method_chain :teardown, :hacks
end

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6962265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档