Ruby minitest assert_output语法 [英] Ruby minitest assert_output syntax
问题描述
我是最小的新手,还是红宝石的新手,真的很厌倦尝试用谷歌搜索这个问题而没有结果.我将非常感谢您的帮助:
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:
在ruby minitest中assert_output的确切语法是什么?
What is the exact syntax of assert_output in ruby minitest?
我在github或其他地方找到的所有内容似乎都使用括号.但是,当我不使用带有assert_output的块时,会收到一条错误消息,这很有意义,因为此方法的定义包含yield语句.
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
testclass.rb
class TestClass
def output
puts 'hey'
end
end
test_test.rb
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
我得到的是:
在0.000000s内完成测试,NaN测试/秒,NaN断言
Finished tests in 0.000000s, NaN tests/s, NaN assertions
0个测试,0个断言,0个失败,0个错误,0个跳过
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.
推荐答案
要运行您的测试方法,方法名称必须以test_
开头.另外, assert_output
的工作方式是该块将写入stdout /stderr,则将检查参数是否与stdout/stderr匹配.检查此IMO的最简单方法是传递一个正则表达式.因此,这就是我编写该测试的方式:
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
这篇关于Ruby minitest assert_output语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!