Scala单元测试组织技巧

时间:2019-08-29 07:41:07

标签: scala unit-testing tdd

我有这样的代码:

private def processCreationBasedOnSomeImportantMessage(importantId: Long, smthChanged: SmthChanged))(implicit session: Session) =
    smthChanged.phases.filter(_.phaseType == APhase).foreach { phaseChanged =>
      val stuffs = phaseChanged.stuffs.filter(_.typex == ATest).zipWithIndex
      stuffs.foreach { case (stuff, position) =>
        val properPosition = position + 1
        val model = Model(stuff, properPosition)
        val generatedNumber = numberHandler.getNumber(model)
        repository.insert(model.toInsertDB(generatedNumber))
      }
    }

  def handleSomeImportantMessage(smthChanged: SmthChanged)(implicit session: Session): Unit = {
    val importantIdOpt = importStuffHandler.getInternalId(smthChanged.id)
    importantIdOpt match {
      case Some(importantId) => processArticleSetCreationBasedOnCostPosition(importantId, smthChanged)
      case None => log.error(s"Missing smth for id: ${smthChanged.id}")
    }
  }

这是用于处理来自RabbitMQ的消息。 根据此值,我查询:importStuffHandler.getInternalId(smthChanged.id) 然后基于此,我需要调用另一个处理程序:numberHandler.getNumber(model) 最终将记录插入数据库:repository.insert(model.toInsertDB(generatedNumber))

包含这些方法的类当然很少注入类(存储库,numberHandler,importStuffHandler)

要对此进行测试,我不想做完整的工作(因此,真正的数据库调用将是集成测试,这是我在其他地方通过API调用->处理程序等-> DB-> Api response进行的) 所以我需要做模拟,但是做得好的模拟会让我关注实现细节:(我想省略。 因为然后在每个测试用例中,我都会模拟这些,模拟那个等等。 你们如何处理此类问题?

0 个答案:

没有答案