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

3

11

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

Molality answered 7/9, 2012 at 12:16 Comment(0)
C
11

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

Chrissie answered 7/9, 2012 at 12:22 Comment(0)
A
11

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
Antipater answered 29/4, 2015 at 18:20 Comment(0)
P
7

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
Plovdiv answered 7/9, 2012 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.