在 Scalatest 中使用什么代替符号? [英] What to use instead of symbols in scalatest?

查看:56
本文介绍了在 Scalatest 中使用什么代替符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In scalatest, you’re supposed to be able to test boolean properties using symbols like this:

iter shouldBe 'traversableAgain

但是这种表示法在最新版本的 scala 中已被弃用,所以现在你应该这样写:

But this notation have been deprecated in the most recent versions of scala, so now you’re supposed to write:

iter shouldBe Symbol("traversableAgain")

这有点难看.有没有更好的选择?

Which is a bit ugly. Is there any better alternative?

推荐答案

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

Consider BePropertyMatcher which provides type-safe predicate matching syntax

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

这篇关于在 Scalatest 中使用什么代替符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆