在 Ruby 单元测试中,如何断言字符串包含子字符串?就像是:
assert_contains string_to_test, substring_to_verify
在 Ruby 单元测试中,如何断言字符串包含子字符串?就像是:
assert_contains string_to_test, substring_to_verify
assert_match pattern, string, [message]
如果string =~ pattern
:_
assert_match substring_to_verify, string_to_test
例如
assert_match /foo/, "foobar"
如果您经常使用它,为什么不编写自己的断言呢?
require 'test/unit'
module Test::Unit::Assertions
def assert_contains(expected_substring, string, *args)
assert_match expected_substring, string, *args
end
end
或者,使用@IvayloStrandjev 描述的方法(更容易理解),您可以定义
require 'test/unit'
module Test::Unit::Assertions
def assert_contains(expected_substring, string, *args)
assert string.include?(expected_substring), *args
end
end
用法完全符合您在问题中的要求,例如
class TestSimpleNumber < Test::Unit::TestCase
def test_something
assert_contains 'foo', 'foobar'
end
def test_something_fails
assert_contains 'x', 'foobar', 'Does not contain x'
end
end
哪个会产生
Run options:
# Running tests:
.F
Finished tests in 0.000815s, 2453.9877 tests/s, 2453.9877 assertions/s.
1) Failure:
test_something_fails(TestSimpleNumber) [assertion.rb:15]:
Does not contain x
2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
根据要求,使用自动消息:
module Test::Unit::Assertions
def assert_contains(exp_substr, obj, msg=nil)
msg = message(msg) { "Expected #{mu_pp obj} to contain #{mu_pp exp_substr}" }
assert_respond_to obj, :include?
assert obj.include?(exp_substr), msg
end
end
改编自原文assert_match
。这实际上也适用于数组!
assert_contains 3, [1,2,3]
例如,你可以写assert string_to_test.include?(string_to_verify)
。您不能期望对您想要执行的所有检查都有断言,因此只需执行布尔条件的经典检查即可。
还可以在这里查看所有可用断言的列表。
有assert_includes
:
assert_includes 'foobar', 'foo'
将断言foobar
包含foo
.
我会使用其中之一:
assert(string_to_test[substring_to_verify])
assert_equal(substring_to_verify, string_to_test[substring_to_verify])
他们完成同样的事情,所以第一个是我通常的选择。
像这样:
assert string_to_test.index(substring_to_verify)
如果未找到子字符串,.index 方法将返回 nil,这将导致断言失败。
我会使用assert_match
:
require 'test/unit'
class MyTest < Test::Unit::TestCase
def test_match
assert_match( /aa/, 'xxaaxx')
end
def test_match_fail
#~ assert_match( /aa/, 'xxbbxx') #fails
end
end
如果你经常需要它,你可以扩展 TestCase:
require 'test/unit'
module Test
module Unit
class TestCase
#Define new assertion
def assert_contains(string_to_test, substring_to_verify)
assert_match( string_to_test, substring_to_verify)
end
def assert_not_contains(string_to_test, substring_to_verify)
assert_not_match( string_to_test, substring_to_verify)
end
end
end
end
class MyTest < Test::Unit::TestCase
def test_contains()
assert_contains( /aa/, 'xxaaxx')
assert_contains( 'aa', 'xxaaxx')
end
#~ def test_contains_fail()
#~ assert_contains( 'aa', 'xxxxxx')
#~ assert_contains( /aa/, 'xxxxxx')
#~ end
#~ def test_contains_not_fail()
#~ assert_not_contains( /aa/, 'xxaaxx')
#~ assert_not_contains( 'aa', 'xxaaxx')
#~ end
def test_contains_not()
assert_not_contains( 'aa', 'xxxxxx')
assert_not_contains( /aa/, 'xxxxxx')
end
def test_contains_special_characters()
assert_contains( '[aa', 'xx[aaxx')
#~ assert_contains( /[aa/, 'xx[aaxx')
end
end
评论:
test_contains_special_characters
工作。