Ruby minitest assert_output syntax

6.1k views Asked by At

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.

1

There are 1 answers

4
blowmage On BEST ANSWER

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