我目前正在做最崇高的编程工作,为 Json 编码/解码编写测试。我正在使用 Argonaut.io为 Json
和 Scalatest对于我的测试框架。在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/