Search code examples
c#testingpactpact-net

Pactnet: How can I create a nulable field in the contract (IMatcher)


I need to define a consumer contract that has a field that is nullable, can be send or not, but if it will be sent I need to ensure the object field types. I didn't find anything to solve it in the PactNet 4.5.

Example:

Match.Type(new
{
                
    Id = Match.Type(123),
    Value = Match.Type(1.5),
    InternalProperty = Match.Type(new
    {
        Name = Match.Type("Test")
    }) //Property must be nullable
}

Solution

  • Pact doesn't support optional fields in this way. See https://docs.pact.io/faq#why-is-there-no-support-for-specifying-optional-attributes for more.

    You need to write one test for when the value is present, and another for when it is not.