我正在使用Minitest 5.12.0编写以下测试:
require 'minitest/autorun'
require 'nokogiri'
require 'net/http'
class NetClass; end
describe NetClass do
attr_accessor :uri, :net
before do
@uri=URI('http://example.com/index.html')
@net=Net::HTTP.get(uri)
end
it 'gets uri' do
net.must_match /Example Domain/i
end
end
测试成功,但输出警告:
已弃用:net_test.rb中的must_match的全局使用:16请改用
_(obj).must_match
。这将在Minitest 6中失败]
为了消除警告,我更改了行:
net.must_match /Example Domain/i
到
_(net).must_match /Example Domain/i
我之前没有看过_(obj)
语法,所以我的问题是_()
在这种情况下正在做什么。
这是最小测试专用功能。从文档:
# Returns a value monad that has all of Expectations methods
# available to it.
#
# Also aliased to #value and #expect for your aesthetic pleasure:
#
# _(1 + 1).must_equal 2
# value(1 + 1).must_equal 2
# expect(1 + 1).must_equal 2
因此,它是将所有需要的测试方法添加到对象的包装器。
实施如下:
def _ value = nil, &block
Minitest::Expectation.new block || value, self
end
您可以深入研究来源here