Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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