麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > 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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩一级免费 | 欧产日产国产精品v | chinese乱子伦xxxx国语对白 | 搜一级毛片| 欧美不卡三区 | 欧美 中文字幕 | 免费国产a | 欧美精选一区二区 | 国产精品久久久久久238 | 欧美视频一区二区三区四区 | 日本羞羞的午夜电视剧 | 男女无遮挡羞羞视频 | 黄色av电影在线 | 19禁国产精品福利视频 | 亚洲成人自拍电影 | 午夜小网站 | 国产精品毛片无码 | 久久精品国产99久久久古代 | pornoⅹxxxxhd麻豆| 新久久久久久 | 国色天香综合网 | 91久久综合 | 久久精品国产99久久久古代 | 把娇妻调教成暴露狂 | 久久久久久久久久亚洲 | 国产免费观看av | 美女很黄很黄免费的 | 久久久久久久久久久av | 亚洲综合网站 | 成人免费毛片在线观看 | 国产精品久久久久久久久久东京 | 欧美福利视频一区二区三区 | 日本xxxx色视频在线观看免费, | 欧美性a视频| 亚洲啊v在线观看 | 在线成人影视 | 国产大片免费看 | 亚洲欧美国产精品va在线观看 | 亚洲国产综合在线观看 | 91成人免费在线视频 | 在线成人影视 |