如何解决尝试在RSpec的have_selector行中使用正则表达式
| 我是Ruby / Rails / RSpec的新手,希望这是一个愚蠢/简单的问题。我目前正在为当前的Projet编写RSpec测试。我目前正在做的事情涉及编写一个用于在页面上显示上次登录时间/日期的测试。 我要做的只是测试日期/时间是否以我们想要的格式显示。我正在使用正则表达式检查格式。我正在创建正则表达式,然后尝试使用应有的have_selector这样的行(请原谅丑陋的正则表达式)-it \"should display last login date and time\" do
date_time_regex = /Last Login: [0-9][0-9]-[0-9][0-9]-[0-9][0-9],[0-9][0-9]:[0-9[0-9]]/
visit root_path
response.should have_selector(\"date_time\",:content => date_time_regex)
end
当我尝试运行测试时,出现以下错误-
1) LayoutLinks when signed in should display last login date and time
Failure/Error: response.should have_selector(\"date_time\",:content => date_time_regex)
NoMethodError:
undefined method `include?\' for #<Regexp:0xef2a40>
# ./layout_links_spec.rb:60:in `block (3 levels) in <top (required)>\'
看来我无法传递正则表达式,但是我不确定下一步该怎么做?在此先感谢您的帮助!
解决方法
date_time_regex = /Last Login: [0-9][0-9]-[0-9][0-9]-[0-9][0-9],[0-9][0-9]:[0-9[0-9]]/
response.should have_selector(\'data_time\') do |data_time|
data_time.should contain(date_time_regex)
end
要么...
response.should have_selector(\'data_time\') do |data_time|
data_time.should =~ date_time_regex
end
,对您有帮助的一件事是,您可以做\\d
而不是[0-9]
。所以你得到
/Last Login: \\d\\d-\\d\\d-\\d\\d,\\d\\d:\\d\\d/
更具可读性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。