每个单元测试代表一个或多个需求。标准的 Xcode API(XCTest 框架)提供了一些基本表达式,它们可以很好地工作,但结果代码不够简洁、易于阅读和理解。
建议使用 CocoaPods 安装。
该库提供了一些辅助函数,每个函数都接受需求描述和闭包,这些闭包将返回将要评估(并返回给某些函数)的值。
当您拥有一个 Optional
值或您有一个会生成 Optional
值的函数/闭包,并且只有在它不是 nil
的情况下才需要这个值,否则调用 XCTFail
let nonNilValue = try RXC.value("Value is NOT nil") {
// return here an optional value,
// it might be result of an expression
// or an optional value captured from the outer scope,
// will call 'XCTFail' if value IS 'nil' or just return
// non-Optional value overwise
}
与上述代码相同,但不返回任何内容。当您有一个 Optional
值或您有一个生成 Optional
值的函数/闭包,并且您需要确保这个值不是 nil
,否则调用 XCTFail
try RXC.isNotNil("Value is NOT nil") { // does not return anything
// return here an optional value,
// it might be result of an expression
// or an optional value captured from the outer scope,
// will call 'XCTFail' if value IS 'nil' or pass through
// silently otherwise
}
当您有一个 Optional
值或您有一个生成 Optional
值的函数/闭包,并且您需要确保这个值是 nil
,否则调用 XCTFail
try RXC.isNil("Value IS nil") { // does not return anything
// return here an optional value,
// it might be result of an expression
// or an optional value captured from the outer scope,
// will call 'XCTFail' if value is NOT 'nil' or pass through
// silently otherwise
}
当您有一个 Bool
值或您有一个生成 Bool
值的函数/闭包,并且您只想在它是 true
的情况下继续,否则调用 XCTFail
try RXC.isTrue("Value is TRUE") { // does not return anything
// return here a boolean value,
// it might be result of an expression
// or an boolean value captured from the outer scope,
// will call 'XCTFail' if value is 'false' or pass through
// silently otherwise
}
VerificationFailed
数据类型只有一个参数
let description: String
,它包含传递给相应 RXC.*
函数的需求描述。