Pact — Provider States

Emerson Rodrigues
Quick Mobile Labs
Published in
2 min readFeb 1, 2017

Continuing our series on Pact, we are going to make a proof of concept of how to use its Provider States.

By default, each pact check must be done in isolation, therefore, without storing any session data. This is a major stalemate when the tests depend on an earlier result.
Tests that depend on some previous context make you think of the famous integration test, which is where you want to get away from.

So, how do you test a request that depends on a pre-existing context? Using the Pact States Provider!

The Provider States allows you to set up data without a provider by injecting directly before an interaction is made, enabling a response as the consumer expects. The state name is fixed in the “given” clause of a consumer interaction, and then used to find the block of code to execute.

This is the place that you have a downtime, a 500 error, or any other state that you want to test.

This allows you to make the same request with different expected responses.

The non-state provider text should make sense if you read it as follows:
Given a resource with id 21”, when you receive a request to search for a resource by id 21:

{
“Method”: “get”,
“Path”: “/ resource / 21”,
}

The provider must respond {“Status”: 200, “resource”: []}

Supplier example Status that changes the result of the request

This.setUp (function (done) {
ChangeState (500, {error: ‘Argh !!!’, done);
});

Description of how it works.

We hope you like today’s post, reaching us on any doubt.

--

--