将 HavePropertyMatcher 用于 ScalaTest 中的集合元素? [英] Using a HavePropertyMatcher for collection elements in ScalaTest?

查看:53
本文介绍了将 HavePropertyMatcher 用于 ScalaTest 中的集合元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 ScalaTest 的 FeatureSpec 几天了,我试图了解是否可以使用内置匹配器定义以下规范(如果不能,我如何编写合适的自定义匹配器).

I've been using ScalaTest's FeatureSpec for a couple of days now and I'm trying to understand if it's possible to define the following spec using the built-in matchers (and if not, how I can write a suitable custom matcher).

假设我有 Book 类:

Suppose I have the class Book:

case class Book(val title: String, val author: String)

在我的测试中,我有一个书籍清单:

and in my test I have a List of books:

val books = List(Book("Moby Dick", "Melville"))

现在,我想指定书籍列表应包含标题为Moby Dick"的书籍.我想写一些类似的东西:

Now, I would like to specify that the books list should contain a book with the title "Moby Dick". I would like to write something like:

books should contain (value with title "Moby Dick")  

我似乎无法从文档和代码中弄清楚是否可以在 ScalaTest 中表达此要求.有没有人遇到过类似的情况?

I can't seem to figure out from the docs and code if it's possible to express this requirement in ScalaTest. Has anyone ran into a similar situation?

推荐答案

目前还没有,不过你很快就能做这样的事情.你现在可以做的是:

Not currently, though you will be able to do something like this very soon in the future. What you can do now is something like:

books.exists(_.title == "Moby Dick") should be (true)

这篇关于将 HavePropertyMatcher 用于 ScalaTest 中的集合元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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