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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在rspec中将字符串与regex进行比较?`前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在做
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

猜你在找的Ruby相关文章