Search code examples
c#testingmicroservicespactpact-net

Validate types in PactNet


I am testing micro services and I'm using PactNet to create and validate pacts. I am finding that the tests are too brittle, as the verifier is checking for exact values and not verifying the types.

For example, I am testing against the GitHub API and the test works. If a new Repo is added, the public_repos value increases by one and the test fails.

Is anyone using this to check the types instead of the concrete values?

Here is the verification code:

[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();

    }

}

And here's the PACT file.

{
  "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"
  }
}

Solution

  • It seems that v1 of Pact is all about testing providers where you can control the state and put the system in a deterministic state.

    v2 is aiming to work with the scenarios you mention.

    At this time PactNet does not support v2 of the Pact specification.

    Update 26/02/2020 - V2.0 is now supported for PactNet (https://github.com/pact-foundation/pact-net)