【问题标题】:Functional testing Play services using scalatest功能测试使用 scalatest 播放服务
【发布时间】:2016-12-06 07:04:31
【问题描述】:

我已经使用 play 2.5 开发了 RESTful 服务,并且我正在尝试为我的服务开发功能测试。我正在关注官方文档here todo 。

这是控制器的测试:

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

我不想在每个测试中重新定义路线信息。在每次测试中构建应用程序时,我需要您的帮助来加载路由文件。

请告诉我如何加载路由文件。提前致谢。

【问题讨论】:

    标签: scala playframework scalatest


    【解决方案1】:

    我建议您在 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
    }
    

    【讨论】:

    • 您的解决方案奏效了!虽然我必须添加 WithApplication 进行测试。感谢您的帮助。
    • 有几种方法可以做到这一点。鉴于我看到您正在使用 DI,我会让我的测试扩展 PlaySpec、OneAppPerSuite(如果您认为有用,可能还有其他一些),您可以在其中通过 GuiceApplicationBuilder 创建您的应用程序,然后使用 DI 注入器来设置您的环境。
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多