我想使用Java(而不是Gherkin)手动设置Cucumber DataTable。
在小 cucumber 中,我的 table 如下所示:
| h1 | h2 |
| v1 | v2 |
到目前为止,我的Java看起来像这样:
List<String> raw = Arrays.asList( "v1", "v2");
DataTable dataTable = DataTable.create(raw, Locale.getDefault(), "h1", "h2");
我得到的是一个带有标题但没有内容的DataTable。它也比预期的长:
| h1| h2 |
| | |
| | |
我敢肯定解决方案必须相当简单,但是现在我有点茫然。我需要做什么才能拿到 table ?
最佳答案
希望这可以帮助。如果您已经吃饱了,那么小 cucumber 的步骤应该是这样的...
When I see the following cooked I should say:
| food | say |
| Bacon | Yum! |
| Peas | Really? |
您想要用Java编写。 (请注意,传入的cucumber.api.DataTable是使用测试前的期望值设置的)。
@When("^I see the following cooked I should say:$")
public void theFoodResponse(DataTable expectedCucumberTable) {
// Normally you'd put this in a database or JSON
List<Cuke> actualCukes = new ArrayList();
actualCukes.add(new Cuke("Bacon", "Yum!"));
actualCukes.add(new Cuke("Peas", "Really?"));
Another link to a Full Example.diff(actualCukes)
}
我会说,在AslakHellesøy的示例中,他实际上并没有使用DataTable。
他会为您做这样的示例:
@When("^I see the following cooked I should say:$")
public void theFoodResponse(List<Entry> entries) {
for (Entry entry : entries) {
// Test actual app you've written
hungryHuman.setInputValue(entry.food);
hungryHuman.setOutputValue(entry.say);
}
}
public class Entry {
String food;
String say;
}
有关更多阅读的完整示例,请查看:
编辑:
对不起@Christian,您可能并不需要在应用程序中使用它的整个上下文,只是使用DataTable.create的一种简洁方法,而我发布的大部分内容是使用Entry来为那只猫换皮的另一种方法上课(这对以后阅读本文的人可能会有帮助。)
因此,您在评论中所做的事情并非遥不可及。我不是集合专业人士,因此我无法给您任何有关制作2D字符串列表的提示,但是我可以澄清最后两个参数(如果您使用全部4个参数)。
中倒数第二个参数中的Format。
用于列名的最后一个,然后它将自动读取列名的顶行字符串。
。
List<List<String>> infoInTheRaw = Arrays.asList( Arrays.asList("h1", "h2"),
Arrays.asList("v1", "v2") );
DataTable dataTable = DataTable.create(infoInTheRaw);
您还可以使用同样麻烦的构造函数。 :)
关于cucumber - 如何创建 cucumber 数据表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21704496/