播放/ Scala注入控制器进入测试
发布时间:2020-12-16 18:36:39 所属栏目:安全 来源:网络整理
导读:因此,根据Play 2.4文档( https://playframework.com/documentation/2.4.x/ScalaTestingWithScalaTest#Unit-Testing-Controllers),控制器应设置为这样的特征 trait ExampleController { this: Controller = def index() = Action { Ok("ok") }}object Example
因此,根据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("") } } 由于控制器不再是特性,我无法将其混合到测试中:使用ExampleController,如何使测试工作正常? 解决方法
您可以直接从ExampleController继承.你也可以删除extends Controller,因为你的控制器已经继承了这个:
class TestController(service: IExampleService) extends ExampleController(service) 您可以使用Play和Guice here找到有关测试的更多信息 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |