为什么我的LinkedHashMap合并键值?

时间:2017-10-01 20:05:13

标签: jenkins groovy

简介

我为Groovy中的Jenkins管道编写了一些代码,如下所示:

def sendNotifications (args) {
  // ... use args ...
  sendSlack(
    failOnError: true,
    color: '#00FF00',
    message: "foo",
    channel: "#mychan"
  )
}
// slackMessage not shown returns a string based on the args.

这段代码似乎在我的管道中运行得很好。现在我想测试它。所以我创建了一个模拟sendSlack的函数,因为我不希望我的测试发送Slack消息,我想检查传递给sendSlack的参数是否是预期的。所以我这样做了:

void testStubbedSendNotifications(Map inputArgs, ArrayList slackArgs) {
    def n = new Notifier()
    def sendSlackArgs = []
    n.metaClass.sendSlack = { Map x -> sendSlackArgs << x }
    n.sendNotifications(inputArgs)
    assert sendSlackArgs == slackArgs
}

这里发生的事情很简单。我没有调用sendSlack,而是将要调用的参数推送到ArrayList,然后检查以确保列表在断言中正确。所以这个函数接受调用sendNotifications的参数以及一个期望值列表,以便看到调用sendSlack作为结果。

问题

我的期望值仅在我的测试中被破坏。而不是像这样的哈希映射的参数:

[
  failOnError: true,
  color: '#00FF00',
  message: "foo",
  channel: "#mychan"
]

我反而得到了这个:

[
  failOnError: true,
  color: '#00FF00',
  "message:foo" : null,
  channel: "#mychan"
]

为什么消息的键和值会合并到密钥中?

0 个答案:

没有答案
相关问题