Play:如何在生产代码中创建虚假请求 [英] Play: How to Create a Fake Request in Production Code

查看:16
本文介绍了Play:如何在生产代码中创建虚假请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FakeRequest 来自工件 play-test 并且仅在测试范围内添加到项目中......但我需要创建一种假请求只是为了调用一个采用隐式 RequestHeader 的方法:

FakeRequest comes from artifact play-test and is added to the project only in the test scope... but I need to create a kind of fake request just to invoke a method that takes an implicit RequestHeader:

import play.api.test._

...

implicit val request = FakeRequest(
    Helpers.POST,
    controllers.routes.auth.Users.triggerPasswordReset(superuser.email.get).url,
    FakeHeaders(),
    ""
)

// createToken takes an implict RequestHeader
createToken(TokenType.Reset, account).map { token =>
    EmailHelper.sendPasswordResetEmail(user.email.get, token.asJwt)
    ...
}

如何在编译范围内导入FakeRequest?有更好的选择吗?还是直接调用控制器的方法?

How do I import FakeRequest in the compile scope? Is there a better option? Or shall I invoke the controller's method directly?

推荐答案

将以下内容添加到 Build.sbt

Add the following to your Build.sbt

libraryDependencies ++= Seq(
  "com.typesafe.play" %% "play-test" % "2.2.1" % "compile"
)

确保将2.2.1"更改为您使用的任何版本的 Play.

Make sure to change "2.2.1" to be whatever version of Play your are using.

这应该将播放测试类暴露给编译范围.

This should expose the play test classes to the compile scope.

干杯!

这篇关于Play:如何在生产代码中创建虚假请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆