代码之家  ›  专栏  ›  技术社区  ›  Michael Durrant

将字符串与rspec中的正则表达式进行比较?

  •  13
  • Michael Durrant  · 技术社区  · 11 年前

    我在做

    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`.
    
    1 回复  |  直到 7 年前
        1
  •  33
  •   Surya    11 年前

    试试看:

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

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

    推荐文章