问题
Is there a variant of the RSpec start_with
matcher that will use regular expression matching instead of equality when examining arrays of strings? (I don't mind writing my own; but I don't want to re-invent the wheel.)
Specifically, I want to have a spec that looks like this:
it 'begins with the standard header' do
output = method_under_test
# output is an array of Strings
expect(output).to start_with([/foo/, /bar/])
end
This spec should pass if output[0]
matches /foo/
and output[1]
matches /bar/
.
Assuming I do need to write my own matcher, is there a way to "overload" start_with
, or do I need to choose a different name?
回答1:
It turns out that the solution is to use a combination of composing matchers and aliases:
it 'begins with the standard header' do
output = method_under_test
# output is an array of Strings
expect(output).to start_with(
a_string_matching(/foo/),
a_string_matching(/bar/)
)
end
来源:https://stackoverflow.com/questions/39254886/rspec-starts-with-matcher-variant-using-regular-expressions