How do you write tests for Swift methods that have preconditions? Here is an example:
func doublePositive(n:Int) -> Int {
precondition(n >= 0)
return 2*n
}
Using XCTAssertThrowsError
does not work:
func testDoublePositive() {
XCTAssertEqual(10, testObject.doublePositive(5)) // Works
XCTAssertThrowsError(testObject.doublePositive(-1)) // Breaks
}
This generates an error when running the test:
Thread 1:EXEC_BAD_INSTRUCTION (Code=EXCI386_INVOP, Subcode=0x0)
Is there a way to test preconditions of Swift?