使用PromiseKit的单元测试功能

时间:2016-08-10 13:29:49

标签: ios swift unit-testing promisekit

我有以下要测试的功能:

func getProduct(ean: String) -> Promise<Product> {
    return Promise { fullfill, reject in
        let urlString = BSConstants.BarcodeScanner.productEndpoint.stringByAppendingString(ean)
        serviceClient.GET(urlString, failure: { (error) in
            reject(error!)
        }) { (response) in
            if let json = response {
                self.storeProduct(json).then ({ returnedProduct in
                    fullfill(returnedProduct)
                }).error { returnedError in
                    // HANDLE ERROR
                    print("HANDLE ERROR")
                }
            }
        }
    }
}

这是我创建的模拟经理。

class MockStoreProductProductManager: BSProductManagerSwift {
    var storeProductWasCalled = false
    var storeProductJSON: JSON?
    let deferredPromise = Promise<Product>.pendingPromise()

    override func storeProduct(json: JSON) -> Promise<Product> {
        storeProductWasCalled = true
        storeProductJSON = json

        return deferredPromise.promise
    }
}

这是非工作测试。

func testGetProduct_WithInStockEAN_RetunsValidInstockJSON() {
    // Given
    let mockManager = MockStoreProductProductManager(serviceClient: BSNetworkingServiceClient())
    let inputEanString = "1234567891234"

    var expectedJSON: JSON? = nil
    if let jsonPath = NSBundle(forClass: self.dynamicType).pathForResource("getproduct-instock-response", ofType: "json") {
        let data = NSData(contentsOfFile: jsonPath)!
        stub(http(.GET, uri: "some-url-i-have-removed"), builder: jsonData(data))

        let jsonResponse = JSON(data: data)
        expectedJSON = jsonResponse["data"]
    }

    // When

    let exp = expectationWithDescription("Return instock product JSON")

    mockManager.getProduct(inputEanString).then { returnedProduct in
        exp.fulfill()
    }.error { error in
        XCTFail(" Instock JSON should have been returned")
        exp.fulfill()
    }

    waitForExpectationsWithTimeout(10, handler: nil)

    // Then
    XCTAssertTrue(mockManager.storeProductWasCalled, " Expected storeProduct function to be called but it wasn't")
    XCTAssertEqual(mockManager.storeProductJSON, expectedJSON, " Incorrect JSON passed to storeProduct")
}

我知道模拟经理正在被召唤,但我的期望永远不会得到满足(exp.fulfill()没有被召唤)而且我不知道为什么。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

原来我几乎就在那里,但我不得不让我的模拟经理返回一个实现并传递ProductNSManagedObject子类)。所以现在我的模拟经理看起来像这样,我的测试工作:

class MockStoreProductProductManager: BSProductManagerSwift {
    var storeProductWasCalled = false
    var storeProductJSON: JSON?
    let deferredPromise = Promise<Product>.pendingPromise()

    override func storeProduct(json: JSON) -> Promise<Product> {
        storeProductWasCalled = true
        storeProductJSON = json

        let managedObjectContext = setUpInMemoryManagedObjectContext()
        let entity = NSEntityDescription.entityForName("Product", inManagedObjectContext: managedObjectContext)
        let product = Product(entity: entity!, insertIntoManagedObjectContext: managedObjectContext)
        deferredPromise.fulfill(product)

        return deferredPromise.promise
    }
}