35

在 Ruby 单元测试中,如何断言字符串包含子字符串?就像是:

assert_contains string_to_test, substring_to_verify
4

6 回答 6

45

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]
于 2013-04-20T14:56:22.203 回答
8

例如,你可以写assert string_to_test.include?(string_to_verify)。您不能期望对您想要执行的所有检查都有断言,因此只需执行布尔条件的经典检查即可。

还可以在这里查看所有可用断言的列表。

于 2013-04-20T14:05:44.393 回答
8

assert_includes

assert_includes 'foobar', 'foo'

将断言foobar包含foo.

于 2018-06-01T09:26:00.140 回答
2

我会使用其中之一:

assert(string_to_test[substring_to_verify])
assert_equal(substring_to_verify, string_to_test[substring_to_verify])

他们完成同样的事情,所以第一个是我通常的选择。

于 2013-04-20T14:38:40.247 回答
0

像这样:

assert string_to_test.index(substring_to_verify)

如果未找到子字符串,.index 方法将返回 nil,这将导致断言失败。

于 2013-04-20T14:10:39.400 回答
0

我会使用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工作。
  • 您可以定义自己的正则表达式。
于 2013-04-20T14:54:55.420 回答