Scala IDE: 对象 scalatest 不是包 org 的成员

Scala IDE: object scalatest is not a member of package org

我正在尝试使用 Scala IDE。我是 Scala 的新手。

HelloSpec.scala:

import org.scalatest._

class HelloSpec extends FlatSpec with Matchers {
  "The Hello object" should "say hello" in {
    Hello.greeting shouldEqual "hello"
  }
}

import org.scalatest._ 标记为错误:

object scalatest is not a member of package org

我已经尝试 google 很多次了,但仍然不明白哪里出了问题。我的项目结构看起来正确:

src/main/scala:
----Hello.scala
src/test/scala:
----HelloSpec.scala
build.sbt
plugins.sbt

build.sbt:

import Dependencies._

ThisBuild / scalaVersion     := "2.12.8"
ThisBuild / version          := "0.1.0-SNAPSHOT"
ThisBuild / organization     := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "$name$",
    libraryDependencies += scalaTest % Test
  )

plugins.sbt:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

顺便说一句,我还没有构建项目。这可能是原因吗?我不知道如何从 Scala IDE 构建项目,如果我从 Windows 命令提示符构建它,那么它不会影响 Scala IDE.

我假设你的项目结构如下,

src/
  main/
    resources/
       <files to include in main jar here>
    scala/
       <main Scala sources>
  test/
    resources
       <files to include in test jar here>
    scala/
       <test Scala sources>
build.sbt
plugins.sbt

这是简单的问候class

object Hello {
  def greeting: String = {
    "hello"
  }
}

我创建了如下单元测试

import org.scalatest._

class HelloSpec extends FlatSpec with Matchers {
  "The Hello object" should "say hello" in {
    Hello.greeting shouldEqual "hello"
  }
}

因此,您只需要在依赖项中添加 scala 测试即可。下面是简单的 sbt 文件

name := "Hello Test"
version := "0.1"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.0.6" % "test")