在 scalatest 中用什么代替符号?
What to use instead of symbols in scalatest?
在 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)
}
}
还有一个相关问题Replacement for using symbols as property matchers for 2.13+ #1679
在 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)
}
}
还有一个相关问题Replacement for using symbols as property matchers for 2.13+ #1679