Composing matchers in Rspec

Donato

New Member
#1
Rspec supports composing matchers. The list of composing matchers it provides is this:
Mã:
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)
The all composing matcher is intuitive. You can pass matchers to the composing matcher and the passed matchers must return true for the expectation to be true:
Mã:
expect(@items).to all(be_visible & be_in_stock)
But I am not certain about the start_with and end_with composing matcher. Look at this example:
Mã:
fruits = ['apple', 'banana', 'cherry']
expect(fruits).to start_with( start_with('a') )
What are the outer and inner start_with doing in this example?
 

Admin

Administrator
Thành viên BQT
#2
In your example, you are testing that the first element of fruits starts with the character a.

Your example passes, but for instance this fails:

Mã:
fruits = ['banana', 'apple', 'cherry']
expect(fruits).to start_with( start_with('a') )
There are several examples in the rspec-expectations tests of arrays where you want to test that the first element(s) start or end with a given value or string. For example:
Mã:
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))
 
OP
OP
D

Donato

New Member
#3
So the outer starts_with targets the first element of array, and the inner starts_with targets the first character of the first element.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top