代码之家  ›  专栏  ›  技术社区  ›  Benjamin Oakes

确保在Test::Unit::TestCase中运行拆卸?

  •  2
  • Benjamin Oakes  · 技术社区  · 16 年前

    Test::Unit::TestCase 编写一些单元测试。目前,我有一个 setup 移动和修改磁盘上某些夹具文件和文件夹的函数(目前这是一个必要的问题。)如果测试崩溃 teardown Errno::EEXIST

    我该怎么办 ensure 拆卸 确保 finally 在其他一些语言中。)

    2 回复  |  直到 16 年前
        1
  •  3
  •   philant    16 年前

    做你的工作怎么样 setup() 进行一些清理,以便即使文件仍然存在也能正常工作?

        2
  •  1
  •   James A. Rosen    16 年前

    在中添加一个on-exit钩子怎么样 setup ,然后将其从中移除 teardown :

    class MyTestCase < Test::Unit::TestCase
    
      def clean_up!
        ...
      end
    
      def setup
        at_exit do
          unless @cleaned_up
            clean_up!
          end
        end
      end
    
      def teardown
        clean_up!
        @cleaned_up = true
      end
    
    end