代码之家  ›  专栏  ›  技术社区  ›  Daniel Viglione

在Rspec中合成匹配器

  •  1
  • Daniel Viglione  · 技术社区  · 8 年前

    all(matcher)
    include(matcher, matcher)
    start_with(matcher)
    end_with(matcher)
    contain_exactly(matcher, matcher, matcher)
    match(matcher)
    change {}.from(matcher).to(matcher)
    change {}.by(matcher)
    

    全合成匹配器是直观的。您可以将匹配器传递给组成匹配器,并且传递的匹配器必须返回true,期望值才会为true:

    expect(@items).to all(be_visible & be_in_stock)
    

    但我不确定用作曲匹配器开始和结束。看看这个例子:

    fruits = ['apple', 'banana', 'cherry']
    expect(fruits).to start_with( start_with('a') )
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   Ana María Martínez Gómez    8 年前

    fruits 从角色开始 a starts_with 以数组的第一个元素和内部元素为目标 以开始 以第一个元素的开头为目标。

    您的示例通过了,但例如失败:

    fruits = ['banana', 'apple', 'cherry']
    expect(fruits).to start_with( start_with('a') )
    

    rspec-expectations tests

    expect([1.01, "food", 3]).to start_with(a_value_within(0.2).of(1), a_string_matching(/foo/))
    
    expect([3, "food", 1.1]).to end_with(a_value_within(0.2).of(1))
    

    也相关: RSpec starts_with matcher variant using regular expressions