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

Alpha picture Alpha · Sep 7, 2012 · Viewed 7.9k times · Source

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' ?

Answer

Lee Hambley picture Lee Hambley · Sep 7, 2012

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