如何在单元测试中使用我的自定义AppLoader

时间:2018-06-05 05:25:51

标签: scalatest playframework-2.6

我正在使用编译时注入,我有自己的AppLoader.scala类。该类通过显式传递所需的依赖项来实例化控制器。例如

AppLoader.scala

val dependency = new SomeDependency();
val controller = new SomeController(dependency)

我想为SomeController创建一个测试类。我在项目的顶层创建了test/ControllerSpec/SomeControllerSpec.scala。我没有在SomeControllerSpec.scala中重复上述代码,而是认为我将使用自定义AppLoader。例如

SomeControllerSpec.scala

package test.ControllerSpec

import controllers.SomeController
import org.scalatestplus.play._
import play.api.mvc._
import play.api.test._
import play.api.test.Helpers._
//import AppLoader //THIS DOESNT WORK
import scala.concurrent.Future


class UserControllerSpec extends PlaySpec {

  "A non-JSON body" must {
    "return  400 (Bad Request) and the validation text 'Incorrect body type. Body type must be JSON'" in {
      val controller = new AppLoader().controller //This doesn't work because the compiler cannot find AppLoader

    }
  }
}

稍后,我尝试通过在test/ControllerSpec/SomeControllerSpec.scala的{​​{1}}文件夹中创建app来移动我的测试位置,但现在我看到未解决/未找到的文件,例如Playimport play.api.test.Helpers._

UPDATE 我能够使用示例https://github.com/playframework/play-scala-compile-di-example

取得进展

基本上我创建了一个新文件Cannot resolve PlaySpec

test/MyApplicationFactory

而不是在规范中明确创建我的自定义加载器的实例,我做了

import java.io.File

import org.scalatestplus.play.FakeApplicationFactory
import play.api._
import play.api.inject._
import play.core.DefaultWebCommands

trait MyApplicationFactory extends FakeApplicationFactory {

  override def fakeApplication: Application = {
    val env = Environment.simple(new File("."))
    val configuration = Configuration.load(env)
    val context = ApplicationLoader.Context(
      environment = env,
      sourceMapper = None,
      webCommands = new DefaultWebCommands(),
      initialConfiguration = configuration,
      lifecycle = new DefaultApplicationLifecycle()
    )
    val loader = new AppLoader()
    loader.load(context)
  }

}

问题1 - 我不明白为什么编译器可以在package ControllerSpec import controllers.UserController import org.scalatestplus.play._ import play.api.mvc._ import play.api.test._ import play.api.mvc.Result import play.api.test.Helpers._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future class UserControllerSpec extends PlaySpec { implicit val port:play.api.test.WsTestClient.Port = 9000; "User signup request with non-JSON body" must { "return 400 (Bad Request) and the validation text 'Incorrect body type. Body type must be JSON'" in { // val controller = new AppLoader()//userController val request = WsTestClient.wsUrl("/ws/users/signup") val responseFuture = request.get() responseFuture.map(response =>{ println("response :"+response); response.status mustBe play.api.http.Status.BAD_REQUEST }) //1 mustEqual 1 //placeholder } } } 但不在MyApplicationFactory中找到AppLoader?

问题2 - 我正在尝试在我的规范UserControllerSpec中打印回复,但我没有看到任何原因?

问题3 - 我希望控制器发送响应println("response :"+response);,因此测试用例应该失败,但事实并非如此!为什么呢?

似乎我没有使用正确的控制器实例,或者有一些管道问题,但我不知道是什么问题。我怀疑我在问题2和3中没有正确处理Ok(Json.toJson(JsonResultError("Invalid Body Type. Need Json"))),但我不知道如何使其工作

0 个答案:

没有答案