5

I'm trying to execute the following code, but it is giving me an undefined method error:

require 'minitest/autorun'
require 'string_extension'

class StringExtensionTest < Minitest::Test
  def test_humanize_returns_nothing
    assert_not_nil "Yo".humanize, "humanize is returning nil."
  end
end

I'm trying to execute this code from Codeschool's Rails course, but it seems that there is no method assert_not_nil. I've also checked the documentation for MiniTest framework, and neither it is defined there, but I'm not sure.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76

4 Answers4

6

Here you can find the list of assertions defined by MiniTest.

As you can see, assert_not_nil is not defined. There's a lot of possible assertions that MiniTest doesn't define, but it's also true that any assertion can be defined with the simplest assertion ever possible assert.

In your specific case:

assert result != nil

You can also pass a custom message

assert result != nil, "Expected something to not be nil"
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • Well that was the information I needed to know if I should change to `MiniTest::Test` or stick with `Test::Unit::TestCase`. Basic `assert` doesn't produce readable error message so `Test::Unit::TestCase` it is. – Martin Apr 05 '20 at 18:27
6

assert_not_nil is just an alias for refute_nil, but it's Rails-only, not part of standard Minitest. Changing your test to extend ActiveSupport::TestCase should do the trick:

class StringExtensionTest < ActiveSupport::TestCase
  def test_humanize_returns_nothing
    assert_not_nil "Yo".humanize, "humanize is returning nil."
  end
end
Chris Kottom
  • 1,249
  • 10
  • 11
0

assert !!result has worked for me. !! means "convert to boolean" (literally double negation).

Victor Pudeyev
  • 4,296
  • 6
  • 41
  • 67
-1

You can use

assert "Yo".humanize

In ruby everthing but nil and false is true-ish.

UPDATE: since you want to check explicetly for not nil:

refute_nil "Yo".humanize

But in the scenario you describe i'd actually check for the return value to be what I expect. Consider:

class String
  def humanize
    false
  end
end

refute_nil "Yo".humanize
assert_equal "Yo", "Yo".humanize
Pascal
  • 8,464
  • 1
  • 20
  • 31