1

我有一个消费者已经成功创建了一个契约文件:

{
  "consumer": {
    "name": "CakeService"
  },
  "provider": {
    "name": "CoolPersonService"
  },
  "interactions": [
    {
      "description": "A GET request to get a person who is cool",
      "providerState": "There is a person who has IsCool set to 'true'6",
      "request": {
        "method": "get",
        "path": "/CoolPersonService/persons/e674a2d0-57e6-471d-96dd-a91c0077283a",
        "headers": {
          "X-Requested-With": "XMLHttpRequest",
          "CallerName": "Pact_Test@Local"
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json; charset=utf-8"
        },
        "body": {
          "FirstName": "Some",
          "LastName": "Name",
          "IsCool": true
        }
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "2.0.0"
    }
  }
}

问题是对提供者的测试。它得到一个 404

/CoolPersonService/persons/e674a2d0-57e6-471d-96dd-a91c0077283a

但是如果我使用 HTTP 客户端向该路由发送 GET,我会收到 200 响应并返回预期的数据。

这是测试运行的输出:

Test Name:  EnsureSomethingApiHonoursPactwithConsumer
Test Outcome:   Failed
Result Message: 
Test method PactTests.PactTests.EnsureSomethingApiHonoursPactwithConsumer threw exception: 
PactNet.PactFailureException: Pact verification failed. See output for details. 
If the output is empty please provide a custom config.Outputters (IOutput) for your test framework, as we couldn't write to the console.
Result StandardOutput:  
[2018-07-13 10:59:05] INFO  WEBrick 1.3.1
[2018-07-13 10:59:05] INFO  ruby 2.2.2 (2015-04-13) [i386-mingw32]
..........++++++
..........++++++
[2018-07-13 10:59:07] INFO  
[2018-07-13 10:59:07] INFO  WEBrick::HTTPServer#start: pid=109760 port=9222
[2018-07-13 10:59:07] ERROR Errno::ECONNRESET: An existing connection was forcibly closed by the remote host.
    C:/redacted/PactTests/bin/Debug/pact-win32/lib/ruby/lib/ruby/2.2.0/openssl/buffering.rb:61:in `sysread'
    C:/redacted/PactTests/bin/Debug/pact-win32/lib/ruby/lib/ruby/2.2.0/openssl/buffering.rb:61:in `fill_rbuff'
    C:/redacted/PactTests/bin/Debug/pact-win32/lib/ruby/lib/ruby/2.2.0/openssl/buffering.rb:301:in `eof?'
    C:/redacted/PactTests/bin/Debug/pact-win32/lib/vendor/ruby/2.2.0/gems/webrick-1.3.1/lib/webrick/httpserver.rb:80:in `run'
    C:/redacted/PactTests/bin/Debug/pact-win32/lib/vendor/ruby/2.2.0/gems/webrick-1.3.1/lib/webrick/server.rb:191:in `block in start_thread'
INFO: Reading pact at C:/redacted/PactTests/Pacts/cakeservice-coolpersonservice.json

Verifying a pact between CakeService and CoolPersonService
  Given There is a person who has IsCool set to 'true'6
    A GET request to get a person who is cool
      with GET /CoolPersonService/persons/e674a2d0-57e6-471d-96dd-a91c0077283a
        returns a response which
DEBUG: Setting up provider state 'There is a person who has IsCool set to 'true'6' for consumer 'CakeService' using provider state server at https://localhost/CoolPersonService/provider-states
I, [2018-07-13T10:59:11.805597 #102868]  INFO -- request: POST https://localhost/CoolPersonService/provider-states
D, [2018-07-13T10:59:11.805597 #102868] DEBUG -- request: User-Agent: "Faraday v0.15.0"
Content-Type: "application/json"
I, [2018-07-13T10:59:23.352124 #102868]  INFO -- response: Status 200
D, [2018-07-13T10:59:23.352625 #102868] DEBUG -- response: content-type: "application/json; charset=utf-8"
server: "redacted"
x-powered-by: "redacted"
date: "Fri, 13 Jul 2018 08:59:23 GMT"
connection: "close"
content-length: "84"
          has status code 200 (FAILED - 1)
          has a matching body (FAILED - 2)
          includes headers
            "Content-Type" which equals "application/json; charset=utf-8" (FAILED - 3)

Failures:

  1) Verifying a pact between CakeService and CoolPersonService Given There is a person who has IsCool set to 'true'6 A GET request to get a person who is cool with GET /CoolPersonService/persons/e674a2d0-57e6-471d-96dd-a91c0077283a returns a response which has status code 200
     Failure/Error: expect(response_status).to eql expected_response_status

       expected: 200
            got: 404

       (compared using eql?)

  2) Verifying a pact between CakeService and CoolPersonService Given There 

is a person who has IsCool set to 'true'6 A GET request to get a person who is cool with GET /CoolPersonService/persons/e674a2d0-57e6-471d-96dd-a91c0077283a returns a response which has a matching body

>Failure/Error: expect(response_body).to match_term expected_response_body, diff_options, example
         Encoding::UndefinedConversionError:
           "\xC3" from ASCII-8BIT to UTF-8
      3) Verifying a pact between CakeService and CoolPersonService Given There is a person who has IsCool set to 'true'6 A GET request to get a person who is cool with GET /CoolPersonService/persons/e674a2d0-57e6-471d-96dd-a91c0077283a returns a response which includes headers "Content-Type" which equals "application/json; charset=utf-8"
         Failure/Error: expect(header_value).to match_header(name, expected_header_value)
           Expected header "Content-Type" to equal "application/json; charset=utf-8", but was "text/html; charset=utf-8"
    1 interaction, 1 failure
    Failed interactions:
    To re-run just this failing interaction, change the verify method to '.Verify(description: "A GET request to get a person who is cool", providerState: "There is a person who has IsCool set to 'true'6")'. Please do not check in this change! # A GET request to get a person who is cool given There is a person who has IsCool set to 'true'6

这是提供者的测试方法:

[TestMethod]
[TestCategory("Pact")]
public void EnsureSomethingApiHonoursPactwithConsumer()
{
    const string serviceUri = "https://localhost/CoolPersonService";

    var config = new PactVerifierConfig
    {
        Verbose = true
    };

    IPactVerifier pactVerifier = new PactVerifier(config);
    pactVerifier
        //.ProviderState($"{serviceUri}/provider-states")
        .ProviderState($"{serviceUri}/provider-states")
        .ServiceProvider("CoolPersonService", serviceUri)
        .HonoursPactWith("CakeService")
        .PactUri(@"C:\repos\Pact\API_CoolPersonService\PactTests\Pacts\cakeservice-coolpersonservice.json")
        .Verify();
}
4

2 回答 2

1

如果您对资源执行 GET 并得到 404,对我来说,这听起来像是服务器上不存在预期的数据 - 这意味着提供者状态尚未正确设置。

请在此处阅读有关提供商状态https://docs.pact.io/getting-started/provider-states和此处https://docs.pact.io/implementation-guides/ruby/provider-states#non-ruby-applications

如果您对同一路由执行 POST 并从 GET 收到预期的响应,则可能是您刚刚创建了一个新资源,并且它正在返回新创建资源的主体。

我看到您也遇到了一些编码问题(Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8)我相信这已在最新版本的底层库(称为“独立协议”)中得到修复,因此请确保您拥有最新版本的 pact-net。如果您已经拥有最新版本的 pact-net,请提出要升级独立版本的问题。

于 2018-07-14T02:48:45.980 回答
0

对于那些仍然被这个问题困扰的人,"\xC3" from ASCII-8BIT to UTF-8错误也会发生,因为您的提供者的响应无法被编码,例如您的提供者返回一个带有 text/html 响应的 500。

于 2020-08-25T09:42:45.097 回答