SBT无法解析src/test/scala测试类中src/main/scala中声明的类

SBT无法解析src/test/scala测试类中src/main/scala中声明的类,scala,intellij-idea,sbt,Scala,Intellij Idea,Sbt,我正在尝试制作自己的自定义CSV阅读器。我将IntelliJ IDEA 14与sbt和specs2测试框架一起使用 src/main中声明的I类如下: import java.io.FileInputStream import scala.io.Source class CSVStream(filePath:String) { val csvStream = Source.fromInputStream(new FileInputStream(filePath)).getLines()

我正在尝试制作自己的自定义CSV阅读器。我将IntelliJ IDEA 14与sbt和specs2测试框架一起使用

src/main
中声明的I类如下:

import java.io.FileInputStream

import scala.io.Source

class CSVStream(filePath:String) {
  val csvStream = Source.fromInputStream(new FileInputStream(filePath)).getLines()
  val headers = csvStream.next().split("\\,", -1)
}
import org.specs2.mutable._

object CSVStreamSpec {

  val csvSourcePath = getClass.getResource("/csv_source.csv").getPath
}

class CSVStreamSpec extends Specification {
  import CSVStreamLib.CSVStreamSpec._

  "The CSV Stream reader" should {
    "Extract the header" in {
      val csvSource = CSVStream(csvSourcePath)
    }
  }
}
[error] /Users/raiyan/IdeaProjects/csvStreamLib/src/test/scala/csvStreamSpec.scala:18: not found: value CSVStream
[error]       val csvSource = CSVStream(csvSourcePath)
[error]                       ^
[error] one error found
[error] (test:compile) Compilation failed
[error] Total time: 23 s, completed 30-Dec-2014 07:44:46
src/test
中测试文件的内容如下:

import java.io.FileInputStream

import scala.io.Source

class CSVStream(filePath:String) {
  val csvStream = Source.fromInputStream(new FileInputStream(filePath)).getLines()
  val headers = csvStream.next().split("\\,", -1)
}
import org.specs2.mutable._

object CSVStreamSpec {

  val csvSourcePath = getClass.getResource("/csv_source.csv").getPath
}

class CSVStreamSpec extends Specification {
  import CSVStreamLib.CSVStreamSpec._

  "The CSV Stream reader" should {
    "Extract the header" in {
      val csvSource = CSVStream(csvSourcePath)
    }
  }
}
[error] /Users/raiyan/IdeaProjects/csvStreamLib/src/test/scala/csvStreamSpec.scala:18: not found: value CSVStream
[error]       val csvSource = CSVStream(csvSourcePath)
[error]                       ^
[error] one error found
[error] (test:compile) Compilation failed
[error] Total time: 23 s, completed 30-Dec-2014 07:44:46
build.sbt
文件包含以下内容:

name := "csvStreamLib"

version := "1.0"

scalaVersion := "2.11.4"

libraryDependencies ++= Seq("org.specs2" %% "specs2-core" % "2.4.15" % "test")

parallelExecution in Test := false
当我进行型式试验时,我得到的错误如下:

import java.io.FileInputStream

import scala.io.Source

class CSVStream(filePath:String) {
  val csvStream = Source.fromInputStream(new FileInputStream(filePath)).getLines()
  val headers = csvStream.next().split("\\,", -1)
}
import org.specs2.mutable._

object CSVStreamSpec {

  val csvSourcePath = getClass.getResource("/csv_source.csv").getPath
}

class CSVStreamSpec extends Specification {
  import CSVStreamLib.CSVStreamSpec._

  "The CSV Stream reader" should {
    "Extract the header" in {
      val csvSource = CSVStream(csvSourcePath)
    }
  }
}
[error] /Users/raiyan/IdeaProjects/csvStreamLib/src/test/scala/csvStreamSpec.scala:18: not found: value CSVStream
[error]       val csvSource = CSVStream(csvSourcePath)
[error]                       ^
[error] one error found
[error] (test:compile) Compilation failed
[error] Total time: 23 s, completed 30-Dec-2014 07:44:46
如何使测试文件中的CSVStreamSpec类可以访问CSVStream类

更新:


我在命令行中使用sbt进行了尝试。结果是一样的。

您忘记了
new
关键字。如果没有它,编译器将查找名为
CSVStream
的伴随对象,而不是类。既然没有,它就抱怨。添加
新的
,它就会工作。

我想每个学习Scala的人都会在这上面重复几次。如果错误消息中包含类似“我看到了一个使用该名称的类型,但没有值”这样的内容,那不是很酷吗