国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

在 Swift 中測試 UIAlertController的方法

2020-03-09 17:50:04
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了在 Swift 中測試 UIAlertController的方法的,需要的朋友可以參考下
 

最近我讀了一篇在 Objective-C 中使用 control swizzling 測試 UIAlertController 的 文章 。這樣的文章總是促使我尋找一種不使用 control swizzling 也可以測試同樣東西的方法。雖然,我知道 swizzling 是開發者的一個非常有力的工具,但我個人是盡可能去避免去使用它的。事實上,在最近的六年時間里,我只在一個應用上用了 swizzling。所以我相信我們現在可以不使用 swizzling 來實現測試。

那么問題來了,如何在 Swift 中不使用 swizzling 來對 UIAlertController 進行測試?

我們先從我們要測試的代碼開始吧。我已經添加一個按鈕到 Storyboard 中。(我之所以使用 Storyboard 為了讓那些不想用代碼寫界面的小伙伴有個更直觀的感受)當按下這個按鈕就會出現一個彈窗(alert),它有標題、消息內容,還有兩個按鈕,分別是 OK 和取消(Cancel)。

下面是這段代碼:

import UIKitclass 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 TestingAlertExperimentclass 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() }}```

我們需要設置 sut 為根視圖控制器,否則視圖控制器不能彈出這個彈窗視圖控制器。

添加 UIAlertController 測試標題的代碼如下:

```Swiftfunc testAlert_HasTitle() { sut.showAlert(UIButton()) XCTAssertTrue(sut.presentedViewController is UIAlertController) XCTAssertEqual(sut.presentedViewController?.title, "Test Title")}``` 

這很簡單。現在讓我們測試 UIAlertController 的取消按鈕。這里有一個問題:無法獲取彈窗動作的閉包。因此我們需要模擬彈窗動作,為了存儲這個 handler 并在測試中調用它,看彈窗動作是否和我們預期的一樣。在測試用例中添加這樣一個類:

```Swiftclass 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() }}

這個模擬類的主要工作是捕獲 handler 塊,以備后用。現在我們需要將這個模擬的類插入到實現代碼中。將視圖控制器中的代碼換成下面這個:

import UIKitclass 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) }}```

我們添加了一個類變量`Action`,并設置為`UIAlertAction.self`。這個變量我們會在初始化彈窗動作時使用。這就能讓我們在測試時可以重寫它。像這樣:

```Swiftfunc 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")}

首先我們插入了這個彈窗動作。之后我們調用代碼彈出彈窗視圖控制器。我們從呈現的視圖控制器中獲取了取消動作,并且成功調用了捕獲的 handler 塊。最后一步就是去斷言當前的動作是否和我們預期的一樣。

就是這樣,一種很簡單的又不使用 swizzling 來測試 UIAlertViewController 的方式。

以上內容是關于在 Swift 中測試 UIAlertController的方法,希望對大家有用。



注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富宁县| 溧阳市| 甘孜县| 靖边县| 临高县| 石景山区| 天气| 裕民县| 孙吴县| 溧水县| 嘉荫县| 新巴尔虎左旗| 县级市| 即墨市| 蚌埠市| 从江县| 灵丘县| 边坝县| 佛教| 大田县| 栖霞市| 鹤岗市| 鄂尔多斯市| 汉中市| 石阡县| 清丰县| 上虞市| 天镇县| 铜梁县| 阿坝县| 弋阳县| 额尔古纳市| 甘孜| 濮阳市| 喜德县| 隆回县| 玛曲县| 霍城县| 贡觉县| 万载县| 灌云县|