Scala编码规范与最佳实践-测试

测试

  1. 测试类应该与被测试类处于同一包下。如果使用Spec2或ScalaTest的FlatSpec等,则测试类的命名应该为:被测类名 + Spec;若使用JUnit等框架,则测试类的命名为:被测试类名 + Test

  2. 测试含有具体实现的trait时,可以让被测试类直接继承Trait。例如:

trait RecordsGenerator {
     def generateRecords(table: List[List[String]]): List[Record] {
          //...
     }
}

class RecordsGeneratorSpec extends FlatSpec with ShouldMatcher with RecordGenerator {
     val table = List(List("abc", "def"), List("aaa", "bbb"))
     it should "generate records" in {
          val records = generateRecords(table)
          records.size should be(2)
     }
}
  1. 若要对文件进行测试,可以用字符串假装文件:
type CsvLine = String
def formatCsv(source: Source): List[CsvLine] = {
     source.getLines(_.replace(", ", "|"))
}

formatCsv需要接受一个文件源,例如Source.fromFile(“testdata.txt”)。但在测试时,可以通过Source.fromString方法来生成formatCsv需要接收的Source对象:

it should "format csv lines" in {
     val lines = Source.fromString("abc, def, hgi\n1, 2, 3\none, two, three")
     val result = formatCsv(lines)
     result.mkString("\n") should be("abc|def|hgi\n1|2|3\none|two|three")
}
上一篇:Mysql Table 'XXXXX' is marked as crashed and should be repaired


下一篇:剑桥雅思写作高分范文ESSAY61