# CDC testing made simple with Pact

## What is Pact ?

{% hint style="success" %}
Pact is ***a contract testing tool***. Contract testing is a way to ensure that services (such as an API provider and a client) can communicate with each other. Without contract testing, the only way to know that services can communicate is by using expensive and brittle integration tests.
{% endhint %}

Pact provides a testing button for your code, allowing you to safely confirm that your applications will work together without having to deploy the world first.

It is available on a lot of platforms and languages :

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZGeBVGeLQ-rPBD7Wm%2Fimage.png?alt=media\&token=b85d6ff5-433e-4d31-8f2e-417826ad4569)

Pact comes with its own specification regarding the format of the contracts between consumers and providers :

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZGipyI_wBQFQK2FNs%2Fimage.png?alt=media\&token=b5177d76-b87a-436a-9bde-31418845087f)

## How can we implement a new service with Pact ? <a href="#f329" id="f329"></a>

Because we use a CDC approach we start with the consumer and here is what we want as a consumer :

{% hint style="info" %}
*As a consumer, I want a Rest API that returns “Hello world v1” when I GET “api/v1.0/helloworld”*
{% endhint %}

### 1) Start with the consumer <a href="#id-15af" id="id-15af"></a>

Let’s implement a simple consumer by :

* Writing Test(s)
* Define interactions / Expectations

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZGsz4C6BIEP5RthEX%2Fimage.png?alt=media\&token=d620582c-605c-4501-a271-85414b4a4ed9)

**Concretely** :

* Build your client : here we call it **consumer-js**

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZH6eIBf52NvemM668%2Fimage.png?alt=media\&token=9c7cba51-de63-4421-808d-a289ed460c71)

* Describe and configure the interactions (behaviors and expectations)

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZHFEpR5h2jwqJU2t1%2Fimage.png?alt=media\&token=de91b3f6-9984-4c1b-9074-1c7e6cd73cb8)

Once you have defined it you can run the tests. From those expectations Pact will create a contract file : a JSON file that will look like this :

### 2) Contract Driven Development <a href="#id-0d10" id="id-0d10"></a>

* Design Your **API** From The Contract
* Write a RED test

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZI7X93PTObmkZSvzc%2Fimage.png?alt=media\&token=f617fd02-8304-4e42-a07a-8963f6efee92)

#### **The RED test (in .NET Core)**

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZIG6j4aqfr3gVTHZS%2Fimage.png?alt=media\&token=3a7061d0-af11-44cc-8e47-7895d163ca36)

The test will be launched and interactions will be verified based on the contract that has been defined on the consumer side.

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZIWHjKMqSgquoVnVo%2Fimage.png?alt=media\&token=dca64687-5b32-469a-b652-cf2e06fb59f1)

#### **Make the test GREEN**

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZIc3q9ZXD_RW4p4Wy%2Fimage.png?alt=media\&token=5dd3540f-33d3-4efd-a93c-334ed8295a50)

## Features offered by Pact <a href="#f090" id="f090"></a>

In this simple example we have just checked a GET interaction with simple string matcher but you can achieve much more complex and more realistic verifications with the tool :

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZIihTAsvt11EwQaYY%2Fimage.png?alt=media\&token=08a199a7-dc4c-477a-9ad1-b2bd96881738)

## S.W\.O.T <a href="#eaa4" id="eaa4"></a>

Based on our experience with Pact here is our S.W\.O.T :

![](https://1936518372-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAffO8xa1ZWmgZvfeK2%2F-MBZGFJF_Cekb-HrdooB%2F-MBZIsDx96nSrwFBGqFs%2Fimage.png?alt=media\&token=dd846be5-c447-4aba-90b0-c10a137666e5)

## Resources <a href="#abaa" id="abaa"></a>

Please find the code samples in this repository : [**https://github.com/agilepartner/pact-sandbox**](https://github.com/agilepartner/pact-sandbox)

To go further check the implementation guide in your favorite language : <https://docs.pact.io/implementation_guides>

## Conclusion <a href="#id-2692" id="id-2692"></a>

Pact is a really good tool to start a CDC approach. It is easy to use but there is one problem that needs to be solved before starting using it in enterprise :

{% hint style="danger" %}
**Where the hell do we store the contracts ?**
{% endhint %}
