问题描述
我是 minitest 的新手,也是 ruby 的新手,真的厌倦了在没有结果的情况下用谷歌搜索这个问题.非常感谢您的帮助:
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.000000 秒内完成的测试,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 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!