Created
December 18, 2017 01:40
-
-
Save BeauNouvelle/c54be792f6d2abb327bb38fb4e4629b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func testRetrieveProductsValidResponse() { | |
// we have a locally stored product list in JSON format to test against. | |
let testBundle = Bundle(forClass: type(of: self)) | |
let filepath = testBundle.pathForResource("products", ofType: "txt") | |
let data = Data(contentsOfFile: filepath!) | |
let urlResponse = HTTPURLResponse(url: URL(string: "https://anyurl.doesntmatter.com")!, statusCode: 200, httpVersion: nil, headerFields: nil) | |
// setup our mock response with the above data and fake response. | |
MockSession.mockResponse = (data, urlResponse: urlResponse, error: nil) | |
let requestsClass = RequestManager() | |
// All our network calls are in here. | |
requestsClass.Session = MockSession.self | |
// Replace NSURLSession with our own MockSession. | |
// We still need to test as if it's asynchronous. Because well, it is. | |
let expectation = XCTestExpectation(description: "ready") | |
// For this test, no need to pass in anything useful since it doesn't affect our mocked response. | |
// This particular function fetches JSON, converts it to custom objects, and returns them. | |
requestsClass.retrieveProducts("N/A", products: { (products) -> () in | |
XCTAssertTrue(products.count == 7) | |
expectation.fulfill() | |
}) { (error) -> () in | |
XCTAssertFalse(error == Errors.NetworkError, "Its a network error") | |
XCTAssertFalse(error == Errors.ParseError, "Its a parsing error") | |
XCTFail("Error not covered by previous asserts.") | |
expectation.fulfill() | |
} | |
waitForExpectations(timeout: 3.0, handler: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment