Last active
April 28, 2017 18:09
-
-
Save chelseatroy/9e397f79fd9982d9380b22a6396f41a7 to your computer and use it in GitHub Desktop.
Testing Asynchronous Calls With FutureKit
This file contains hidden or 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
import FutureKit | |
import XCTest | |
import Nimble | |
@testable import VillainApp | |
class VillainsMasterViewControllerTest: XCTestCase { | |
var viewController: VillainsMasterViewController! | |
var mockVillainService: MockVillainService! | |
var somePromise: Promise<VillainResponse>? | |
class MockVillainService: VillainService { | |
var stubbedPromise: Promise<VillainResponse>? | |
override func getVillainList() -> Future<VillainResponse> { | |
return stubbedPromise.future | |
} | |
} | |
override func setUp() { | |
super.setUp() | |
somePromise = Promise<VillainResponse>() | |
mockVillainService = MockVillainService() | |
mockVillainService.stubbedPromise = somePromise | |
} | |
... | |
func testSuccessfulCallDisplaysVillains() { | |
let villainsDict = ["villainCount": 3, | |
"villains":[ | |
["name": "Cruella DeVille", | |
"specialty": "unethical treatment of animals"], | |
["name": "Ursula", | |
"specialty": "fine print"], | |
["name": "Elphaba", | |
"specialty": "faking own death"], | |
] | |
] | |
let villainsResponse = VillainResponse.init(fromJSON: JSON(villainsDict)) | |
villainsController.loadFromStoryboard(mockVillainService) | |
assertThat(villainsController.view, present()) | |
let window = UIWindow() | |
window.rootViewController = controller | |
window.makeKeyAndVisible() | |
controller.didTapLoginButton(NSObject()) | |
expect(self.somePromise?.isCompleted).toNot(beTrue()) | |
somePromise?.completeWithSuccess(villainsResponse!) | |
expect(self.somePromise?.isCompleted).to(beTrue()) | |
//assert that the villain list is populated | |
window.resignKey() | |
window.isHidden = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment