scala - Unit Testing a Play framework Controller -
i have written controller works browser
package controllers import play.api._ import play.api.mvc._ class application extends controller { val productmap = map(1 -> "keyboard", 2 -> "mouse", 3 -> "monitor") def listproductsxml() = action { ok(views.xml.products(productmap)) } }
the route defined
get /listproducts.xml controllers.application.listproductsxml
now writing unit test controller
import controllers._ import play.api.test.fakerequest import play.api.test.helpers._ import org.specs2.mutable._ import play.api.test.withapplication class controllertest extends specification { "controllers.application" should { "respond xml /listproducts.xml requests" in new withapplication { val result = controllers.application.listproductsxml()(fakerequest()) status(result) must equalto(ok) contenttype(result) must besome("application/xml") contentasstring(result) must contain("products") } } }
when run activator test-only
error
[foo_play] $ test-only controllertest [error] productspec.scala:10: object application not member of package controllers [error] note: class application exists, has no companion object. [error] val result = controllers.application.listproductsxml()(fakerequest()) [error] ^ [error] 1 error found [error] (test:compileincremental) compilation failed [error] total time: 1 s, completed jun 19, 2015 3:48:14 pm
try replacing:
controllers.application.listproductsxml()(fakerequest())
with:
new controllers.application().listproductsxml()(fakerequest())
pre play 2.4, controllers used objects. play 2.4 on, it's encouraged them classes instead.
Comments
Post a Comment