【问题标题】:calling single tests works but not running all tests in MiniTest调用单个测试有效,但不能在 MiniTest 中运行所有测试
【发布时间】:2014-10-12 14:29:30
【问题描述】:

我在 jruby-1.7.13 中从 Test Unit 切换到 MiniTest。我也使用摩卡/集成。我的问题是运行“rake test”会引发 Mocha::ExpectationError:意外调用:MyClass.new。使用 MyClass.new 在 lib 文件夹中定义一个类并在测试类中使用。我发现单独运行测试效果很好。它看起来像一个时间或顺序问题。我试图通过使用 setup/teardown 在每次测试之前启动 MyClass.new 来规避它,但这无济于事。我必须模拟/存根 MyClass.new 吗?

【问题讨论】:

    标签: unit-testing rake jruby minitest


    【解决方案1】:

    假设您使用的是 Rails 4,您需要将以下内容添加到您的 config/application.rb

    config.autoload_paths += %W(#{config.root}/lib)
    

    杀死 Spring/Zeus/Spork,重新运行测试,它应该可以工作。

    【讨论】:

    • 我不使用 Rails。我只运行纯 Ruby/JRuby 没有 web 应用程序。
    • 我不明白你的代码行是做什么的。是否有一些配置可以限制可以运行多少个测试?
    • 为了更清楚:在切换到 MiniTest 之前,我可以在我的项目中运行所有测试(其中数百个在 test/lib 下的子文件夹中)。现在连test目录下的子文件夹都跑不通了。
    • 该行是特定于 Rails 的配置,需要自动加载 lib/ 目录中的类,因此它不会帮助您解决问题。
    • 您能否提供您的test_helper.rb 和失败的测试用例的要点?不看代码就无法猜测可能是什么问题。
    【解决方案2】:

    helper.rb 示例代码...

    require 'rubygems'
    require 'bundler'
    
    begin
      Bundler.setup(:default, :development)
    rescue Bundler::BundlerError => e
      $stderr.puts e.message
      $stderr.puts "Run `bundle install` to install missing gems"
      exit e.status_code
    end
    
    require 'minitest/spec'
    require 'minitest/autorun'
    require 'mocha/integration'
    
    $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
    $LOAD_PATH.unshift(File.dirname(__FILE__))
    require 'dibta-modbus-adapter'
    require 'dibta-interface'
    
    class MiniTest::Test
      def test_order
        :alpha
      end
      def self.runnable_methods
        methods = methods_matching(/^test_/)
    
        case self.test_order
        when :random, :parallel then
          max = methods.size
          methods.sort.sort_by { rand max }
        when :alpha, :sorted then
          methods.sort   ## note: Runnable is shuffeled in minitest.rb
        else
          raise "Unknown test_order: #{self.test_order.inspect}"
        end
      end
    end
    

    如你所见,我开始修改 test_order 和 runnable_methods ....

    这取决于测试失败的种子“编号”。在某些情况下(具有特殊种子编号),测试甚至可以通过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2017-10-12
      • 2015-01-09
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2013-07-29
      相关资源
      最近更新 更多