Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用scalatest对Play服务进行功能测试_Scala_Playframework_Scalatest - Fatal编程技术网

使用scalatest对Play服务进行功能测试

使用scalatest对Play服务进行功能测试,scala,playframework,scalatest,Scala,Playframework,Scalatest,我已经使用Play2.5开发了RESTful服务,我正在尝试为我的服务开发功能测试。我是按照官方文件来做的 以下是控制器的测试: package com.x.oms.controller import org.scalatestplus.play._ import play.api.inject.guice.GuiceApplicationBuilder import play.api.mvc.Action import play.api.mvc.Results._ import play.a

我已经使用Play2.5开发了RESTful服务,我正在尝试为我的服务开发功能测试。我是按照官方文件来做的

以下是控制器的测试:

package com.x.oms.controller

import org.scalatestplus.play._
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc.Action
import play.api.mvc.Results._
import play.api.routing.Router
import play.api.test.Helpers.{GET => GET_REQUEST}

class Test extends PlaySpec with OneServerPerSuite {
  implicit override lazy val app =
    new GuiceApplicationBuilder().router(Router.from {
      // TODO: Find out how to load routes from routes file.
      case _ => Action {
        Ok("ok")
      }
    }).build()

  "Application" should {
    "be reachable" in {

      // TODO: Invoke REST services using Play WS

      // TODO: Assert the result
    }
  }
}
package controllers.com.x.oms

import com.google.inject.Inject
import com.x.oms.AuthenticationService
import com.x.oms.model.DBModels.User
import com.x.oms.model.UIModels.Token
import play.api.Logger
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}

class AuthenticationController @Inject()(as: AuthenticationService) extends Controller {

  private val log = Logger(getClass)

  def authenticate = Action(parse.json) {
    request =>
      val body = request.body
      val username = (body \ "username").as[String]
      val password = (body \ "password").as[String]

      log.info(s"Attempting to authenticate user $username")
      val token = as.authenticate(User(username, password))
      log.debug(s"Token $token generated successfully")
      token match {
        case token: String => Ok(Json.toJson(new Token(token)))
        case _ => Unauthorized
      }
  }

  def logout = Action {
    request =>
      val header = request.headers
      as.logout(header.get("token").get)
      Ok("Logout successful")
  }

  def index = Action {
    request =>
      Ok("Test Reply")
  }

}
以下是控制器的配置:

package com.x.oms.controller

import org.scalatestplus.play._
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc.Action
import play.api.mvc.Results._
import play.api.routing.Router
import play.api.test.Helpers.{GET => GET_REQUEST}

class Test extends PlaySpec with OneServerPerSuite {
  implicit override lazy val app =
    new GuiceApplicationBuilder().router(Router.from {
      // TODO: Find out how to load routes from routes file.
      case _ => Action {
        Ok("ok")
      }
    }).build()

  "Application" should {
    "be reachable" in {

      // TODO: Invoke REST services using Play WS

      // TODO: Assert the result
    }
  }
}
package controllers.com.x.oms

import com.google.inject.Inject
import com.x.oms.AuthenticationService
import com.x.oms.model.DBModels.User
import com.x.oms.model.UIModels.Token
import play.api.Logger
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}

class AuthenticationController @Inject()(as: AuthenticationService) extends Controller {

  private val log = Logger(getClass)

  def authenticate = Action(parse.json) {
    request =>
      val body = request.body
      val username = (body \ "username").as[String]
      val password = (body \ "password").as[String]

      log.info(s"Attempting to authenticate user $username")
      val token = as.authenticate(User(username, password))
      log.debug(s"Token $token generated successfully")
      token match {
        case token: String => Ok(Json.toJson(new Token(token)))
        case _ => Unauthorized
      }
  }

  def logout = Action {
    request =>
      val header = request.headers
      as.logout(header.get("token").get)
      Ok("Logout successful")
  }

  def index = Action {
    request =>
      Ok("Test Reply")
  }

}
路由文件:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

POST        /login         controllers.com.x.oms.AuthenticationController.authenticate
POST        /logout        controllers.com.x.oms.AuthenticationController.logout

GET         /              controllers.com.x.oms.AuthenticationController.index
我不想在每次测试中重新定义路由信息。在每次测试中构建应用程序时,我需要您的帮助来加载路由文件


请让我知道如何加载路由文件。提前谢谢

我建议您在Play基础架构之外测试您的
AuthenticationService
。如果您希望测试控制器本身,那么它们只是可以使用
FakeRequest直接调用的类:

val api = new AuthenticationController(as)

"fail an authentication attempt with bad credentials" in {
  val request = FakeRequest(POST, "/login")
    .withJsonBody(jsonBadCredentials)
  val result = call(api.authenticate(), request)
  status(result) mustBe UNAUTHORIZED
}

你的解决方案奏效了!虽然我不得不加入考试申请表。谢谢你的帮助。有几种方法可以做到这一点。如果我看到您正在使用DI,我会让我的测试扩展PlaySpec、OneAppPerSuite(如果您认为有用,还可以扩展其他一些),您可以通过GuiceApplicationBuilder创建应用程序,然后使用DI注入器设置环境。