运行测试时,由于404,PactNet Provider测试失败

时间:2018-07-13 09:18:36

标签: pact pact-net

我有一个已成功创建契约文件的使用者:

{
  "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();
}

2 个答案:

答案 0 :(得分:0)

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

请在https://docs.pact.io/getting-started/provider-stateshttps://docs.pact.io/implementation-guides/ruby/provider-states#non-ruby-applications这里阅读有关提供商状态的信息

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

我看到您还遇到一些编码问题(Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8),我相信这已在基础库的最新版本(称为“独立契约”)中得到解决,因此请确保您拥有最新版本契约网。如果您已经拥有最新版本的pact-net,请提出一个问题以升级独立版本。

答案 1 :(得分:0)

对于那些仍然为问题所困扰的人,还会发生"\xC3" from ASCII-8BIT to UTF-8错误,因为您的提供者的响应无法进行编码,例如您的提供商将返回500,其中包含text / html响应。