在Swift中单元测试fatalError

在Swift中单元测试fatalError,第1张

概述如何在 Swift中执行fatalError代码路径的单元测试? 例如,我有以下swift代码 func divide(x: Float, by y: Float) -> Float { guard y != 0 else { fatalError("Zero division") } return x / y} 当y = 0时,我想单位测试的情况. 如何在 Swift中执行fatalError代码路径的单元测试?

例如,我有以下swift代码

func divIDe(x: float,by y: float) -> float {    guard y != 0 else {        fatalError("Zero division")    }    return x / y}

当y = 0时,我想单位测试的情况.

注意,我想使用fatalError不是任何其他断言功能.

这个想法是用自己的内置fatalError函数替换,在单元测试的执行期间被替换,以便您在其中运行单元测试断言.

然而,棘手的部分是fatalError是@noreturn,所以你需要用一个永远不会返回的函数来覆盖它.

覆盖fatalError

仅在您的应用程序目标中(不要添加到单元测试目标):

// overrIDes Swift global `fatalError`@noreturn func fatalError(@autoclosure message: () -> String = "",file: StaticString = __file__,line: UInt = __liNE__) {    FatalErrorUtil.fatalErrorClosure(message(),file,line)    unreachable()}/// This is a `noreturn` function that pauses forever@noreturn func unreachable() {    repeat {        NSRunLoop.currentRunLoop().run()    } while (true)}/// Utility functions that can replace and restore the `fatalError` global function.struct FatalErrorUtil {    // Called by the custom implementation of `fatalError`.    static var fatalErrorClosure: (String,StaticString,UInt) -> () = defaultFatalErrorClosure    // backup of the original Swift `fatalError`    private static let defaultFatalErrorClosure = { Swift.fatalError(
extension XCTestCase {    func expectFatalError(expectedMessage: String,testcase: () -> VoID) {        // arrange        let expectation = expectationWithDescription("expectingFatalError")        var assertionMessage: String? = nil        // overrIDe fatalError. This will pause forever when fatalError is called.        FatalErrorUtil.replaceFatalError { message,_,_ in            assertionMessage = message            expectation.fulfill()        }        // act,perform on separate thead because a call to fatalError pauses forever        dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),testcase)        waitForExpectationsWithTimeout(0.1) { _ in            // assert            XCTAssertEqual(assertionMessage,expectedMessage)            // clean up             FatalErrorUtil.restoreFatalError()        }    }}
,file: ,line: ) } /// Replace the `fatalError` global function with something else. static func replaceFatalError(closure: (String,UInt) -> ()) { fatalErrorClosure = closure } /// Restore the `fatalError` global function back to the original Swift implementation static func restoreFatalError() { fatalErrorClosure = defaultFatalErrorClosure }}

延期

将以下扩展名添加到您的单元测试目标:

class TestCase: XCTestCase {    func testExpectPreconditionFailure() {        expectFatalError("boom!") {            doSomethingThatCallsFatalError()        }    }}

测试用例

我从这篇文章中得到了关于单元测试断言和前提条件的想法:
Testing assertion in Swift

总结

以上是内存溢出为你收集整理的在Swift中单元测试fatalError全部内容,希望文章能够帮你解决在Swift中单元测试fatalError所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1041280.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存