pact go contract testing - should I mock business logic

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 answer

  • answered 2022-01-21 05:38 Syed Muhammad Dawoud Sheraz

    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:

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum