Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method: assert_not_nil in Minitest

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.

like image 676
Arslan Ali Avatar asked Mar 12 '16 09:03

Arslan Ali


2 Answers

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"
like image 92
Simone Carletti Avatar answered Oct 30 '22 08:10

Simone Carletti


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
like image 27
Chris Kottom Avatar answered Oct 30 '22 09:10

Chris Kottom