我正在测试微服务,并且正在使用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"
}
}