如何为scala对象编写scalatest单元测试?

如何为scala对象编写scalatest单元测试?,scala,unit-testing,scalatest,Scala,Unit Testing,Scalatest,我正试图使用scalatest为scala对象编写一个单元测试。 我为我的sbt导入了以下依赖项 libraryDependencies+=org.scalactic%%scalactic%3.0.5 libraryDependencies+=org.scalatest%%scalatest%3.0.5%测试 如何编写此代码的测试用例 object Word { def readFile(): Map[String, Int] = { val counter = scala.io.

我正试图使用scalatest为scala对象编写一个单元测试。 我为我的sbt导入了以下依赖项

libraryDependencies+=org.scalactic%%scalactic%3.0.5 libraryDependencies+=org.scalatest%%scalatest%3.0.5%测试

如何编写此代码的测试用例

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)
  }
}

你想问什么?请在您的问题中添加详细信息您面临的问题是什么?谢谢您的回复。在这里,我想问一下,使用上述依赖关系,我需要为scala对象实现一个测试用例,如下所示:这很好,但您面临什么问题?实际上,只有在任何人都能帮助你的时候,才能为你的问题提供更多的信息。def readFile:Map[String,Int]={val counter=scala.io.Source.fromFileData\\Reading.txt.getLines.flatMap\ux.split\\W+.foldLeftMap.empty[String,Int]{count,word=>count+word->count.getOrElseword,0+1}return counter}因此我需要在对象字中为上述方法实现SACALAST