I'm trying to write contract tests for an object t...
# pact-net
s
I'm trying to write contract tests for an object that contains a dictionary of objects. I want to verify the entries respect my contract. The keys are changing between the consumer and provider. Right now, the matching rules of my contract are trying to find specific keys in the body of my message such as
"$.properties.desired.deploymentsRemovals['4JgEA5GCeqwVsu6Qada9XS'].appId"
Is it possible to write contract tests in my situation? I'm using the PactNet nuget version 4.0.0-beta.3. Using a matcher on the key such as
Copy code
deployments = new Dictionary<object, object> {
                            {Match.Type("6XKISmGMWynbwM52mxov6S"),  
                                new {...
produces a contract searching for "pactNet.Matchers.TypeMatcher" as the key
Copy code
"deployments": {
          "pactNet.Matchers.TypeMatcher": {
đź‘‹ 1
y
Hi @Sophie Bossé thanks for making it along and thanks for the really good example. I'm not overly familiar with .NET, but will look across our demo/examples and see if I we have an example of correct use, or spin up an example to try. Will promise to look into it today, but if you were able to provide an repro repository that would be amazing.
Not sure if you've seen our dotnet example workshop? But it pre the v4 beta, so https://github.com/DiUS/pact-workshop-dotnet-core-v3 This is a decent upgrade guide for v4 https://github.com/pact-foundation/pact-net/blob/master/docs/upgrading-to-4.md so this will be my first port of call :)
There is an example here of using the Type matcher https://github.com/pact-foundation/pact-net/blob/cb7693dd4e8dcc4dee301d1a7617b8ed5[…]25/samples/Messaging/Consumer.Tests/StockEventProcessorTests.cs The pact it generates is here https://github.com/pact-foundation/pact-net/blob/cb7693dd4e8dcc4dee301d1a7617b8ed5[…]ts/pacts/Stock%20Event%20Consumer-Stock%20Event%20Producer.json Are you able to fork this repo and change this example to match your data structure and we can see what is generated and help further?
s
great idea, I'm doing that right away
y
Awesome, thank you so much, I can see the issue, I am just about to sign off for the weekend (its nearly 7pm here), so I will hope to get some OSS time. I've pinged a message in here, and we will get you sorted
s
thank you very much. I'm about to sign off for the weekend too anyway
Any news on your side?
y
Apologies for the delay, I have been fighting the tide this week. Friday is looking pretty clear so will start poking around and see where I get too!
c
Hi Sophie, as you are using the PactNet nuget version 4.0.0-beta.3 the upgrade guide may be a more helpful example for you, as a lot of aspects are different from the examples you would have seen written with PactNet v3: https://github.com/pact-foundation/pact-net/blob/master/docs/upgrading-to-4.md V4 is still considered beta and the documentation and examples need some improvement to support it, as such you could also try downgrading to the latest v3 PactNet package which will be what the other examples are using.
s
I need to do contract test for a messaging system, not an API, that's why I want to use the new version.
👍 1
c
Ah yeah, that makes sense. It is just about to be released officially anyway so there shouldn't be many functional issues at this stage. Let me know how you go following the upgrading guide to v4 🙂
y
Hey @Adam Rodger (pact-net), sorry for the direct tag, but wondering if you might be able to help out when you have a moment, I imagine this might hopefully be a quick one for you to answer, and there is a direct repo against the Pact-net repo as well, so hopefully shouldn't take too much of your time. Thank you 🙏
a
I'm on holiday this week, but having a very quick look through that on my phone I'm not sure you can really get that to work. The key needs to just be a string, not a matcher.
s
Essentially, my messages contain dictionaries of objects, I want to validate the objects structures. But I can't produce a contract that doesn't require my key to always be the same.