4

我正在测试微服务,并且正在使用PactNet创建和验证协议。我发现测试太脆弱了,因为验证者正在检查确切的值而不是验证类型。

例如,我正在针对 GitHub API 进行测试,并且测试有效。如果添加了新的 Repo,则该public_repos值会增加 1,并且测试失败。

有人用它来检查类型而不是具体值吗?

这是验证码:

[Test]
public void VerifyPact()
{

    // Arrange.
    var pactVerifier = new PactVerifier(() => { }, () => { });
    pactVerifier.ProviderState("There is call with the name 'karlgjertsen'");

    // Act.
    using (var client = new HttpClient { BaseAddress = new Uri("https://api.github.com/users/karlgjertsen") })
    {

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident / 6.0)");

        // Assert.
        pactVerifier
            .ServiceProvider("GitHub API", client)
            .HonoursPactWith("Pact Test")
            .PactUri(@"C:\Pact\pacts\pact_test-git_api.json")
            .Verify();

    }

}

这是 PACT 文件。

{
  "provider": {
    "name": "GitHub API"
  },
  "consumer": {
    "name": "PACT Test"
  },
  "interactions": [
    {
      "description": "A GET request for user deatils for 'karlgjertsen'",
      "provider_state": "There is call with the name 'karlgjertsen'",
      "request": {
        "method": "get",
        "path": "/users/karlgjertsen",
        "headers": {
          "Accept": "application/json"
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json; charset=utf-8"
        },
        "body": {
          "login": "karlgjertsen",
          "id": 4457667,
          "avatar_url": "https://avatars.githubusercontent.com/u/4457667?v=3",
          "gravatar_id": "",
          "url": "https://api.github.com/users/karlgjertsen",
          "html_url": "https://github.com/karlgjertsen",
          "followers_url": "https://api.github.com/users/karlgjertsen/followers",
          "following_url": "https://api.github.com/users/karlgjertsen/following{/other_user}",
          "gists_url": "https://api.github.com/users/karlgjertsen/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/karlgjertsen/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/karlgjertsen/subscriptions",
          "organizations_url": "https://api.github.com/users/karlgjertsen/orgs",
          "repos_url": "https://api.github.com/users/karlgjertsen/repos",
          "events_url": "https://api.github.com/users/karlgjertsen/events{/privacy}",
          "received_events_url": "https://api.github.com/users/karlgjertsen/received_events",
          "type": "User",
          "site_admin": false,
          "name": "Karl Gjertsen",
          "company": "infiniforms.io",
          "blog": "http://www.karlgjertsen.com",
          "location": "UK",
          "email": null,
          "hireable": null,
          "bio": null,
          "public_repos": 1,
          "public_gists": 0,
          "followers": 0,
          "following": 0,
          "created_at": "2013-05-17T14:05:30Z",
          "updated_at": "2016-03-07T19:39:58Z"
        }
      }
    }
  ],
  "metadata": {
    "pactSpecificationVersion": "1.1.0"
  }
}
4

2 回答 2

5

PactNet v2 已经支持类型和正则表达式匹配

全身类型匹配:

// IMockProviderService
.WillRespondWith(new ProviderServiceResponse
{
    Body = Match.Type(new { Id = 123, FirstName = "John" })
});

或对于财产:

.WillRespondWith(new ProviderServiceResponse
{
    Body = new { Id = 123, FirstName = Match.Type("John") }
});

正则表达式匹配:

.WillRespondWith(new ProviderServiceResponse
{
    Body = new { FirstName = Match.Regex("Jan", @"\A\w+\z") }
});

数组还有Match.MinType

于 2017-12-08T12:54:09.883 回答
2

似乎 Pact 的 v1 完全是关于测试提供者,您可以在其中控制状态并将系统置于确定性状态。

v2 旨在处理您提到的场景。

目前 PactNet 不支持Pact 规范的 v2 。

2020 年 2 月 26 日更新 - PactNet 现在支持 V2.0 ( https://github.com/pact-foundation/pact-net )

于 2016-05-13T07:26:19.930 回答