-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow example data for stub server to be different than template for verification #378
Comments
Unfortunately, the way the matchers work today, is that you provide a single representative sample that can be used in the list object. But I do think there is merit in allowing the scenario you are talking about, you're not the first to ask, and it makes sense when used with the stub server. @uglyog what do you think about supporting this? I'm wondering if there are other similar matchers that could allow a similar form (e.g. |
This is a separate problem but also needs to be handled. It sounds like a bug on the surface of it. |
With Pact-JVM, the DSL has a second form for all the like matching functions that allows you to specify the number of examples. I think having this option would help here? I.e.: .willRespondWith({
status: StatusCodes.OK,
body: Matchers.atLeastOneLike(
{ foo: Matchers.integer(42) },
3
]),
}); However, they would all have the same example value (42). .willRespondWith({
status: StatusCodes.OK,
body: Matchers.atLeastOneLike(
{ foo: Matchers.integer() }, // Use a random integer generator
3 // have 3 example values
]),
}); You could also have a form where you don't provide the example value, and the Pact framework could use a generator to replace the values at runtime with random ones: |
We have that option already also (you can optionally set the I think the ask is that the user can specify heterogenous examples that are more useful in a stub context (e.g. different rows of data). The contract could store these examples, but each example given must also match the matcher. e.g. .willRespondWith({
status: StatusCodes.OK,
body: Matchers.atLeastOneLike(
{ "name": "Moe" }, // All examples must have the key `name` that is a string
[ // Additional examples to use in the current test / serialised pact
{ "name": "Marge" },
{ "name": "Apu" },
{ "name": "Homer" },
]
]),
}); This way, when the contract is used in a stub scenario, the data will be more realistic. What do you think? |
Yes, absolutely! Random data would not be beneficial, and probably hard to generate if it's not just a number but a complex object that also needs to make sense in the problem domain.
It would be great if such an API would be supported! I pasted your code, looks like it's currently not. |
That is a pretty actual issue. I work from FE side, defining Pacts for BE API.
I have min 0 as we want to allow empty array and count 2 as I want 2 examples. Another option is to code a matcher explicitly with my fixtures data:
That will use my fixtures, but it will implicitly match that the expected array length is 2 elements, and this is not what we want. Would be great to have 4th parameter to provide example values:
given entry1 and entry2 have the same object structure and the property value will be taken only if no value was specified for a matcher. In this case, the address would be taken from entry1 and entry2, but buildingIds will use a specified matcher, as it has a value set in the matcher. Also, imagine I need a meaningful entry to be matched, such as a rectangle coordinates, which is 4 integers. Would be great to have 5th parameter: |
Hello! This would be incredibly useful in allowing our Frontend's be able to test more accurately and provide better isolated testing. Are there any updates on this? |
No update. If you are interested, it would require a change in the core. I'll move the issue there as a feature request. |
By stub server, I assume you mean the mock server provided by Pact? |
In the OP I was referring to the stub server that provides examples for the frontend people. |
Ok, do you mean this one? https://github.com/pact-foundation/pact-stub-server |
Yes, I think so. Answering this while boarding an airplane. Cannot check my project's source code right now. |
Feature description
We use the stub server extensively to provide frontend developers with good examples. A common use case is that APIs return a list of objects:
We were unable to find a way to specify the following for the interaction that would return this set of data:
{ "foo": <integer> }
templateThe interaction for the example above includes this:
This provides all 3 examples to the fontend developer as intended. But since the
like
matcher does not validate the actual array length, the API would be verified even if it returned[]
.The main reason we use Pact is to make sure the schema between frontend and API is compatible. Since the API developers can "work around" being really verified by returning an empty array this is rather unfortunate.
Something we tried is using
atLeastOneLike
, but that will return the array encapsulated in another array when using the stub server:Another variant ensures that the API contains an array with length >=1, but the fontend developer just gets 1 example to work with:
Maybe we're doing it wrong?
The text was updated successfully, but these errors were encountered: