Why 'undefined method `assert_equal' ' is thrown even after requiring 'test/unit'

核能气质少年 提交于 2019-12-09 16:37:14

问题


I opened irb & entered:

require 'test/unit'

but when I used the assert_equal method, I got following error: NoMethodError: undefined method 'assert_equal' for main:Object. Why is this happening even after requiring 'test/unit' ?


回答1:


assert_equal is defined on subclasses of Test::Unit::TestCase, so are only available in that class. You may have some success with include Test::Unit::TestCase to load those methods onto the current scope.

More likely you could be better writing your tests in a short file, and running them with ruby ./my_file.rb




回答2:


You can use in built ruby error testing

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

OR

If you get error messages when trying to use assertions, like "NoMethodError: undefined method `assert' for main:Object", then add this to the top of your script:

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



回答3:


This is how assertions are used:

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


来源:https://stackoverflow.com/questions/12317921/why-undefined-method-assert-equal-is-thrown-even-after-requiring-test-unit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!