在Scala方法中自动委派给val

时间:2011-06-08 18:37:10

标签: scala dsl

我正在为Wicket应用编写ScalaTest FeatureSpec。我有一个wicketTester值,我继续调用方法,即:

scenario("No username and password") {
  val wicketTester = new WicketTester(app)
  given("user visits Admin home page")
  wicketTester.startPage(classOf[AdminHomePage])

  then("signin page is displayed")
  val login = wicketTester.newFormTester("signInPanel:signInForm")
  wicketTester.assertRenderedPage(classOf[SignInPage])
  ...
  when("correct username and password are entered")
  ...
  then("no login and error is displayed")
  wicketTester.assertRenderedPage(classOf[SignInPage])
  wicketTester.assertErrorMessages(Array("Field 'password' is required."))

现在所有这些对wicketTester的参考都让我失望,所以我想删除它们,目标是DSL天堂......

scenario("No username and password") {
  val wicketTester = new WicketTester(app)
  given("user visits Admin home page")
  startPage(classOf[AdminHomePage])

  then("signin page is displayed")
  val login = wicketTester.newFormTester("signInPanel:signInForm")
  assertRenderedPage(classOf[SignInPage])...

有没有办法让本地的方法看起来好像是我班上的方法?

1 个答案:

答案 0 :(得分:7)

您尝试导入了吗?

scenario("No username and password") {
   val wicketTester = new WicketTester(app)
   import wicketTester._    // import wicketTester's members into scope
   given("user visits Admin home page")
   startPage(classOf[AdminHomePage])

   then("signin page is displayed")
   val login = wicketTester.newFormTester("signInPanel:signInForm")
   assertRenderedPage(classOf[SignInPage])...  
}