时间挤的话,总是会有的
比如说需求做完的时候,等同事接入的时候
1.NSAlert的两种弹出方式
首先创建NSAlert,并添加选项
let alert = NSAlert()
alert.messageText = "警告消息⚠️"
alert.informativeText = "警告消息⚠️的描述"
alert.addButton(withTitle: "确定")
alert.addButton(withTitle: "隐藏")
alert.addButton(withTitle: "取消")
runModal方法,警告栏是独立在window外部
let result = alert.runModal()
switch result {
case .alertFirstButtonReturn:
print("确定")
case .alertSecondButtonReturn:
print("隐藏")
case .alertThirdButtonReturn:
print("取消")
default:
break
}
效果:

beginSheetModal方法,使警告栏在window内部
alert.beginSheetModal(for: view.window!) { (response) in
print("选择了\"\(response)\"")
}
效果:

2.自动化测试UI
NSAlertViewUITests.swift
效果图:

可以测试后加入XCTAssert(window.staticTexts["确定"].exists)
,可以测试通过
其实UITests自动化测试UI,自己在项目中接触的不多,在这里记录并尝试一下
3.单元测试
创建Song.swift
模型,里面有属性title
,author
,duration
class Song: NSObject {
var title: String
var author: String
var duration: Double
init(title: String, author: String, duration: Double) {
self.title = title
self.author = author
self.duration = duration
}
}
创建Constants.swift
储存一些常量来进行测试
class Constants: NSObject {
static let title = "Great Title"
static let author = "MySelf"
static let duration = 60.0
}
创建SongTests.swift
需要继承于XCTestCase
import XCTest
// 需要导入整个项目名称NSAlertView
@testable import NSAlertView
class SongTests: XCTestCase {
func textInit() {
let song = Song(title: Constants.title, author: Constants.author, duration: Constants.duration)
XCTAssertEqual(song.title, Constants.title)
XCTAssertEqual(song.author, Constants.author)
XCTAssertEqual(song.duration, Constants.duration)
}
}
点击测试测试通过,证明构造方法的结果和模型结果一致
网友评论