在 scalatest 中,您应该能够使用如下符号测试布尔属性:
iter shouldBe 'traversableAgain
但这种表示法在最新版本的 scala 中已被弃用,所以现在你应该这样写:
iter shouldBe Symbol("traversableAgain")
这有点难看。还有更好的选择吗?
考虑BePropertyMatcher它提供类型安全的谓词匹配语法
iter should be (traversableAgain)
例如
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.{BePropertyMatchResult, BePropertyMatcher}
import org.scalatest.matchers.should.Matchers
trait CustomMatchers {
val traversableAgain = new BePropertyMatcher[Iterator[_]] {
def apply(left: Iterator[_]): BePropertyMatchResult =
BePropertyMatchResult(left.isTraversableAgain, "isTraversableAgain")
}
}
class BePropertyMatcherExampleSpec extends AnyFlatSpec with Matchers with CustomMatchers {
"BePropertyMatcher" should "provide type-safe checking of predicates" in {
Iterator(42, 11) should be (traversableAgain)
}
}
还有一个相关问题替换使用符号作为 2.13+ 的属性匹配器 #1679
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)