-->

为什么“未定义方法`assert_equal”'甚至需要‘测试/单元’抛出后(Why 

2019-07-31 10:59发布

我打开IRB&进入:

require 'test/unit'

但是当我用assert_equal方法,我得到了以下错误: NoMethodError: undefined method 'assert_equal' for main:Object 。 这究竟是为什么甚至要求“测试/单位”之后?

Answer 1:

assert_equal定义上的子类Test::Unit::TestCase ,所以只能在该类可用。 你可能有一些成功的include Test::Unit::TestCase那些方法加载到目前的范围。

更有可能的是,你可以更好地在很短的文件中写入你的测试,并与运行它们ruby ./my_file.rb



Answer 2:

您可以在内置红宝石误码测试使用

raise "Message you want to throw when error happens" if/unless "Condition when you want to throw the error "

要么

“:未定义的方法`断言”主:NoMethodError对象”如果你想使用断言,就像当遇到错误信息,然后添加到您的脚本的顶部:

require "test/unit/assertions"
include Test::Unit::Assertions


Answer 3:

这是怎样的断言被使用:

class Gum
  def crisis; -42 end
end

# and as for testing:

require 'test/unit'

class GumTest < Test::Unit::TestCase
  def test_crisis
    g = Gum.new
    assert_equal -42, g.crisis
  end
end


文章来源: Why 'undefined method `assert_equal' ' is thrown even after requiring 'test/unit'
标签: ruby testunit