服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - 在 Swift 中测试 UIAlertController的方法

在 Swift 中测试 UIAlertController的方法

2020-12-21 14:49Swift教程网 Swift

这篇文章主要介绍了在 Swift 中测试 UIAlertController的方法的,需要的朋友可以参考下

最近我读了一篇在 Objective-C 中使用 control swizzling 测试 UIAlertController 的 文章 。这样的文章总是促使我寻找一种不使用 control swizzling 也可以测试同样东西的方法。虽然,我知道 swizzling 是开发者的一个非常有力的工具,但我个人是尽可能去避免去使用它的。事实上,在最近的六年时间里,我只在一个应用上用了 swizzling。所以我相信我们现在可以不使用 swizzling 来实现测试。

那么问题来了,如何在 Swift 中不使用 swizzling 来对 UIAlertController 进行测试?

我们先从我们要测试的代码开始吧。我已经添加一个按钮到 Storyboard 中。(我之所以使用 Storyboard 为了让那些不想用代码写界面的小伙伴有个更直观的感受)当按下这个按钮就会出现一个弹窗(alert),它有标题、消息内容,还有两个按钮,分别是 OK 和取消(Cancel)。

下面是这段代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)
 }
}

注意,在这个例子中弹窗动作没有做什么具体的操作,他们只表示能验证单元测试。

让我们开始一个简单的测试:测试这个弹窗控制器的标题和消息内容。

测试的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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()
 }
}
```

我们需要设置 sut 为根视图控制器,否则视图控制器不能弹出这个弹窗视图控制器。

添加 UIAlertController 测试标题的代码如下:

?
1
2
3
4
5
6
7
```Swift
func testAlert_HasTitle() {
 sut.showAlert(UIButton())
 XCTAssertTrue(sut.presentedViewController is UIAlertController)
 XCTAssertEqual(sut.presentedViewController?.title, "Test Title")
}
```

这很简单。现在让我们测试 UIAlertController 的取消按钮。这里有一个问题:无法获取弹窗动作的闭包。因此我们需要模拟弹窗动作,为了存储这个 handler 并在测试中调用它,看弹窗动作是否和我们预期的一样。在测试用例中添加这样一个类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
```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()
 }
}

这个模拟类的主要工作是捕获 handler 块,以备后用。现在我们需要将这个模拟的类插入到实现代码中。将视图控制器中的代码换成下面这个:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)
 }
}
 
```

我们添加了一个类变量`Action`,并设置为`UIAlertAction.self`。这个变量我们会在初始化弹窗动作时使用。这就能让我们在测试时可以重写它。像这样:

?
1
2
3
4
5
6
7
8
9
```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")
}

首先我们插入了这个弹窗动作。之后我们调用代码弹出弹窗视图控制器。我们从呈现的视图控制器中获取了取消动作,并且成功调用了捕获的 handler 块。最后一步就是去断言当前的动作是否和我们预期的一样。

就是这样,一种很简单的又不使用 swizzling 来测试 UIAlertViewController 的方式。

以上内容是关于在 Swift 中测试 UIAlertController的方法,希望对大家有用。

延伸 · 阅读

精彩推荐
  • SwiftSwift中排序算法的简单取舍详解

    Swift中排序算法的简单取舍详解

    对于排序算法, 通常简单的, 为大家所熟知的有, 选择排序, 冒泡排序, 快速排序, 当然还有哈希, 桶排序之类的, 本文仅比较最为常见的选择, 冒泡和快排,文...

    Castie111012021-01-10
  • SwiftSwift算法之栈和队列的实现方法示例

    Swift算法之栈和队列的实现方法示例

    Swift语言中没有内设的栈和队列,很多扩展库中使用Generic Type来实现栈或是队列。下面这篇文章就来给大家详细介绍了Swift算法之栈和队列的实现方法,需要...

    李峰峰10002021-01-05
  • Swiftswift相册相机的权限处理示例详解

    swift相册相机的权限处理示例详解

    在iOS7以后要打开手机摄像头或者相册的话都需要权限,在iOS9中更是更新了相册相关api的调用,那么下面这篇文章主要给大家介绍了关于swift相册相机权限处...

    hello老文12682021-01-08
  • Swift浅谈在Swift中关于函数指针的实现

    浅谈在Swift中关于函数指针的实现

    这篇文章主要介绍了浅谈在Swift中关于函数指针的实现,是作者根据C语言的指针特性在Swifft中做出的一个实验,需要的朋友可以参考下...

    Swift教程网4372020-12-21
  • SwiftSwift 基本数据类型详解总结

    Swift 基本数据类型详解总结

    在我们使用任何程序语言编程时,需要使用各种数据类型来存储不同的信息。变量的数据类型决定了如何将代表这些值的位存储到计算机的内存中。在声明...

    Lucky_William4672021-12-26
  • Swift详解Swift 之clipped是什么如何用

    详解Swift 之clipped是什么如何用

    这篇文章主要介绍了详解Swift 之clipped是什么如何用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    iCloudEnd8532021-05-28
  • Swift分析Swift性能高效的原因

    分析Swift性能高效的原因

    绝大多数公司选择Swift语言开发iOS应用,主要原因是因为Swift相比Objc有更快的运行效率,更加安全的类型检测,更多现代语言的特性提升开发效率;这一系...

    louis_wang9092021-01-16
  • SwiftSwift网络请求库Alamofire使用详解

    Swift网络请求库Alamofire使用详解

    这篇文章主要为大家详细介绍了Swift网络请求库Alamofire的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    lv灬陈强56682021-01-06