Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 调用单个测试可以工作,但不能在MiniTest中运行所有测试_Unit Testing_Rake_Jruby_Minitest - Fatal编程技术网

Unit testing 调用单个测试可以工作,但不能在MiniTest中运行所有测试

Unit testing 调用单个测试可以工作,但不能在MiniTest中运行所有测试,unit-testing,rake,jruby,minitest,Unit Testing,Rake,Jruby,Minitest,我在jruby-1.7.13中从测试单元切换到了小型测试。我也使用摩卡咖啡/集成咖啡。我的问题是运行“rake测试”会带来Mocha::ExpectationError:意外调用:MyClass.new。使用MyClass.new创建在lib文件夹中定义并在测试类中使用的类。我发现单独运行测试很好。这看起来像是时间或顺序问题。我试图通过在每次测试之前使用setup/teardown启动MyClass.new来绕过它,但这没有帮助。我必须模拟/存根MyClass.new吗 假设您使用的是Rails

我在jruby-1.7.13中从测试单元切换到了小型测试。我也使用摩卡咖啡/集成咖啡。我的问题是运行“rake测试”会带来Mocha::ExpectationError:意外调用:MyClass.new。使用MyClass.new创建在lib文件夹中定义并在测试类中使用的类。我发现单独运行测试很好。这看起来像是时间或顺序问题。我试图通过在每次测试之前使用setup/teardown启动MyClass.new来绕过它,但这没有帮助。我必须模拟/存根MyClass.new吗

假设您使用的是Rails 4,则需要在config/application.rb中添加以下内容:


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

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
如您所见,我开始修改测试顺序和可运行方法


这取决于测试失败的种子数。在某些情况下,如果使用特殊的种子编号,测试甚至可以运行。

我不使用Rails。我只运行纯Ruby/JRuby,没有web应用程序。我不明白你的代码行是做什么的。这是一种限制可以运行多少测试的配置吗?更清楚地说:在切换到MiniTest之前,我可以在test/lib下的子文件夹中运行项目中的所有测试,其中的百分之一百可以运行。现在,即使是测试目录中的子文件夹也无法运行。该行是一个Rails特定的配置,需要在lib/目录中自动加载类,因此它不会帮助您解决问题。您能否提供test_helper.rb和失败的测试用例的GIST?在没有看到代码的情况下,要想猜出问题出在哪里是不可能的。
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