Skip to content

Instantly share code, notes, and snippets.

@jmnavarro
Last active April 27, 2016 04:49
Show Gist options
  • Save jmnavarro/25c6c003d8f2ecee4be8 to your computer and use it in GitHub Desktop.
Save jmnavarro/25c6c003d8f2ecee4be8 to your computer and use it in GitHub Desktop.
Since it seems XCTAssertNotNil is not working with Swift's optional, this is an alternative method. In order to stop execution when test fails, check XCTestCase's continueAfterFailure property
func XCTAssertOptional(expression: @autoclosure () -> AnyObject?, _ message: String? = nil) {
let evaluatedExpression:AnyObject? = expression()
if evaluatedExpression == nil {
if let messageValue = message {
XCTFail(messageValue)
}
else {
XCTFail("Optional asertion failed: \(evaluatedExpression)")
}
}
}
@bishalg
Copy link

bishalg commented Apr 27, 2016

can you update to support latest Swift, on Xcode 7.3 its suggesting the following change to method signature

func XCTAssertOptional(expression: () -> AnyObject?, _ message: String? = nil)  { }

ie it has removed the @autoclosure
with warning - "Closure parameter prior to parameters with default arguments will not be treated as a trailing closure"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment