Ruby Rspec中的组合匹配器

Ruby Rspec中的组合匹配器,ruby,rspec,Ruby,Rspec,Rspec支持组合匹配器。它提供的合成匹配器列表如下: 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) 全合成匹配器是直观的。您可以将匹配器传递给合成匹配器,传

Rspec支持组合匹配器。它提供的合成匹配器列表如下:

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') )

在本例中,外部和内部的起始字符是什么?

在您的示例中,您正在测试
水果的第一个元素是否以字符
a
开始。因此,外部的
开始\u以数组的第一个元素为目标,而内部的
开始\u以第一个元素为目标

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

fruits = ['banana', 'apple', 'cherry']
expect(fruits).to start_with( start_with('a') )
数组中有几个示例,您希望测试第一个元素是否以给定的值或字符串开始或结束。例如:

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))

同样相关:

因此外部以数组的第一个元素为目标启动\u,而内部以第一个元素的第一个字符为目标启动\u。没错!我也会在答案中加上这个。