在 Scalatest 中使用什么代替符号?

编程入门 行业动态 更新时间:2024-10-19 16:37:29
本文介绍了在 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 中使用什么代替符号?

本文发布于:2023-11-25 15:29:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   Scalatest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!