如何为 scala 对象编写 scalatest 单元测试?
How to write scalatest unit test for scala object?
我正在尝试使用 scalatest 为 scala 对象 编写单元测试但是。
我为 sbt
导入了以下依赖项;
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
如何编写这段代码的测试用例?
object Word {
def readFile(): Map[String, Int] = {
val counter = scala.io.Source.fromFile("filepath")
.getLines.flatMap(_.split("\W+"))
.foldLeft(Map.empty[String, Int]) {
(count, word) =>
count + (word ->
(count.getOrElse(word, 0) + 1))
}
counter
}
}
有很多方法可以为它编写测试。有像 FlatSpec、FunSuit 这样的库。
所以在这里,我将使用 FunSuit
为其编写测试用例
import org.scalatest.FunSuite
class WordTest extends FunSuite {
test("should return word count ") {
val wordCount = Word.readFile()
assert(wordCount.nonEmpty)
}
}
我正在尝试使用 scalatest 为 scala 对象 编写单元测试但是。
我为 sbt
导入了以下依赖项;
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
如何编写这段代码的测试用例?
object Word {
def readFile(): Map[String, Int] = {
val counter = scala.io.Source.fromFile("filepath")
.getLines.flatMap(_.split("\W+"))
.foldLeft(Map.empty[String, Int]) {
(count, word) =>
count + (word ->
(count.getOrElse(word, 0) + 1))
}
counter
}
}
有很多方法可以为它编写测试。有像 FlatSpec、FunSuit 这样的库。 所以在这里,我将使用 FunSuit
为其编写测试用例import org.scalatest.FunSuite
class WordTest extends FunSuite {
test("should return word count ") {
val wordCount = Word.readFile()
assert(wordCount.nonEmpty)
}
}