Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing Asynchronous Calls With FutureKit
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