XCTAssertThrowsError strange behavior with custom errorHandler

Viewed 2356

In my unit test I have the following code that checks whether thrown error is of expected type. It is done with two identical statements one of which doesn't compile:

    enum Error: ErrorType {
        case SomeExpectedError
        case SomeUnexpectedError
    }

    func functionThatThrows() throws {
        throw Error.SomeExpectedError
    }

    // 1) this compiles fine
    XCTAssertThrowsError(try functionThatThrows()) { (error) in
        switch error {
        case Error.SomeExpectedError: break
            //everything is fine
        case Error.SomeUnexpectedError: fallthrough
        default:
            XCTFail("Unexpected error thrown")
        }
    }

    // 2) doesn't compiles at all
    XCTAssertThrowsError(try functionThatThrows()) { (error) in
        XCTAssertEqual(error as? Error, Error.SomeExpectedError)
    }

First statement compiles and works fine, but seconds one says me that there are two errors: Errors thrown from here are not handled and Cannot convert value of type '(Error) -> Void' to expected argument type 'String'.

What could be wrong with this code? What do error messages mean?

I am using Xcode 7.3 .

1 Answers
Related