Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
  }
}
like image 898
Karl Gjertsen Avatar asked Mar 13 '23 08:03

Karl Gjertsen


1 Answers

PactNet v2 already supports type and regex matching:

Type matching for whole body:

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

or for a property:

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

Regex matching:

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

There's also Match.MinType for arrays.

like image 57
foka Avatar answered Mar 20 '23 01:03

foka