Ruby单元测试断言匹配不区分大小写的字符串

时间:2016-07-25 13:22:55

标签: ruby-on-rails ruby string unit-testing

在Ruby单元测试中,即使外壳可能不同,我如何断言字符串与另一个字符串匹配?我想避免对两个字符串进行消毒以便匹配我需要回来做一些调查,但同时它们也是相同的结果。

e.g assert_match 'Test', 'TEST'

我尝试用assert_match进行捏造以进行不区分大小写的比较,但到目前为止我没有运气,而且我无法通过旧的Regexp隐式转换为String。

module Test::Unit::Assertions
  def assert_match matcher, obj, msg = nil
    msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" }
    assert_respond_to matcher, :"=~"
    matcher = Regexp.new Regexp.escape matcher if String === matcher
    assert matcher =~ /#{obj}/i, /#{msg}/i
  end
end

1 个答案:

答案 0 :(得分:3)

我会在两个字符串上使用downcase

assert_equal "expected".downcase, actual.downcase

或编写自己的方法:

def assert_equal_case_insensitive(expected, actual)
  assert_equal expected.downcase, actual.downcase
end