Unit Test function that uses PromiseKit
<p>I have  the following function which I want to test:</p>

<pre><code>func getProduct(ean: String) -&gt; Promise&lt;Product&gt; {
    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")
                }
            }
        }
    }
}
</code></pre>

<p>Here is the mock manager I created.</p>

<pre><code>class MockStoreProductProductManager: BSProductManagerSwift {
    var storeProductWasCalled = false
    var storeProductJSON: JSON?
    let deferredPromise = Promise&lt;Product&gt;.pendingPromise()

    override func storeProduct(json: JSON) -&gt; Promise&lt;Product&gt; {
        storeProductWasCalled = true
        storeProductJSON = json

        return deferredPromise.promise
    }
}
</code></pre>

<p>And here is the non-working test.</p>

<pre><code>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")
}
</code></pre>

<p>I know the mock manager is getting called but my expectation never gets fulfilled (<code>exp.fulfill()</code> is not called) and I don't know why. Any ideas?</p>