Hi team, I'm trying to write a basic test using Pa...
# pact-swift
v
Hi team, I'm trying to write a basic test using PactConsumerSwift. However it is failing when Pact is trying to connect to the server.
Copy code
test(): failed - Error setting up pact: Could not connect to the server.
I tried to change the localhost address as indicated in some fixes, but it didn't work. Here is my code, I'd be glad to receive your suggestions:
Copy code
import Foundation
import PactConsumerSwift
import XCTest
import Hamcrest

class SimulatedSaleIntegration: XCTestCase {
    
    var mockService: MockService?
    let apiClient: SimulatedSaleViewModel = SimulatedSaleViewModel()
    let successfulSale = Sale(amount: 5000.0, vat: 0.19, tip: 0.15, paymentMethod: "", location: "")
    
    
    override func setUp() {
        super.setUp()
        mockService = MockService(provider: "calculator-service", consumer: "Example-app-for-testing")
        apiClient.setServer(url: mockService!.baseUrl)
      }
    
    override func tearDown() {
        super.tearDown()
      }
    
    func testGetSuccessfulSimulatedSale() {
        mockService!
            .uponReceiving("A request to get a simulated sale")
            .withRequest(
                method: .GET,
                path: "/calculator",
                query: [...]
            )
            .willRespondWith(
                status: 200,
                headers: ["Content-Type": "application/json"],
                body: [...]
            )
        
        mockService!.run { (testComplete) -> Void in
            self.apiClient.getSimulatedSale(sale: self.successfulSale)
            assertThat(self.apiClient.simulatedSale?.fee, equalTo(168.34))
            testComplete()
        }
    }
}
In logs, all I can see is
interactions/verification
request could not being performed
Copy code
Task <5AEFFE81-A9B5-4574-9DFB-EE823F7B83B9>.<2> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x6000015ed710 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: en0[802.11], uses wifi, _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <5AEFFE81-A9B5-4574-9DFB-EE823F7B83B9>.<2>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
  "LocalDataTask <5AEFFE81-A9B5-4574-9DFB-EE823F7B83B9>.<2>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=<https://0.0.0.0:1234/interactions/verification>, NSErrorFailingURLKey=<https://0.0.0.0:1234/interactions/verification>, _kCFStreamErrorDomainKey=1}
Anyone has an idea about this issue? Thanks in advace 😉
🙌 1
a
This new library https://github.com/surpher/PactSwift uses an in process server so it avoids this type of problem. It also supports v3.
v
Hi Andy, thanks for your suggestion, I'm gonna check the library