Cab*_*ero 5 scala guice scalatest playframework playframework-2.4
所以根据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
Run Code Online (Sandbox Code Playgroud)
为了使测试像这样工作
class ExampleControllerSpec extends PlaySpec with Results {
class TestController() extends Controller with ExampleController
"Example Page#index" should {
"should be valid" in {
//test code
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我正在使用Guice依赖注入,并且根据Play 2.4文档(https://playframework.com/documentation/2.4.x/ScalaDependencyInjection),我的控制器如下所示:
@Singleton
class ExampleController @Inject() (exampleService: IExampleService) extends Controller {
def index() = Action {
Ok("")
}
}
Run Code Online (Sandbox Code Playgroud)
由于控制器不再是特性,我不能将其混合到测试中:with ExampleController如何使测试成功?
您可以直接从ExampleController继承。您还可以消除extends Controller,因为您的控制器已经继承了它:
class TestController(service: IExampleService) extends ExampleController(service)
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到有关使用 Play 和 Guice 进行测试的更多信息
| 归档时间: |
|
| 查看次数: |
1220 次 |
| 最近记录: |