10

I am new to minitest and still new to ruby and really tired of trying to google this question without result. I would be really grateful for help:

What is the exact syntax of assert_output in ruby minitest?

All I find on github or elsewhere seems to use parentheses. Yet, I get an error message when I don't use a block with assert_output, which makes sense as the definition of this method contains a yield statement.

But I cannot make it work, whatever I try.

testclass.rb

class TestClass
  def output
    puts 'hey'
  end
end

test_test.rb

require 'minitest/spec'
require 'minitest/autorun'
require_relative 'testclass'


class TestTestClass < MiniTest::Unit::TestCase 
  def setup
    @test = TestClass.new
  end

  def output_produces_output
    assert_output( stdout = 'hey' ) { @test.output}
  end   
end 

What I get is:

Finished tests in 0.000000s, NaN tests/s, NaN assertions

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

What am I doing wrong? It must be something totally obvious, but I cannot figure it out. Thanks for your help.

enahel
  • 355
  • 3
  • 15

1 Answers1

17

In order for your test method to run, the method name needs to start with test_. Also, the way assert_output works is that the block will write to stdout/stderr, and the arguments will be checked if they match stdout/stderr. The easiest way to check this IMO is to pass in a regexp. So this is how I would write that test:

class TestTestClass < MiniTest::Unit::TestCase 
  def setup
    @test = TestClass.new
  end

  def test_output_produces_output
    assert_output(/hey/) { @test.output}
  end   
end 
blowmage
  • 8,854
  • 2
  • 35
  • 40
  • THANK YOU!! So it was the method name not starting with 'test' and I was questioning syntax. Thank you for sparing me to search for longer on the wrong question. – enahel Nov 11 '14 at 10:13
  • One more question: The example works the way you wrote it with regex, it also works the way I wrote it with stdout = 'hey'. It does not work with only assert_output('hey') without 'stdout ='. Why is that? – enahel Nov 11 '14 at 10:16
  • 1
    And - lastly - why can I have more assertions than runs in minitest? In my actual example there is one single test, it says "1 runs, 2 assertions". (BTW Can I suppress the actual stdout output of the method during the test to have command prompt more readable?) – enahel Nov 11 '14 at 11:00
  • 1
    There are two assertions because `assert_output` calls `assert` twice. – blowmage Nov 11 '14 at 23:15