pact go contract testing - should I mock business logic

420 views Asked by At

I'm researching Pact contract testing and I've got a question whether the business logic should be mocked during this test.

Let's say I have an endpoint

POST /user

{
  "name": "first name"
  "accountID": "12345678"
}

Assume I've written the consumer test and verified it with a provider. However, to verify it I need to have the actual API running. The problem is that when I'm running in my local environment accountID of 12345678 does exist. However, when another dev will run it in his env - it might not exist.

My question is - how do I get around this problem? Do I need to mock the business logic inside the endpoint? But in that case doesn't it just become a payload validator essentially which protobuf could do?

1

There are 1 answers

0
Syed Muhammad Dawoud Sheraz On

The consumer tests will only be responsible for generating a valid contract. The contract is generated by mocking the API responses. You typically mock what certain fields or actions you would expect/need against the request.

In provider, you will verify that contract by hitting against a running provider instance. Now, it is very common that a provider would need to be in a particular state before verifying an interaction of the contract. To achieve that, you can use the Provider States to make sure the DB is in the right state. You should define state setup per interaction separately and make sure the DB is reset before/after each interaction to avoid one state data affecting the other.

References: