【问题标题】:Minitest - Tests Don't Run - No RailsMinitest - 测试不运行 - 没有 Rails
【发布时间】:2015-11-23 13:13:49
【问题描述】:

我刚刚开始一个小项目来模拟嘉年华的售票亭,其中一个准则是测试用户是否可以输入门票数量。该程序在控制台中运行,由于@Stefan 在this question 上的回答,我最终(希望)弄清楚了如何实现此测试。

问题是现在,当我运行测试文件时,minitest 说:

0 次运行,0 次断言,0 次失败,0 次错误,0 次跳过

当我尝试使用ruby path/to/test/file.rb --name method-name 按名称运行测试时,我得到了相同的结果。我不确定这是否是因为我的代码仍然有问题,或者是因为我错误地设置了 minitest。我试图在 SO 上查找类似的问题,但大多数问题似乎都涉及使用带有 rails 的 minitest,而我只有一个普通的 ruby​​ 项目。

这是我的测试文件:

gem 'minitest', '>= 5.0.0'
require 'minitest/spec'
require 'minitest/autorun'
require_relative 'carnival'

class CarnivalTest < MiniTest::Test
  def sample
    assert_equal(1, 1)
  end

  def user_can_enter_number_of_tickets
    with_stdin do |user|
      user.puts "2"
      assert_equal(Carnival.new.get_value, "2")
    end
  end

  def with_stdin
    stdin = $stdin                 # global var to remember $stdin
    $stdin, write = IO.pipe        # assign 'read end' of pipe to $stdin
    yield write                    # pass 'write end' to block
  ensure
    write.close                    # close pipe
    $stdin = stdin                 # restore $stdin
  end
end

在一个名为 carnival.rb 的文件中,与我的测试文件位于同一文件夹中

Class Carnival
  def get_value
    gets.chomp
  end
end

如果有人能帮助找出测试未运行的原因,我将不胜感激!

【问题讨论】:

    标签: ruby minitest


    【解决方案1】:

    按照惯例,Minitest 中的测试都是以test_ 开头的公共实例方法,因此原始测试没有实际的测试方法。您需要更新您的测试类,以便带有断言的方法遵循以下约定:

    class CarnivalTest < Minitest::Test
      def test_sample
        assert_equal(1, 1)
      end
    
      def test_user_can_enter_number_of_tickets
        with_stdin do |user|
          user.puts "2"
          assert_equal(Carnival.new.get_value, "2")
        end
      end
    
      # snip...
    end
    

    【讨论】:

    • 我不敢相信它这么简单!谢谢!
    【解决方案2】:

    是的,总是用 test_ 开始你的所有测试,所以它知道你想要那个函数/方法

    class CarnivalTest < MiniTest::Test
     def test_sample
    assert_equal(1, 1)
    end
    
     def test_user_can_enter_number_of_tickets
    with_stdin do |user|
      user.puts "2"
      assert_equal(Carnival.new.get_value, "2")
      end
    end
    

    这应该适合你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多