VictorGram
VictorGram

Reputation: 2661

Scala : play is not recognizing the test components

I am transitioning from Java to Scala. I have written a simple test to render a view. Like :

import org.scalatestplus.play.PlaySpec
import org.scalatest._
import org.slf4j.LoggerFactory
import play.test.WithApplication


class EvTemplateTests extends PlaySpec{
  implicit lazy val log = LoggerFactory.getLogger(getClass)

 //run your test//run your test

 "render eval template" in {
  val html = views.html.index("Hello")
  contentType(html) must equalTo("text/html")
  contentAsString(html) must contain("Welcome to Play!")
 }
}

When compiling, looks like it is not finding "index","contentType", "contentAsString" etc.Looks like, the project is using the libraries:

lazy val thirdPartyDependencies = Seq(
jdbc,
"com.typesafe.play" %% "anorm" % "2.4.0",
"com.typesafe.play" %% "play-mailer" % "3.0.1",
"com.microsoft.sqlserver" % "mssql-jdbc" % "6.4.0.jre8",
"io.swagger" %% "swagger-play2" % "1.5.0",  // This version adds Play 2.4 support.
// ScalaTest+ Play (have to use non-release 1.4.0-M4 version for now as it is only compatible with Play 2.4)
"org.scalatestplus" %% "play" % "1.4.0-M4" % "test",
"org.mockito" % "mockito-core" % "1.10.19" % "test"
)

May I get any insight?

Upvotes: 0

Views: 146

Answers (1)

Bunyod
Bunyod

Reputation: 1371

You can start from here:

import controllers.AssetsFinder
import org.specs2.mutable.Specification
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.mvc._
import play.api.test._
import play.api.test.Helpers._

class EvTemplateTests
    extends Specification
    with DefaultAwaitTimeout
    with FutureAwaits
    with Results {

  val application: Application = GuiceApplicationBuilder().build()

  "render eval template" in new WithApplication(app = application) {
    implicit val assetsFinder = app.injector.instanceOf[AssetsFinder]
    val html = views.html.index("Hello")
    contentType(html) mustEqual "text/html"
    contentAsString(html) must contain("Hello")
  }

}

add this after jdbc in LibraryDependencies: specs2 % Test

Most important part of test setup here is implicit AssetFinder that derived from GuiceApplicationBuilder:

val application: Application = GuiceApplicationBuilder().build()

AssetFinder is really important part of view testing in PlayFramework

Upvotes: 2

Related Questions