14

我正在用一些 ScalaTest 匹配器编写 Scala 测试。

当我的测试失败时,intellij 会说类似

{"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","position":27},{"id":"20","position":341},{"id":"19","position":33}]} was not equal to {"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","timestamp":"2015-01-28T11:55:44.494Z","content":"Episode","position":27},{"id":"20","timestamp":"2015-01-19T11:55:44.494Z","content":"Program","position":341},{"id":"19","timestamp":"2015-01-17T11:55:44.494Z","content":"Episode","position":33}]}
org.scalatest.exceptions.TestFailedException: {"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","position":27},{"id":"20","position":341},{"id":"19","position":33}]} was not equal to {"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","timestamp":"2015-01-28T11:55:44.494Z","content":"Episode","position":27},{"id":"20","timestamp":"2015-01-19T11:55:44.494Z","content":"Program","position":341},{"id":"19","timestamp":"2015-01-17T11:55:44.494Z","content":"Episode","position":33}]}
at    org.scalatest.MatchersHelper$.newTestFailedException(MatchersHelper.scala:160)
at org.scalatest.Matchers$ShouldMethodHelper$.shouldMatcher(Matchers.scala:6231)
at org.scalatest.Matchers$AnyShouldWrapper.should(Matchers.scala:6265)
...

但是,intellij 并没有让我方便地看到文本功能的差异。

我想可能是因为我在比较 2 个对象

  val responseBody = responseAs[JsValue]
  responseBody should be(viewingByAccountIdResponseJson)

但将其更改为

assert(responseBody.toString() === viewingByAccountIdResponseJson.toString())

也不允许我进行文本比较。

有没有办法配置intellij来做到这一点?

(我目前正在使用带有 Matchers 的 FlatSpec)

注意:这与这个问题 格式化输出有关,以便 Intellij Idea 显示两个文本的差异

但是,即使使用推荐的 intellij 可能会采用的语法,它也不起作用。

4

3 回答 3

5

我认为目前这是不可能的,截至目前,这是一个功能请求:

https://youtrack.jetbrains.com/issue/SCL-4867

于 2015-03-06T10:42:45.200 回答
2

由于布鲁斯提到的intellij 功能请求庆祝它的 7 岁生日,我们中的一些人已经失去了希望(仍然不要忘记 +1)。这是一个丑陋的脚本,可以让您稍微缓解这个问题。只需复制This was not equal to That行并将其提供给此脚本的标准输入:

| scalatest-diff
cat > ~/bin/scalatest-diff
#!/usr/bin/perl

my $in = join("", <STDIN>);
if( $in =~ m/[^:]+\: (.+?) was not equal to (.+)/so ) {
    my $this = $1;
    my $that = $2;
    $this =~ s/,/,\n/g;
    $that =~ s/,/,\n/g;
    open(thisFile, ">", "/tmp/this");
    open(thatFile, ">", "/tmp/that");
    print thisFile $this; close thisFile;
    print thatFile $that; close thatFile;
    exec("vimdiff /tmp/this /tmp/that");
}
<Ctrl-D>
chmod a+x ~/bin/scalatest-diff

PS 随意将 vimdiff 更改为您喜欢的不同。

于 2019-05-31T23:33:42.513 回答
0

问题是 ScalaTest 在显示差异之前试图很好地格式化左右对象。

您可以通过定义自己的匹配器来解决这个问题,该匹配器会抛出一个不那么花哨的TestFailedException. 以下是如何在基于匹配器的测试中实现这一点的草图:


  it must "show diff" in {
    """Hello
      |World!
      |""".stripMargin shouldEqualPlainly
      ("""Hello
         |ScalaTest.
         |""".stripMargin)
  }

  implicit class PlainEquality[T](leftSideValue: T) {
    // Like should equal, but does not try to mark diffs in strings with square brackets,
    // so that IntelliJ can show a proper diff.
    def shouldEqualPlainly(right: Any)(implicit equality: Equality[T]): Assertion =
      if (!equality.areEqual(leftSideValue, right)) {
        throw new TestFailedException(
          (e: StackDepthException) => Some(s"""${leftSideValue} did not equal ${right}"""),
          None,
          Position.here
        )
      } else Succeeded
  }

这将让您单击并为您提供如下内容: 在此处输入图像描述

于 2020-03-17T12:47:18.550 回答