【发布时间】:2016-02-21 15:01:24
【问题描述】:
所以根据 Play 2.4 文档 (https://playframework.com/documentation/2.4.x/ScalaTestingWithScalaTest#Unit-Testing-Controllers),控制器应该设置为这样的特征
trait ExampleController {
this: Controller =>
def index() = Action {
Ok("ok")
}
}
object ExampleController extends Controller with ExampleController
为了让测试像这样工作
class ExampleControllerSpec extends PlaySpec with Results {
class TestController() extends Controller with ExampleController
"Example Page#index" should {
"should be valid" in {
//test code
}
}
}
但是,我使用的是 Guice 依赖注入,根据 Play 2.4 文档 (https://playframework.com/documentation/2.4.x/ScalaDependencyInjection),我的控制器如下所示:
@Singleton
class ExampleController @Inject() (exampleService: IExampleService) extends Controller {
def index() = Action {
Ok("")
}
}
由于控制器不再是一个特征,我不能像这样将它混合到测试中:with ExampleController,我该如何让上面的测试工作?
【问题讨论】:
标签: scala playframework guice scalatest playframework-2.4