Hey guys, I have a question. If I understand every...
# pact-net
n
Hey guys, I have a question. If I understand everything correctly, in nested models of the response we have to define exact values to verify. So if I have this ResonseModel:
class ResponseModel
{
public NestedModel NestedModel {get; set;}
}
class NestedModel
{
public string Name {get; set;}
}
I have to specify the
Name
's value as expected, for example with Request id 123, the
Name
is "Andy". I mean, I cannot somehow specify that it is just a string, because pact verifier checks the value (C# is strongly typed language, I cannot assign
IMatcher
to a
string
). So when I have to change the expected response to be:
class ResponseModel
{
public NestedModel[] NestedModel {get; set;}
}
I do not know the exact values of the other objects in the array. So how should I proceed in this situation?
m
sorry for the delay, I think you want to look at the concept of Matchers
they allow you to specify the shape of response rather than match on exact values
n
@Matt (pactflow.io / pact-js / pact-go) thanks for your response. Actually, I have an object with Dictionaries inside it, and I cannot figure out how to specify the pact. When I define the response as Dictionary<IMatcher, IMatcher>, for example:
var response = new {
JobInfosMap = new Dictionary<IMatcher, IMatcher> {
{Match.Type(900L), Match.Type(new JobInfo{Id = 900, Number = "900", Name = "Clogged Drain", StartDate = DateTime.Parse("6/13/2023 6:00:00 AM")} )}
}
}
the generated pact is:
"jobInfosMap": {
"pactNet.Matchers.TypeMatcher": {
"endDate": null,
"id": 900,
"name": "Clogged Drain",
"number": "900",
"startDate": "2023-06-13T06:00:00Z"
}
},
which means that the key is of type
pactNet.Matchers.TypeMatcher
, but the key is of type
long
as I define in
Match.Type(900L)
. So how to be with Dictionaries?
t
seems the key Matcher is not there? in the pact
n
in JSON it is just an object with keys and values, where the keys are just names, not changing values. It is in the Dictionary that the keys actually are changing values, and it's the C# deserializer that makes a Dictionary out of JSON object.
🎯 1
t
gotcha
m
I don't think you can apply matches to keys in Pact .NET. Do you have dynamic keys or dynamic values?
n
if by 'dynamic' you mean that they depend on request arguments, yes....in Dictionary both keys and values are dynamic
m
I mean, could you describe the possible set of keys with something like an OpenAPI file, or are they arbitrary in nature and could be basically anything?
If the former, then you shouldn’t need to match on the keys, but matching on values is normal. That’s what most of the Matchers ar efor
n
No, they are not a known set of keys. Although they are named keys, they are not names of fields. In fact they are values that change depending on the request. That's the problem..
m
gotcha
I don’t think Pact .NET has the matcher for that yet
but you could raise a PR