我试图在specs2中使用DataTables来定义输入以及结果应该是什么样子并且无法使其工作。我在想类似于以下代码的内容:
class MySpec extends Specification with DataTables {
"A Container" should {
"after data is added container should have the following data" in new TestContainer {
"a" | "flag" | "d" |
100 ! 1 ! "abc" |
300 ! 1 ! "abc" |
200 ! 0 ! "xyz" |>
{ (a, flag, d) =>
container.add(Data(a, flag, d)) must not(throwA[Exception])
} and
"a" | "flag" | "d" |
300 ! 1 ! "abc" |
100 ! 1 ! "abc" |>
{ (a, flag, d) => ????
}
}
}
免责声明:我是scala和spec的新手。为了简洁起见,省略了一些代码。
最佳答案
在对specs2有了更好的了解之后,我想到了一个解决方案:
class MySpec extends Specification with DataTables {
"A Container" should {
"after data is added container should have the following data" in new TestContainer {
"a" | "flag" | "d" |
100 ! 1 ! "abc" |
300 ! 1 ! "abc" |
200 ! 0 ! "xyz" |>
{ (a, flag, d) =>
container.add(Data(a, flag, d)) must not(throwAn[Exception])
}
val state = container.list
"a" | "flag" | "d" |
300 ! 1 ! "abc" |
100 ! 1 ! "abc" |>
{ (a, flag, d) => state must contain((a, flag, d))
}
}
}
如果顺序很重要,则第二个表可以将元组添加到某个列表中,然后在处理第二个表后比较2个列表。请注意,此行为在2.3中已损坏,但在2.4-SNAPSHOT中有效。
关于scala - 如何在specs2中将输入和结果定义为DataTable?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21434810/