Scala 如何让intellij对失败的测试提供文本差异比较

Scala 如何让intellij对失败的测试提供文本差异比较,scala,intellij-idea,scalatest,intellij-14,Scala,Intellij Idea,Scalatest,Intellij 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":

我正在用一些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并没有为我提供方便的查看文本功能差异的功能

我想可能是因为我在比较两个物体

  val responseBody = responseAs[JsValue]
  responseBody should be(viewingByAccountIdResponseJson)
但是把它改成

assert(responseBody.toString() === viewingByAccountIdResponseJson.toString())
也不允许我做文本比较

有没有办法配置intellij来实现这一点

(我目前正在使用带有匹配器的FlatSpec)

注:这与本问题有关


但是,即使使用intellij可能选择的语法,它也不起作用。

我认为目前不可能做到这一点,而截至目前,这是一个功能请求:

正如Bruce在庆祝7岁生日时提到的,我们中的一些人已经失去了希望(仍然不要忘记+1)。下面是一个丑陋的脚本,它允许您稍微缓解这个问题。只需复制
这不等于那行
并将其馈送到此脚本的stdin:

| scalatest-diff
cat>~/bin/scalatest diff
#!/usr/bin/perl
我的$in=加入(“,”);
如果($in=~m/[^::::+\:(.+?)不等于(.+)/so){
my$this=$1;
我的$that=2美元;
$this=~s/,/,\n/g;
$that=~s/,/,\n/g;
打开(thisFile,“>”,“/tmp/this”);
打开(该文件“>”,“/tmp/that”);
打印此文件$this;关闭此文件;
打印该文件$that;关闭该文件;
exec(“vimdiff/tmp/this/tmp/that”);
}
chmod a+x~/bin/scalatest差异

另外,请随意将vimdiff更改为您最喜欢的差异。

问题在于ScalaTest试图在显示差异之前对左右对象进行良好的格式化

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


它必须在中“显示差异”{
“你好
|世界!
|“.”。条边距应相等
你好
|缩放测试。
|“.stripMargin)
}
隐式类完全相等[T](leftSideValue:T){
//Like应该相等,但不尝试用方括号标记字符串中的差异,
//这样IntelliJ可以显示一个适当的差异。
def shouldEqualPlainly(right:Any)(隐式相等:相等[T]):断言=
如果(!equality.areEqual(leftSideValue,right)){
抛出新的TestFailedException(
(e:StackDeptheException)=>一些“${leftSideValue}”不等于“${right}”),
没有一个
位置,这里
)
}否则就成功了
}
这将允许您单击并提供如下内容:

使用Idea 14.0.2,ScalaTest 2.2.1和Scala 2.11.4,如果我比较两个字符串
x
y
使用
assert(x==y)
x应该是(y)
我看到
“a[bc]de”不等于“a[bc]de”
,我认为这是您想要的行为。你确定没有什么特殊的配置妨碍你吗?或者你只是在使用ScalaTest的旧版本?我相信在任何情况下,它只适用于字符串。它是否提供了比较两个文本突出显示差异的链接?我发现java+groovy可以做到这一点,只是在Scala测试中没有。这意味着比较json字符串,找出不同之处是一个相当大的挑战。不,不是。您有任何理由相信ScalaTest在IntelliJ的上下文之外会这样做吗?这里的问题是,IntelliJ看到输出时,预期的全文和实际的全文已经被删除。
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