1

我是 Pact 的新手,也是 groovy 的新手。

我想写一个协议把它交给一个提供者。

提供者应使用字符串数组( ["foo", "bar", "foobar"].

这是我的工作状态,它被剥离到必要的部分,但仍然是可执行的:

import au.com.dius.pact.consumer.groovy.PactBuilder
import groovyx.net.http.RESTClient
import spock.lang.Specification

class MyPact extends Specification {
    def "some test"() {
        given:
        when:

        def pactBuilder = new PactBuilder()
        pactBuilder {
            serviceConsumer "consumer"
            hasPactWith "provider"
            port 1234
            given('some provider state')
            uponReceiving('a request')
            withAttributes(method: 'get', path: '/my/endpoint')
            willRespondWith(status: 200)
            withBody(["foo", "bar", "foobar"])
        }

        pactBuilder.run() { mockServer ->
            RESTClient('http://localhost:1234/').get(path: '/my/endpoint')
        }

        then: 1 == 1

    }
}

此测试生成以下协议文件:

{
  "provider": {
    "name": "provider"
  },
  "consumer": {
    "name": "consumer"
  },
  "interactions": [
    {
      "description": "a request",
      "request": {
        "method": "GET",
        "path": "/my/endpoint"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": [
          "foo",
          "bar",
          "foobar"
        ]
      },
      "providerStates": [
        {
          "name": "some provider state"
        }
      ]
    }
  ],
  "metadata": {
    "pact-specification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "3.5.6"
    }
  }
}

Pact 文件希望提供者始终以["foo", "bar", "foobar"]

我想要的是一个通用的字符串数组。

当我尝试以下操作时:

withBody {
    key eachLike (3, "foo")
}

生成的 Pact 文件如下所示:

{
  "provider": {
    "name": "provider"
  },
  "consumer": {
    "name": "consumer"
  },
  "interactions": [
    {
      "description": "a request",
      "request": {
        "method": "GET",
        "path": "/my/endpoint"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": {
          "key": [
            "foo",
            "foo",
            "foo"
          ]
        },
        "matchingRules": {
          "body": {
            "$.key": {
              "matchers": [
                {
                  "match": "type"
                }
              ],
              "combine": "AND"
            }
          }
        }
      },
      "providerStates": [
        {
          "name": "some provider state"
        }
      ]
    }
  ],
  "metadata": {
    "pact-specification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "3.5.6"
    }
  }
}

有matchingRules,这看起来不错。但是根级别上有一个对象,我需要数组作为根。

有没有办法将数组作为根元素,字符串按类型匹配而不是按值匹配?

4

0 回答 0