使用 scala 和 Injection 进行功能测试 playframework 的更好方法是什么
What is the better approach on functional testing playframework with scala with Injection
我正在使用 scala 2.8,我是 scala 的新手。由于注入,我在测试这个端点时遇到了挑战。
下面是我的端点
@Singleton
class AuthController @Inject()(
cc:ControllerComponents
,authService: IAuthService
)
extends AbstractController(cc){
def login() = Action.async { implicit request: Request[AnyContent] =>
val username:String = request.body.asFormUrlEncoded.flatMap(m => m.get("username").flatMap(_.headOption)).getOrElse("")
val password:String = request.body.asFormUrlEncoded.flatMap(m => m.get("password").flatMap(_.headOption)).getOrElse("")
val loginRequest = LoginRequest(username, password)
authService.validate(loginRequest)
.flatMap{
case Some(value) => Future.successful(Ok( value.access_token))
case None => Future.successful(BadRequest("Something went wrong"))
}
}
def register(): Unit ={
TODO
}
}
下面是我的测试片段
class AuthControllerTest extends PlaySpec {
"AuthControllerTest" should {
"login" in {
val controller = new AuthController(stubControllerComponents(), null)
val json = Json.parse("{\"firstName\":\"Foo\", \"lastName\":\"Bar\", \"age\":13}")
val request = FakeRequest(POST, "/v1/auth/login").withJsonBody(json)
val home = controller.login().apply(request)
status(home) mustBe OK
}
"register" in {
}
}
}
您可以考虑在您的测试用例中进行以下更改,这些是外观更改
class AuthControllerTest extends PlaySpec with Injecting {
val controller = inject[AuthController]
"AuthControllerTest" should "login" in {
val json = Json.parse(
""" {
| "firstName": "Foo",
| "lastName": "Bar",
| "age": 13
|}""".stripMargin)
val request = FakeRequest(POST, "/v1/auth/login").withJsonBody(json)
val result = call(controller.login(), request)
status(result) mustBe OK
}
"register" in {
}
}
}
我正在使用 scala 2.8,我是 scala 的新手。由于注入,我在测试这个端点时遇到了挑战。
下面是我的端点
@Singleton
class AuthController @Inject()(
cc:ControllerComponents
,authService: IAuthService
)
extends AbstractController(cc){
def login() = Action.async { implicit request: Request[AnyContent] =>
val username:String = request.body.asFormUrlEncoded.flatMap(m => m.get("username").flatMap(_.headOption)).getOrElse("")
val password:String = request.body.asFormUrlEncoded.flatMap(m => m.get("password").flatMap(_.headOption)).getOrElse("")
val loginRequest = LoginRequest(username, password)
authService.validate(loginRequest)
.flatMap{
case Some(value) => Future.successful(Ok( value.access_token))
case None => Future.successful(BadRequest("Something went wrong"))
}
}
def register(): Unit ={
TODO
}
}
下面是我的测试片段
class AuthControllerTest extends PlaySpec {
"AuthControllerTest" should {
"login" in {
val controller = new AuthController(stubControllerComponents(), null)
val json = Json.parse("{\"firstName\":\"Foo\", \"lastName\":\"Bar\", \"age\":13}")
val request = FakeRequest(POST, "/v1/auth/login").withJsonBody(json)
val home = controller.login().apply(request)
status(home) mustBe OK
}
"register" in {
}
}
}
您可以考虑在您的测试用例中进行以下更改,这些是外观更改
class AuthControllerTest extends PlaySpec with Injecting {
val controller = inject[AuthController]
"AuthControllerTest" should "login" in {
val json = Json.parse(
""" {
| "firstName": "Foo",
| "lastName": "Bar",
| "age": 13
|}""".stripMargin)
val request = FakeRequest(POST, "/v1/auth/login").withJsonBody(json)
val result = call(controller.login(), request)
status(result) mustBe OK
}
"register" in {
}
}
}