scala - 如何使用 AsyncFeatureSpec?
问题描述
我正在尝试编写异步测试,AsyncFeatureSpec
如下所示:
import java.net.ConnectException
import org.scalatest._
final class SapRsSpec extends AsyncFeatureSpec
with Matchers
with GivenWhenThen {
feature("Kafka distribution to a server via websocket") {
scenario("Send data to SAP server when Kafka is DOWN") {
Given("Kafka server is NOT active")
When("consumer client get started")
val ex = SenderActor.run
Then("print message `Failed to connect to Kafka`")
ex.failed map { ex =>
intercept[ConnectException](ex)
}
}
scenario("Send data to a server when Kafka is UP") {
Given("Kafka server is active")
When("consumer client get started")
Then("print message `Successfully connected to Kafka`")
}
}
}
编译器抱怨:
Error:(20, 36) type mismatch;
found : java.net.ConnectException
required: org.scalatest.compatible.Assertion
intercept[ConnectException](ex)
Error:(27, 11) type mismatch;
found : Unit
required: scala.concurrent.Future[org.scalatest.compatible.Assertion]
Then("print message `Successfully connected to Kafka`")
在第一种情况下,我想针对收到的异常类型进行测试,但我不知道该怎么做。
解决方案
shouldBe
matcher 可用于检查接收到的异常的类型,如下所示
ex.failed map { ex =>
ex shouldBe a [ConnectException]
}
这应该解决第一个编译器错误。关于第二个错误,我们必须在断言或匹配器表达式中结束异步样式测试:
...第二个测试将被隐式转换
Future[Assertion]
并注册。隐式转换是 fromAssertion
toFuture[Assertion]
,因此您必须在某些 ScalaTest 断言或匹配器表达式中结束同步测试。如果测试不会以其他方式结束 typeAssertion
,则可以放在succeed
测试的末尾。
因此succeed
,在测试体的末尾写入将暂时满足类型检查器,直到我们编写实际检查:
scenario("Send data to a server when Kafka is UP") {
...
succeed // FIXME: add proper checks
}
推荐阅读
- python - 试图将摩尔斯电码翻译成英文
- javascript - 仅显示一定数量的 wordpress 评论
- matlab - 如何在matlab中截断字符串
- javascript - 我应该选择哪个 redux 中间件?
- python - python将文件读入数据结构
- ios - 苹果自定义布局算法如何保持纵横比?
- react-native - 在本机反应中将类类型应用于状态中的属性
- spring-boot - Spring boot REST - Swagger Docs 使用 Spring FOX 并在同一构建中生成客户端 SDK
- python - 使用 .ftplib 和 python 将数字(var)写入 FTP 服务器上的 .txt
- python - 生成抽象基类 Python