Skip to content

Commit b64a4c4

Browse files
committed
chore: clarify explanation of given
Signed-off-by: JP-Ellis <[email protected]>
1 parent 272da45 commit b64a4c4

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

MIGRATION.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ The v3 interface favours method chaining and provides more granular control over
103103
.with_body({'id': 123, 'name': 'Alice'}, content_type='application/json'))
104104
```
105105

106-
1. The new provider states can be streamlined by parameterizing them directly in the `given()` method. So instead of defining multiple variations of a `"user exists"` state, you can define it once and pass different parameters as needed. These can be passed as keyword arguments to `given()`, or as a dictionary in the second positional argument.
106+
1. In v2, there was limited support for parameterizing provider states, and each state variation often required a separate definition. For example, `given("user Alice exists with id 123")` and `given("user Bob exists with id 456")` would be two distinct states, which would then need to be handled separately in the provider state setup.
107+
108+
The new interface can now define a common descriptor that can be reused with different parameters: `.given("user exists", id=123, name='Alice')` and `.given("user exists", id=456, name='Bob')`. This approach reduces redundancy and makes it easier to manage provider states.
107109

108110
Some methods are shared across request and response definitions, such as `with_header()` and `with_body()`. Pact Python automatically applies them to the correct part of the interaction based on whether they are called before or after `will_respond_with()`. Alternatively, these methods accept an optional `part` argument to explicitly specify whether they apply to the request or response.
109111

src/pact/interaction/_base.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,16 @@ def given(
151151
```
152152
153153
This function can be called repeatedly to specify multiple provider
154-
states for the same Interaction. If the same state is specified with
155-
different parameters, then the parameters are merged together.
154+
states for the same Interaction. This allows for the same provider state
155+
to be reused with different parameters:
156+
157+
```python
158+
(
159+
pact.upon_receiving("a request")
160+
.given("a user exists", id=123, name="Alice")
161+
.given("a user exists", id=456, name="Bob")
162+
)
163+
```
156164
157165
Args:
158166
state:
@@ -170,11 +178,17 @@ def given(
170178
)
171179
```
172180
181+
These parameters are merged with any additional keyword
182+
arguments passed to the function.
183+
173184
kwargs:
174185
The additional parameters for the provider state, specified as
175186
additional arguments to the function. The values must be
176187
serializable using Python's [`json.dumps`][json.dumps]
177188
function.
189+
190+
These parameters are merged with any parameters passed in the
191+
`parameters` positional argument.
178192
"""
179193
if not parameters and not kwargs:
180194
pact_ffi.given(self._handle, state)

0 commit comments

Comments
 (0)