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

我目前正在做最高尚的编程工作,为Json编码/解码编写测试.我正在为Json使用 Argonaut.io,为我的测试框架使用 Scalatest.在scalatest下,在断言验证期间使用===会在发生故障时提供附加信息,而使用==只是给出了这个org.scalatest.exceptions.TestFailedException被抛出..但是,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

use = =但是提供了一个干净的编译和传递.有没有办法为编译器提供关于使用哪个转换或转换顺序的提示

解决方法

我会在这里使用ScalaTest的版本.一种方法是明确应用转换:

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

但是,如果你在一个文件中多次使用===,那就会有很多不愉快的样板.另一种方法是从一般导入中排除Scalaz转换:

import scalaz._,Scalaz.{ ToEqualOps => _,_ }

您也可以切换到Scalaz的单点导入,并确保不通过scala.Syntax.equal._拉入ToEqualOps.我承认我发现点菜进口有时会产生很大的痛苦,但是如果你在测试中对Scalaz做的不多,那就不会太糟糕了.

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...