import UIKit
class ViewController: UIViewController {
var actionString: String?
@IBAction func showAlert(sender: UIButton) {
let alertViewController = UIAlertController(title: "Test Title", message: "Message", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
self.actionString = "OK"
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) -> Void in
self.actionString = "Cancel"
}
alertViewController.addAction(cancelAction)
alertViewController.addAction(okAction)
presentViewController(alertViewController, animated: true, completion: nil)
}
}
import XCTest
@testable import TestingAlertExperiment
class TestingAlertExperimentTests: XCTestCase {
var sut: ViewController!
override func setUp() {
super.setUp()
sut = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! ViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = sut
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
}
```
```Swift
func testAlert_HasTitle() {
sut.showAlert(UIButton())
XCTAssertTrue(sut.presentedViewController is UIAlertController)
XCTAssertEqual(sut.presentedViewController?.title, "Test Title")
}
```
```Swift
class MockAlertAction : UIAlertAction {
typealias Handler = ((UIAlertAction) -> Void)
var handler: Handler?
var mockTitle: String?
var mockStyle: UIAlertActionStyle
convenience init(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Void)?) {
self.init()
mockTitle = title
mockStyle = style
self.handler = handler
}
override init() {
mockStyle = .Default
super.init()
}
}
import UIKit
class ViewController: UIViewController {
var Action = UIAlertAction.self
var actionString: String?
@IBAction func showAlert(sender: UIButton) {
let alertViewController = UIAlertController(title: "Test Title", message: "Message", preferredStyle: .Alert)
let okAction = Action.init(title: "OK", style: .Default) { (action) -> Void in
self.actionString = "OK"
}
let cancelAction = Action.init(title: "Cancel", style: .Cancel) { (action) -> Void in
self.actionString = "Cancel"
}
alertViewController.addAction(cancelAction)
alertViewController.addAction(okAction)
presentViewController(alertViewController, animated: true, completion: nil)
}
}
```
```Swift
func testAlert_FirstActionStoresCancel() {
sut.Action = MockAlertAction.self
sut.showAlert(UIButton())
let alertController = sut.presentedViewController as! UIAlertController
let action = alertController.actions.first as! MockAlertAction
action.handler!(action)
XCTAssertEqual(sut.actionString, "Cancel")
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有