scala - 在 Scalatest 和 Argonaut.io 中导致编译失败的模糊隐式转换

标签 scala scalaz scalatest

我目前正在做最崇高的编程工作,为 Json 编码/解码编写测试。我正在使用 Argonaut.ioJsonScalatest对于我的测试框架。在scalatest下,使用===如果发生故障,断言验证期间会提供附加信息,同时使用 ==简单地给出这个 org.scalatest.exceptions.TestFailedException was thrown. .然而,scala 编译器并不高兴。这是代码:

val default = new Broadcast("default", "default", "default")
test("Should parse out network when present") {
  val hcursor = testHCursor(jsonPath + "complete-broadcast.json")
  val actualNetwork = Parser.BroadcastDecodeJson(hcursor)
    .getOr(default)
    .network
  assert(actualNetwork === "ESPNU")
}

这喷出这个:

[info] Compiling 1 Scala source to /home/vagrant/waltercamp/waltercamp-dataservice/target/scala-2.10/test-classes...
[error] /home/vagrant/waltercamp/waltercamp-dataservice/src/test/scala/io/ptx/waltercamp/schedules/BroadcastParserSuite.scala:16: type mismatch;
[error]  found   : actualNetwork.type (with underlying type String)
[error]  required: ?{def ===(x$1: ? >: String("ESPNU")): ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error]  both method ToEqualOps in trait ToEqualOps of type [F](v: F)(implicit F0: scalaz.Equal[F])scalaz.syntax.EqualOps[F]
[error]  and method convertToEqualizer in trait Assertions of type (left: Any)BroadcastParserSuite.this.Equalizer
[error]  are possible conversion functions from actualNetwork.type to ?{def ===(x$1: ? >: String("ESPNU")): ?}
[error]     assert(actualNetwork === "ESPNU")
[error]            ^
[error] one error found
[error] (test:compile) Compilation failed

使用==但是提供了干净的编译和传递。有没有办法向编译器提供有关使用哪种转换或转换顺序的提示?

最佳答案

我会在这里使用 ScalaTest 的版本。一种方法是显式应用转换:

assert(convertToEqualizer(actualNetwork) === "ESPNU")

但是,这有点令人不快,并且如果您使用 === 会涉及大量重复的样板文件。在一个文件中多次。另一种方法是从一般导入中排除 Scalaz 转换:
import scalaz._, Scalaz.{ ToEqualOps => _, _ }

您也可以切换到 Scalaz 的点菜导入,并确保您不要引入 ToEqualOps通过 scala.syntax.equal._ .我承认我发现点菜进口有时很难维护,但如果你在测试中没有用 Scalaz 做太多事情,这也不会太糟糕。

关于scala - 在 Scalatest 和 Argonaut.io 中导致编译失败的模糊隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473059/

相关文章:

scala - 如何通过 sbt 使用 flatspec 只运行在 scala 中没有特定标签的测试?

scala - sbt 条件 if else 样式配置

scala - 具有最小样板的无形 StringLike 特征

list - 将列表拆分为多个具有固定元素数量的列表

验证:未找到隐式 scalaz.Bind

scala - Scala 中的 ISO 宏

arrays - Scala:将文件逐行读入列表数组

scala - 基于akka流条件的替代流

scala - 在 Scala 中是否可以明确提供上下文绑定(bind)?

scala - 我应该使用Scalatest : Spec versus WordSpec versus FlatSpec,中的BDD?