如何在specs2中将输入和结果定义为DataTable?

时间:2014-01-29 15:01:59

标签: scala testing specs2

我试图在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和specs的新手。为简洁起见,省略了一些代码。

1 个答案:

答案 0 :(得分:3)

在更好地理解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中有效。