【问题标题】:Compare string to regex in rspec?将字符串与rspec中的正则表达式进行比较?
【发布时间】:2014-08-18 21:39:04
【问题描述】:

我在做

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`.

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rspec


    【解决方案1】:

    试试这个:

    expect(@link.url_address).to match(/abc/)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      相关资源
      最近更新 更多