4

我正在尝试定义一些上下文,以便为数据表的每一行执行它(在每一行上运行断言之前)。

我找到了这个例子,但对于我的生活,我无法弄清楚如何编写完整的测试套件。我想定义一次上下文并与所有示例共享。这大致是我所拥有的:

class SomeSuite extends Specification with DataTables {

// TODO: define context somehow???
// val context = new Before { println("BEFORE") }

"test 1" should {
  "do something" in {
    context |
    "col1" | "col2" |
    val1   ! val2   |
    val3   ! val4   |> {
      (a, b) => //some assertion with (a, b)
    }
  }
}
}

我希望在每个带有(a,b)的断言之前每次(总共 2 次)打印“之前”。

我真的很感激任何帮助。

谢谢 ;)

感谢 Eric,这是我的最终代码。我只添加了“隐式”,因为许多测试共享上下文:

class SomeSuite extends Specification with DataTables {

  implicit val context = new Before { def before = println("BEFORE") }

  "test 1" should {
    "do something" in {
      "col1"  | "col2"  |
      val1    ! val2    |
      val3    ! val4    |> { (a, b) => 
        a must_== b // this is wrapped with context
      }
    }
  }
}
4

1 回答 1

5

最简单的方法是使用applya的方法Context

class SomeSuite extends Specification with DataTables {

  val context = new Before { def before = println("BEFORE") }

  "test 1" should {
    "do something" in {
      "col1"  | "col2"  |
      val1    ! val2    |
      val3    ! val4    |> { (a, b) => 
        context { a must_== b } 
      }
    }
  }
}
于 2013-05-08T02:24:03.800 回答