加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 尝试在RSpec has_selector行中使用正则表达式

发布时间:2020-12-17 03:59:05 所属栏目:百科 来源:网络整理
导读:我是 Ruby / Rails / RSpec的新手,希望这是一个愚蠢/简单的问题.我正在为我们当前的项目编写RSpec测试.我目前正在做的是编写一个测试,用于在页面上显示上次登录时间/日期. 我想做的只是测试日期/时间是否以我们想要的格式出现.我正在使用正则表达式来检查格
我是 Ruby / Rails / RSpec的新手,希望这是一个愚蠢/简单的问题.我正在为我们当前的项目编写RSpec测试.我目前正在做的是编写一个测试,用于在页面上显示上次登录时间/日期.

我想做的只是测试日期/时间是否以我们想要的格式出现.我正在使用正则表达式来检查格式.我正在创建正则表达式,然后尝试使用should 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读