Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active April 28, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/9e397f79fd9982d9380b22a6396f41a7 to your computer and use it in GitHub Desktop.
Save chelseatroy/9e397f79fd9982d9380b22a6396f41a7 to your computer and use it in GitHub Desktop.
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