Monday, August 29, 2011

SpecFlow Tests and Lists

Some days I just can't see the forest for the trees...

Sometimes you just want to pass a list to check against:

Scenario: some files match
	Given I have the following files
		| FileName           |
		| test.txt           |
		| sample.txt         |
		| sample.file.txt |
		| sample.file2.txt |
		And  I have the following processes
			| Regex        | Process         |
			| sample.*.txt | TestProcess.bat |
	When I run the application
	Then The files should be processed
		| FileName           |
		| sample.file.txt |
		| sample.file2.txt |

If you're using SpecFlow and need to pass in a list to one of your bindings, here's a simple one-liner to convert a table to a list of strings:

[TestClass]
[Given(@"I have the following files")]
public void GivenIHaveTheFollowingFiles(Table table)
{
    var files = table.Rows.Select(o => o["FileName"]);
}

'files' will be a list of the strings in the table. I'm sure there's a better way, but this one works.

1 comment: