首页 > 解决方案 > 在 IntelliJ 中找不到 PlaySpec

问题描述

下面是 websocket 的 Scala 测试:

   import java.util.function.Consumer

import play.shaded.ahc.org.asynchttpclient.AsyncHttpClient
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.{Helpers, TestServer, WsTestClient}
import scala.compat.java8.FutureConverters
import scala.concurrent.Await
import scala.concurrent.duration._
import org.scalatestplus.play._

class SocketTest extends PlaySpec with ScalaFutures {

  "HomeController" should {

    "reject a websocket flow if the origin is set incorrectly" in WsTestClient.withClient { client =>

      // Pick a non standard port that will fail the (somewhat contrived) origin check...
      lazy val port: Int = 31337
      val app = new GuiceApplicationBuilder().build()
      Helpers.running(TestServer(port, app)) {
        val myPublicAddress = s"localhost:$port"
        val serverURL = s"ws://$myPublicAddress/ws"

        val asyncHttpClient: AsyncHttpClient = client.underlying[AsyncHttpClient]
        val webSocketClient = new WebSocketClient(asyncHttpClient)
        try {
          val origin = "ws://example.com/ws"
          val consumer: Consumer[String] = new Consumer[String] {
            override def accept(message: String): Unit = println(message)
          }
          val listener = new WebSocketClient.LoggingListener(consumer)
          val completionStage = webSocketClient.call(serverURL, origin, listener)
          val f = FutureConverters.toScala(completionStage)
          Await.result(f, atMost = 1000.millis)
          listener.getThrowable mustBe a[IllegalStateException]
        } catch {
          case e: IllegalStateException =>
            e mustBe an[IllegalStateException]

          case e: java.util.concurrent.ExecutionException =>
            val foo = e.getCause
            foo mustBe an[IllegalStateException]
        }
      }
    }

  }
}

但是编译失败import org.scalatestplus.play._并出现错误:

Cannot resolve symbol scalatestplus

https://www.playframework.com/documentation/2.8.x/ScalaTestingWithScalaTest我添加了 scalatest 和 play 来构建:

构建.sbt:

name := "testproject"

version := "1.0" 

lazy val `testproject` = (project in file(".")).enablePlugins(PlayScala)

resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"

resolvers += "Akka Snapshot Repository" at "https://repo.akka.io/snapshots/"

scalaVersion := "2.12.2"

libraryDependencies ++= Seq( jdbc , ehcache , ws  , guice , specs2 % Test)

// https://mvnrepository.com/artifact/com.typesafe.scala-logging/scala-logging
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"

libraryDependencies ++= Seq(
  "org.scalatestplus.play" %% "scalatestplus-play" % "3.0.0" % "test"
)
unmanagedResourceDirectories in Test <+=  baseDirectory ( _ /"target/web/public/test" )  

当我右键单击 build.sbt 但未找到导入时,我尝试在 IntelliJ“构建”选项和“构建选项”中重建项目和模块。

标签: scalaintellij-idea

解决方案


sbt dist从 Intellij “sbt shell” 然后 File -> “Invalidate caches” 重新启动 IntelliJ 似乎解决了这个问题

:无效缓存截图


推荐阅读