Auto-delegation to a val in a Scala method
I'm writing ScalaTest FeatureSpec's for a Wicket app. I have a wicketTester value, that I keep on having to call methods on, viz:
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."))
Now all these refs to wicketTester are getting me down, so I want to remove them, aiming for DSL nirvana...
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开发者_如何学Go = wicketTester.newFormTester("signInPanel:signInForm")
assertRenderedPage(classOf[SignInPage])...
Is there any way to make the methods on a local appear as if they are methods on my class?
Did you try an import?
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])...
}
精彩评论