从正则表达式中拉出的字符串似乎匹配但不匹配

时间:2013-01-11 00:26:27

标签: ruby regex selenium testunit

这对我没有任何意义。我试图断言这两个日期是一样的。他们看起来一样,但不知怎的,他们是不同的。

#assert correct time and date
assert_equal(/[0-9]{1,2}, [0-9]{4} at [0-9]{1,2}:[0-9]{2} [A-Z]{2}/.match($driver.find_element(:class, "flash-information").text), /[0-9]{1,2}, [0-9]{4} at [0-9]{1,2}:[0-9]{2} [A-Z]{2}/.match($driver.find_element(:class, "news_date").text))

这是我遇到的失败。

F
===============================================================================
Failure: <#<MatchData "10, 2013 at 4:13 PM">> expected but was
<#<MatchData "10, 2013 at 4:13 PM">>. 

   39:     #assert correct time and date
=> 40:     assert_equal(/[0-9]{1,2}, [0-9]{4} at [0-9]{1,2}:[0-9]{2} [A-Z]{2}/.match($driver.find_element(:class, "flash-information").text), /[0-9]{1,2}, [0-9]{4} at [0-9]{1,2}:[0-9]{2} [A-Z]{2}/.match($driver.find_element(:class, "news_date").text))

我在测试单元内运行

1 个答案:

答案 0 :(得分:2)

您不匹配字符串,而是匹配MatchData个对象。

a = "foobar".match /f/   # #<MatchData "f">
b = "foobar".match /f/   # #<MatchData "f">
c = "barfoo".match /f/   # #<MatchData "f">

a == b   # true
a == c   # false

如果要比较匹配的字符串,则必须从MatchData对象中提取它们:

a[0]           # "f"
a[0] == b[0]   # true
a[0] == c[0]   # true