Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created September 7, 2016 16:49
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/e520f5a3fc6d27288d5b6629bdb768d3 to your computer and use it in GitHub Desktop.
Save chelseatroy/e520f5a3fc6d27288d5b6629bdb768d3 to your computer and use it in GitHub Desktop.
View Controller Dependency Injection Test
class ExampleViewControllerTest: XCTestCase {
class FakeDependableService: DependableService {
var lastRequestParameter: String?
var stubbedResponse: DependableResponse?
init() {
super.init()
self.lastRequestParameter = ""
self.stubbedResponse = nil
}
override func attemptCall(param: String?) -> Future<DependableResponse> {
self.lastRequestParameter = param
return Future(success: stubbedResponse!)
}
}
var dependableService = FakeDependableService()
func testClickCallButton_makesCallToService_successResponse() {
dependableService.stubbedResponse = DependableResponse.Success("You win!")
let controller = ExampleViewController.loadFromStoryboard(dependableService)
assertThat(controller.view, present())
UIWindow.present(viewController: controller) { () in
controller.didTapCallButton(NSObject())
assertThat(dependableService.lastRequestParameter, presentAnd(equalTo("Do I win?")))
assertThat(controller.presentedViewController, presentAnd(instanceOf(NextViewController)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment