Lifecycle for examples
The overall lifecycle of a contract test is:
-
Define a contract
- Define the examples
- Run tests to confirm the examples are correct
- Upload contracts to a broker
-
Verify the contract:
- Download contract from the broker
- Verify the contract
- Publish verification result to the broker
Zooming in on the run tests step
In order to define examples, it's useful to understand how the examples will be run. The general lifecycle for all ContractCase Examples is:
- Define example
- Set up mock
- Call state setup handlers (if any)
- Invoke
trigger
to call mock- Compare mock expectations
- Return appropriate response
- Invoke
testResponse
ortestErrorResponse
function - If everything was successful, record successful example in contract.
If any of the steps or comparisons failed, the whole contract is failed.
- Call state teardown handlers (if any)
- (Later, at the provider side, during verification) re-run steps 2-5.
ContractCase handles most of the lifecycle for you. Not all ContractCase Example types need to have all lifecycle steps defined by the user - for example, ContractCase defines and invokes its own triggers when testing HTTP servers. For information on which lifecycle you should define for each ContractCase Example type, see the example types documentation.
Next we will discuss how to define examples inside runExample
.