在Scalatest和Argonaut.io中导致编译失败的模糊隐式转换
发布时间:2020-12-16 18:33:44 所属栏目:安全 来源:网络整理
导读:我目前正在做最高尚的编程工作,为Json编码/解码编写测试.我正在为Json使用 Argonaut.io,为我的测试框架使用 Scalatest.在scalatest下,在断言验证期间使用===会在发生故障时提供附加信息,而使用==只是给出了这个org.scalatest.exceptions.TestFailedException
我目前正在做最高尚的编程工作,为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做的不多,那就不会太糟糕了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- shell获取CentOS6.8网卡上的IP地址
- bootstrapfileinput实现文件自动上传
- unix – Diff和“–GTYPE-group-format”
- scala – 播放框架:Dependecy Inject Action Builder
- vim – 显示vi或emacs生产力的视频
- 基于Metronic的Bootstrap开发框架经验总结(14)--条码和二
- angular – XHR错误(404 Not Found)加载http:// localhost
- scala – 在编译时获取类?
- scala – Spark上的动态集代数
- Angular 2 利用Router事件和Title实现动态页面标题