erikackermann / pact-consumer-swift

A Swift / ObjeciveC DSL for creating pacts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pact Consumer Swift

  • Swift, Carthage Example build: Swift, Carthage Example - Build Status
  • ObjeciveC, Git Submodules Example build: Build Status

This DSL is in very early stages of development, please bear with us as we give it some polish. Please raise any problems you have in the github issues.

This codebase provides a iOS DSL for creating pacts. If you are new to Pact, please read the Pact README first.

This DSL relies on the Ruby pact-mock_service gem to provide the mock service for the iOS tests.

Installation

Install the pact-mock_service

gem install pact-mock_service -v 0.5.1

Add the PactConsumerSwift library to your project

Using Carthage library manager

  • See the PactSwiftExample for an example project using the library with Carthage.

Using Git Submodules

Writing Pact Tests

Testing with Swift

Write a Unit test similar to the following (NB: this example is using the Quick test framework)

import PactConsumerSwift

...
  beforeEach {
    animalMockService = MockService(provider: "Animal Service", consumer: "Animal Consumer Swift", done: { result in
      expect(result).to(equal(PactVerificationResult.Passed))
    })
    animalServiceClient = AnimalServiceClient(baseUrl: animalMockService!.baseUrl)
  }

  it("gets an alligator") {
    var complete: Bool = false

    animalMockService!.given("an alligator exists")
                      .uponReceiving("a request for an alligator")
                      .withRequest(method:.GET, path: "/alligator")
                      .willRespondWith(status:200,
                                       headers: ["Content-Type": "application/json"],
                                       body: ["name": "Mary"])

    //Run the tests
    animalMockService!.run { (testComplete) -> Void in
      animalServiceClient!.getAlligator { (alligator) in
        expect(alligator.name).to(equal("Mary"))
        complete = true
        testComplete()
      }
    }

    // Wait for asynch HTTP requests to finish
    expect(complete).toEventually(beTrue())
  }

See the PactSpecs.swift for examples on how to expect error responses, how to use query params, etc.

Testing with Objective-C

Write a Unit test similar to the following

@import PactConsumerSwift;
...
- (void)setUp {
  [super setUp];
  XCTestExpectation *exp = [self expectationWithDescription:@"Pacts all verified"];
  self.animalMockService = [[MockService alloc] initWithProvider:@"Animal Provider"
                                                        consumer:@"Animal Service Client Objective-C"
                                                            done:^(PactVerificationResult result) {
    XCTAssert(result == PactVerificationResultPassed);
    [exp fulfill];
  }];
  self.animalServiceClient = [[OCAnimalServiceClient alloc] initWithBaseUrl:self.animalMockService.baseUrl];
}

- (void)testGetAlligator {
  typedef void (^CompleteBlock)();

  [[[[self.animalMockService given:@"an alligator exists"]
                             uponReceiving:@"oc a request for an alligator"]
                             withRequestHTTPMethod:PactHTTPMethodGET
                                              path:@"/alligator"
                                             query:nil headers:nil body:nil]
                             willRespondWithHTTPStatus:200
                                               headers:@{@"Content-Type": @"application/json"}
                                                  body: @"{ \"name\": \"Mary\"}" ];

  [self.animalMockService run:^(CompleteBlock testComplete) {
      Animal *animal = [self.animalServiceClient getAlligator];
      XCTAssertEqualObjects(animal.name, @"Mary");
      testComplete();
  }];

  [self waitForExpectationsWithTimeout:5 handler:nil];
}

Verifying your iOS client against the service you are integrating with

If your setup is correct and your tests run against the pack mock server, then you should see a log file here: $YOUR_PROJECT/tmp/pact.log And the generated pacts, here: $YOUR_PROJECT/tmp/pacts/...

For an end to end example with a ruby back end service, have a look at the KatKit example

More reading

  • The original pact library, with lots of background and guidelines Pact
  • The pact mock server that the Swift library uses under the hood Pact mock service
  • A pact broker for managing the generated pact files (so you don't have to manually copy them around!) Pact broker

Contributing

Please read CONTRIBUTING.md

About

A Swift / ObjeciveC DSL for creating pacts.

License:MIT License


Languages

Language:Swift 69.5%Language:Objective-C 16.2%Language:Ruby 6.8%Language:Shell 5.9%Language:C++ 1.6%