I have a string: I attach "dog.png", "cat.png", and "alphabet.doc"

This is what I want matched:

1. dog.png
2. cat.png
3. alphabet.doc

I can do this by doing "([^"]*)", however, the string has to start with 'I attach'

This is the current rule I have in my step definition: /^I attach "([^"]*)"$/ but this doesn't work.

This is for cucumber/gherkin, I can figure something out using scan but I don't think you can use that in cuke/gherkin?

Any suggestions on how to do this?

Thanks

有帮助吗?

解决方案

If you want to capture a variable number of inputs in a single step, I think the best option is to use scan. While you cannot use scan directly in the step name, you can capture the full list of files and then parse the captured result.

Try the following step definition:

Given /^I attach (.*)$/ do |file_list|
  files = file_list.scan(/"(.+?)"/).flatten
  #=> ["dog.png", "cat.png", "alphabet.doc"]
end

This will create an array of files to attach. It can handle 1 to infinite files.

其他提示

This seems like a good place to use the #And alias.

Given I attach dog.png
  And I attach cat.png
  And I attach alphabet.doc
...

Alternatively you can use data tables for easy multiline arguments

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top