How to unit test throwing functions in Swift?

前端 未结 5 665
暗喜
暗喜 2021-02-01 12:56

How to test wether a function in Swift 2.0 throws or not? How to assert that the correct ErrorType is thrown?

5条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 13:22

    Swift 4.1 Error throwing Test for associated values

    enum ParseError: Error, Equatable {
        case unexpectedArgument(String)
    }
    
    func testWithNoSchemaButWithOneArgument() {
        XCTAssertThrowsError(try Args(withSchema: "", andArguments: ["-x"])) { error in
            XCTAssertEqual(error as? ParseError, ParseError.unexpectedArgument("Argument(s) -x unexpected."))
        }
    }
    

提交回复
热议问题