在 scalatest 中用什么代替符号?

Wil*_*lQu 5 symbols scala scalatest

在 scalatest 中,您\xe2\x80\x99应该能够使用如下符号测试布尔属性:

\n
iter shouldBe 'traversableAgain\n
Run Code Online (Sandbox Code Playgroud)\n

但这个表示法在最新版本的 scala 中已被弃用,所以现在你\xe2\x80\x99应该写:

\n
iter shouldBe Symbol("traversableAgain")\n
Run Code Online (Sandbox Code Playgroud)\n

这有点难看。还有更好的选择吗?

\n

Mar*_*lic 4

考虑BePropertyMatcher哪个提供类型安全谓词匹配语法

iter should be (traversableAgain)
Run Code Online (Sandbox Code Playgroud)

例如

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

还有一个相关问题Replacement for using Symbols as property matchers for 2.13+ #1679