[英]Patches in scalatest
I'm new to scala and same for scalatest.我是 Scala 的新手,对于 Scalatest 也是如此。
My aim is to write a Unit test for the following small piece of code:我的目标是为以下一小段代码编写单元测试:
import java.sql.SQLException
import com.typesafe.scalalogging.LazyLogging
import slick.jdbc.MySQLProfile.api._
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor, Future}
object DbCreator extends LazyLogging {
implicit val ex: ExecutionContextExecutor = ExecutionContext.global
def createDatabaseIfNotExist(): Future[String] = {
Database
.forURL(url = "some host", user = "user", password = "pass", driver = "driver")
.run(sqlu"CREATE DATABASE ...").map(_ => "created")
.recover {
case e: Throwable => {
logger.error("Error!", e)
throw new SQLException(e.getMessage)
}
}
}
}
I've previously used python, which has the patch concept .我以前用过python,它有补丁概念。
So my idea was to patch the Database
class being imported and used in createDatabaseIfNotExist
, so I can verify the different scenarios.所以我的想法是修补在
createDatabaseIfNotExist
导入和使用的Database
类,以便我可以验证不同的场景。
Unfortunately, I could not find equivalent/similar concept in scalatest.不幸的是,我在 scalatest 中找不到等效/相似的概念。
Did I miss it?我错过了吗?
Is my approach wrong?我的方法错了吗? If so, how would u suggest me write a UT for the
createDatabaseIfNotExist
method?如果是这样,您如何建议我为
createDatabaseIfNotExist
方法编写 UT?
Is the current DbCreator
implementation not testable enough?当前的
DbCreator
实现是否不够可测试?
Many thanks for all help!非常感谢所有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.