Curious_Bop Curious_Bop - 1 year ago 150
Ruby Question

Ruby unit test assert match case insensitive strings

In a Ruby unit test, how do I assert that a string matches another string even though the casing might be different? I want to avoid sanitizing the two strings to match incase I need to come back and do some investigation but at the same time they are the same outcomes.

e.g assert_match 'Test', 'TEST'

I've tried fudging with assert_match to make an case insensitive compare but I have had no luck thus far and I cannot get past the old implicit conversion of Regexp into 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.escape matcher if String === matcher
assert matcher =~ /#{obj}/i, /#{msg}/i

Answer Source

I would use downcase on both of the strings.

assert_equal "expected".downcase, actual.downcase

or write your own method:

def assert_equal_case_insensitive(expected, actual)
  assert_equal expected.downcase, actual.downcase
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download