简体   繁体   English

契约使用者测试仅用于生成合同json文件吗?

[英]Is pact consumer test for generating contract json files only?

Is pact consumer test for generating contract json files? 契约消费者是否可以测试生成合同json文件?

I am studing pact and got qurioused about what is the consumer test for? 我正在研究协议,并对消费者测试的目的感到好奇? It tests the response that the test class defindes. 它测试测试类定义的响应。

In my code below. 在下面的代码中。 I defined a response with 200 and simple body, then Test it calling by mockProvider. 我用200和简单的主体定义了一个响应,然后通过模仿提供者对其进行测试。 seems useless. 似乎没用。 Anybody please give me some guides. 有人请给我一些指导。

public class PactTest {

  @Rule
  public PactProviderRuleMk2 mockProvider
        = new PactProviderRuleMk2("test-provider", "localhost", 8017, this);


  @Pact(consumer = "test-consumer")
  public RequestResponsePact createPact(PactDslWithProvider builder){
    Map<String, String> headers = new HashMap<>();

    return builder
            .given("test Get")
                .uponReceiving("GET REQUEST")
                .path("/pact")
                .method("GET")
            .willRespondWith()
                .status(200)
                .headers(headers)
                .body("{\"condition\": true, \"name\":\"tom\"}")
            .toPact();
  }

  @Test
  @PactVerification
  public void givenGet_whenSendRequest_shouldReturn200withProperHeaderAndBody() {
    ResponseEntity<String> res = new RestTemplate()
                                        .getForEntity(mockProvider.getUrl()+"/pact", String.class);

    assertThat(res.getStatusCode().value()).isEqualTo(200);
  }
}

Short answer - no. 简短答案-不。

Calling the mock API in the test independent of your actual consumer code is worthless (as you imply), because it is a self-fulfilling prophecy. 与您的实际使用者代码无关地在测试中调用模拟API是毫无用处的(正如您暗示的那样),因为这是一个自我实现的预言。 Pact is designed to test the collaborating service on the Consumer side; Pact旨在测试消费者方面的协作服务; the adapter code that makes the call to the Provider. 调用提供程序的适配器代码。

Typically, this call will pass through things like data-access layers and other intermediates. 通常,此调用将通过数据访问层和其他中间对象。 Your Pact tests would use a service that uses these, and the benefit is that the contract gets defined through this process, that is guaranteed to be up-to-date with consumer needs, because it is generated via your code. 您的Pact测试将使用使用这些服务的服务,并且好处是可以通过此过程定义合同,因为该合同是通过代码生成的,因此保证可以满足消费者的需求。

We've just updated the docs today, perhaps that helps. 我们今天才更新文档 ,也许有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM