gpt4 book ai didi

scala - Specs2:集合匹配器上的类型不匹配编译错误

转载 作者:行者123 更新时间:2023-12-04 17:55:23 26 4
gpt4 key购买 nike

使用 specs2 2.3.12,Scala 2.11.6。我在一个示例中看到类型不匹配错误,我觉得我从文档中得到了其他信息。代码如下:

val newUsers: Seq[(String, User)] = response.newUsers.toSeq
newUsers must contain((email: String, user: User) => (user.email.toString must be_==(email))).forall

我收到以下错误:

[error] <redacted>/UserSpec.scala:561: type mismatch;
[error] found : org.specs2.matcher.ContainWithResult[(String, com.nitro.models.User) => org.specs2.matcher.MatchResult[String]]
[error] required: org.specs2.matcher.Matcher[Seq[(String, com.nitro.models.User)]]
[error] newUsers must contain((email: String, user: User) => (user.email.toString must be_==(email))).forall
[error] ^
[error] one error found
[error] (api/test:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Aug 3, 2015 10:07:04 AM

这些是 examples我在关注:

// contain matcher accepting a function
Seq(1, 2, 3) must contain((i: Int) => i must be_>=(2))
Seq(1, 2, 3) must contain(be_>(0)).forall // this will stop after the first failure

我绝对可以重写测试来绕过这个错误,但我想了解我哪里出错了。感谢您的指点!

最佳答案

这里发生了几件事。最重要的一点是,当您针对元组进行测试时,您的函数返回一个 MatchResult[String],编译器不会帮助您意识到这一点,因为如果你返回了一个 MatchResult[(String, User)]

您可以构建自己的 MatchResult,但是(在我看来)您最好还是创建一个 Matcher[(String,User)],这真的很简单。

如果您将此添加到您的规范中:

  def matchingEmail: Matcher[(String, User)] =
(pair: (String, User)) => (pair._1 == pair._2.email, s"email ${pair._1} did not match user ${pair._2}")

您可以简单地用 newUsers must contain(matchingEmail).forall 调用它

关于scala - Specs2:集合匹配器上的类型不匹配编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31793037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com