Ruby minitest assert_output syntax

試著忘記壹切 提交于 2019-12-01 02:24:12

问题


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:


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 


来源:https://stackoverflow.com/questions/26854048/ruby-minitest-assert-output-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!