j3d
j3d

Reputation: 9724

Play: How to Create a Fake Request in Production Code

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)
    ...
}

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

Upvotes: 1

Views: 1009

Answers (1)

g00dnatur3
g00dnatur3

Reputation: 1193

Add the following to your Build.sbt

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

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.

Cheers!

Upvotes: 4

Related Questions