【问题标题】:Discard Non Unit Value Compiler Error For Assertion放弃断言的非单位值编译器错误
【发布时间】:2020-09-23 13:11:41
【问题描述】:

当我根据 sbt-toplecat 打开编译器标志时,出现以下编译器错误。

await(myService(request).value).isLeft shouldBe true

现在编译器抱怨:

discarded non-Unit value
await(myService(request).value).isLeft shouldBe true
                                       ^

这个 shouldBe 匹配器来自 Scalatest:

    def shouldBe(right: Any): Assertion = {
      if (!areEqualComparingArraysStructurally(leftSideValue, right)) {
        val (leftee, rightee) = Suite.getObjectsForFailureMessage(leftSideValue, right)
        val localPrettifier = prettifier // Grabbing a local copy so we don't attempt to serialize AnyShouldWrapper (since first param to indicateFailure is a by-name)
        indicateFailure(FailureMessages.wasNotEqualTo(localPrettifier, leftee, rightee), None, pos)t
      }
      else indicateSuccess(FailureMessages.wasEqualTo(prettifier, leftSideValue, right))
    }

我需要做什么来解决这个问题?显然,断言将是真或假,所以我总是得到一个非单位值,然后将其丢弃。

【问题讨论】:

    标签: scala scalatest scalamock


    【解决方案1】:

    考虑放宽测试配置的编译器标志,因为此代码不在生产环境中运行。也许只是明确地为测试配置设置一些你可能喜欢的标志

    Test / scalacOptions := Seq(
      // few flags I want for tests
    )
    

    【讨论】:

    • 您的意思是说由于 scalatest 的工作方式,对此无能为力吗?
    • @Mojo 请提供完整测试的示例源代码。这在 ScalaTest 中应该不是真正的问题。很可能您在某处放置了 Unit 返回类型,而它实际上应该是 Assertion 返回类型。
    猜你喜欢
    • 2011-10-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多