DiUS / pact-consumer-swift

A Swift / ObjeciveC DSL for creating pacts.
MIT License
98 stars 43 forks source link

`Matcher.somethingLike` not working according to the docs? #85

Closed Nonnus closed 4 years ago

Nonnus commented 4 years ago

We are in the process of covering our mobile apps with contract testing using Pact tests. After successful initial setup and tests, we started to take a step further and start testing type matching instead of strict value matching but have hit a roadblock as we cannot find swift documentation on how to use this feature and any sample codes we could find followed the same pattern as our code

this is how we are setting up helloProvider (we tested on top of the sayHello sample code):

helloProvider!.uponReceiving("a request for hello")
              .withRequest(method:.GET, path: "/sayHello")
              .willRespondWith(status: 200,
                               headers: ["Content-Type": "application/json"],
                               body: ["reply": "Hello",
                                      "user": Matcher.somethingLike("Me")])

Pact tests target compiles fine but then fails when running: .../Pact Tests/Pact_Tests.swift:27: error: -[Pact_Tests.Pact_Tests testItSaysHello] : Error setting up pact: Error

This is obviously related to "user": Matcher.somethingLike("Me") as when we replace it with "user": "Me" the test runs fine, but we could not find any way to make it work with type matching as we need to

I assume Matcher.somethingLike works fine and its our implementation that is not ideal, but honestly speaking we did our best to search for solution across your docs but found very little regarding type matching in swift.

Thanks in advance for any support, we promise to do our best to spread the knowledge

andrewspinks commented 4 years ago

Hi @Nonnus what you are doing looks OK, as far as I can tell. We have a very similar test here: https://github.com/DiUS/pact-consumer-swift/blob/master/Tests/PactConsumerSwiftTests/PactSpecs.swift#L245

There should be a generated log file, assuming it is getting that far. By default the log file is generated in the ./tmp/pact.log (unless you've configured the mock server to startup with a different --log ).

If you could share the contents of the log file, or even better, a sample project reproducing the problem, we could probably help you more.

Nonnus commented 4 years ago

Hi @andrewspinks

Thanks a lot for your super quick reply and for assuring me I was on the right track

After reading your message I started to notice the behaviour was quite flaky and then noticed I had started pack-mock-service from command line and it was still running, regardless if tests were running or not, Xcode open or not, etc

So I stoped the service, ran the tests again from project and lo and behold, it all seems fine now and Matcher.somethingLike is not generating the correct pact json:

"response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": {
          "reply": "Hello",
          "user": "Me"
        },
        "matchingRules": {
          "$.body.user": {
            "match": "type"
          }
        }
      }

I will now close this ticket but want to take the chance to thank you again for your help