RSpec starts_with matcher variant using regular expressions

送分小仙女□ 提交于 2019-12-11 05:20:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!