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

ruby-on-rails – 在rspec中将字符串与regex进行比较?`

发布时间:2020-12-17 04:33:28 所属栏目:百科 来源:网络整理
导读:我在做 expect(@link.url_address == 'abc').to be_true 但url_address可能在abc之后有其他文本,所以我正在尝试 expect(@link.url_address =~ 'abc').to be_true 但我得到了 Failure/Error: expect(@link.url_address =~ /abc/).to be_true expected to resp
我在做
expect(@link.url_address == 'abc').to be_true

但url_address可能在abc之后有其他文本,所以我正在尝试

expect(@link.url_address =~ 'abc').to be_true

但我得到了

Failure/Error: expect(@link.url_address =~ /abc/).to be_true
   expected  to respond to `true?`

我也试过了

expect(@link.url_address).to =~ /abc/

但我明白了

Failure/Error: expect(@link.url_address).to =~ /abc/
 ArgumentError:
   The expect syntax does not support operator matchers,so you must pass a matcher to `#to`.

解决方法

试试这个:
expect(@link.url_address).to match(/abc/)

资料来源:https://github.com/rspec/rspec-expectations#regular-expressions

(编辑:李大同)

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

    推荐文章
      热点阅读