scalatest-用fallbackTo测试Future[S]的方法



前提:当我的API响应用户对象的请求时,我想尝试用case class PartnerView(id: String, vipStatus: Option[Boolean], latestSession: Option[Timestamp]的属性丰富它。由于数据库有时可能不可靠,因此我使用fallbackTo将值作为可选值提供,因此不会在用户JSON响应中显示这些值。

到目前为止,以下实现似乎是有效的(通过Postman运行请求会返回不带可选值的UserJSON(,但我的单元测试会抱怨,好像我有一个未捕获的Exception。

服务类别:

class Service(repo: Repository) {
def get(id: String): Future[Partner] = {
val account = repo.getAccount(id)
val getLatestSession = repo.getLatestSession(id)
val partnerView = (for {
account <- getAccount
latestStartTime <- getLatestSession.map {
case Some(x) => x.scheduledStartTime
case _ => None
}
} yield PartnerView(partnerId, account.vipStatus, latestStartTime))
.fallbackTo(Future.successful(PartnerView(id, None, None)))
partnerView
}
}

Repository类:

class Repository(database: DatabaseDef, logger: LoggingAdapter) {
def getAccount(id: String): Future[Account] = database.run((...).result.head)
.recover {
case e: Exception =>
logger.error(e, "DB Server went down")
throw e
}
def getLatestSession(id: String): Future[Option[Session]] = database.run((...).result.headOption)
.recover {
case e: Exception =>
logger.error(e, "DB Server went down")
throw e
}
}

单元测试:

class ServiceSpec extends AsyncFlatSpec with AsyncMockFactory with OneInstancePerTest {
val mockRepo = mock[Repository]
val service = new Service(mockRepo)
behaviour of "Service"
it should "get an empty PartnerView when the repository get an Exception" in {
(mockRepository.getAccount _)
.expects("partner")
.throwing(new Exception)
service.get("partner")
.map(partnerView => assert(partnerView.id == "partner" && partnerView.vipStatus.isEmpty))
}
}

测试将失败,并显示消息

Testing started at 5:15 p.m. ...




java.lang.Exception was thrown.
{stacktrace here}

我在等Exception

通过将mock设置更改为以下内容,测试成功运行:

it should "get an empty PartnerView when the repository get an Exception" in {
(mockRepository.getAccount _)
.expects("partner")
.returning(Future.failed(new Exception))
...
}

由于recover方法将Exception封装在Future

来源:

恢复与恢复使用

scala官方文章

相关内容

  • 没有找到相关文章

最新更新