迷你测试不工作

Minitest not working

我有一个简单的 ruby 应用程序,它解析一个字符串并计算 X 和 Os 的数量,returns 如果数量相等则为真,否则为假。

我想知道为什么我的测试不起作用。

当我 运行 测试时,应用程序被调用,测试 运行s,但是有 0 运行s,0 断言...等等。为什么?

应用程序:

class Calc

  def xo(str)
    ex = 0
    oh = 0
    for i in (0...str.length)
      if str[i] == "x" || str[i] == "X"
        ex += 1
      elsif str[i] == "o" || str[i] == "O"
        oh += 1
      else
      end
    end
    if ex == oh
      puts "equal" 
      return true
    else
      puts "not equal"
      return false
    end
  end
end

Calc.new.xo("xoxoo")

测试:

require_relative "task"
require "minitest/autorun"

class Test < MiniTest::Test

  def zeetest
    @calc = Calc.new
    assert_equal( true, @calc.xo("xoxo"))
  end

end

试试这个:

require_relative "task"
require "minitest/autorun"

class TestCalc < MiniTest::Test

  def setup
    @calc = Calc.new
  end

  def test_xo
    assert_equal( true, @calc.xo("xoxo"))
  end

end