You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
deftest_get_token_auto_create():
pact=Consumer('foo').has_pact_with(Provider("token"))
with (pact
.given("user foo has no token")
.upon_receiving("query token of user foo")
.with_request("GET", "/user/foo/token")
.will_respond_with(200, body=json.dumps({
"data": [ ]
}))
.given("user foo has no token")
.upon_receiving("create token for user foo")
.with_request("POST", "/user/foo/token")
.will_respond_with(200, body=json.dumps({
"token": "xxx"
}))
.given("user foo has a token")
.upon_receiving("query token for user foo")
.with_request("POST", "/user/foo/token")
.will_respond_with(200, body=json.dumps({
"data": [ {"data": "xx" }]
}))
):
):
token=get_token("foo")
tokens=list_tokens("foo")
assertlen(tokens) ==1andtokenintokens
the test above is not valid, because there two GET requests match path "/user/foo/tokens".
To fix it, maybe we need make mock service stateful. The state of service can be changed on specific match interaction, just like presudo code below,
deftest_get_token_auto_create():
pact=Consumer('foo').has_pact_with(Provider("token"))
with (pact
.given("user foo has no token") # first state as initial state implicitly
.upon_receiving("query token of user foo")
.with_request("GET", "/user/foo/token")
.will_respond_with(200, body=json.dumps({
"data": [ ]
}))
.given("user foo has no token")
.upon_receiving("create token for user foo")
.with_request("POST", "/user/foo/token")
.will_respond_with(200, body=json.dumps({
"token": "xxx"
}))
.transition_to("user foo has a token") # <----------------------- HERE !
.given("user foo has a token")
.upon_receiving("query token for user foo")
.with_request("POST", "/user/foo/token")
.will_respond_with(200, body=json.dumps({
"data": [ {"data": "xx" }]
}))
):
):
token=get_token("foo")
tokens=list_tokens("foo")
assertlen(tokens) ==1andtokenintokens
The text was updated successfully, but these errors were encountered:
consumer code,
test code,
the test above is not valid, because there two GET requests match path "/user/foo/tokens".
To fix it, maybe we need make mock service stateful. The state of service can be changed on specific match interaction, just like presudo code below,
The text was updated successfully, but these errors were encountered: