当数组内容因每个项目而异时,使用Pact.eachLike()

时间:2017-05-25 08:57:05

标签: pact

您好我使用Pact NPM生成消费者测试https://www.npmjs.com/package/pact

我使用以下代码生成pact.json:

         var my_tbody = document.getElementById('my-tbody');





                    for(var i=0; i<data.length; i++){
                          var row = my_tbody.insertRow( my_tbody.rows.length ); 

                          var col1 = row.insertCell(0);
                          col1.innerHTML = data[i].section1;

                          var col2 = row.insertCell(1);
                          col2.innerHTML = data[i].section2;

                          var col3 = row.insertCell(2);
                          col3.innerHTML = data[i].product;

                          var col4 = row.insertCell(3);
                          col4.innerHTML = data[i].product_description;

                          var col5 = row.insertCell(4);
                          col5.innerHTML = data[i].status;

                          var col6 = row.insertCell(5);
                          col6.innerHTML = data[i].rate;

                          var col7 = row.insertCell(6);
                          col7.innerHTML = data[i].start_date;

                          var col8 = row.insertCell(7);
                          col8.innerHTML = data[i].completion_date;

                          var col9 = row.insertCell(8);
                          col9.innerHTML = data[i].proceed_detail;

                          var col10 = row.insertCell(9);
                          col10.innerHTML = data[i].schedule_detail;

                          var col11 = row.insertCell(10);
                          col11.innerHTML = data[i].issues;

                          var col12 = row.insertCell(11);
                          col12.innerHTML = data[i].in_charge;

                          var col13 = row.insertCell(12);
                          col13.innerHTML = data[i].se;
                    }

            }

它生成以下testconsumer-testprovider.json文件:

provider
    .addInteraction({
        state: 'test',
        uponReceiving: 'a test,
        withRequest: {
          method: 'GET',
          path: '/test'
        },
        willRespondWith: {
            status: 200,
            headers: { 'Content-Type': 'application/json' }
            body: {
                "company": like("My big company"),
                "factories": eachLike({
                    "location": like("Sydney"),
                    "capacity": like(5)
                },{min:1})
            }
        }
    })
    .then(function(){ done(); });

但是,当我们针对以下提供的输出进行测试时,我们会遇到geopCoords的错误,因为它是一个意外的键/值:

{
  "consumer": {
    "name": "TestConsumer"
  },
  "provider": {
    "name": "TestProvider"
  },
  "interactions": [
    {
      "description": "a request for loans",
      "providerState": "broker is logged in, list all loans",
      "request": {
        "method": "GET",
        "path": "/test"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/vnd.hal+json"
        },
        "body": {
          "company": "My big company",
          "factories": [
            {
              "location": "Sydney",
              "capacity": 5
            }
          ]
        },
        "matchingRules": {
          "$.headers.Content-Type": {
            "match": "regex",
            "regex": "application\\/.*json.*"
          },
          "$.body.company": {
            "match": "type"
          },
          "$.body.factories": {
            "min": 1
          },
          "$.body.factories[*].*": {
            "match": "type"
          },
          "$.body.factories[*].location": {
            "match": "type"
          },
          "$.body.factories[*].capacity": {
            "match": "type"
          }
        }
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "3.0.0"
    }
  }
}

因此我们只允许在数组中使用意外的键/值,因为我们只测试所需的键/值,并且我们不希望将来在将新值添加到我们的提供程序时,pact测试失败。 / p>

1 个答案:

答案 0 :(得分:0)

支持您所描述的方案,请参阅https://github.com/pact-foundation/pact-js/tree/master/examples/e2e以获取示例。

如果您要删除,请说出eligibility对象并运行测试,一切仍然有效。

如果您仍然遇到麻烦,请在pact-js存储库中提出一个缺陷,我们将深入了解它。