I'm testing the error handling for SFSpeechRecognizer
. I'm not able to get the function to throw an error using XCTAssertThrowsError()
.
Creating SFSpeechRecognizer
will return an object or nil
if there is no locale.
public convenience init?() // Returns speech recognizer with user's current locale, or nil if is not supported
public init?(locale: Locale) // returns nil if the locale is not supported
func createSpeechRecognization(locale : Locale) throws -> SFSpeechRecognizer {
guard let speechRecognizer = SFSpeechRecognizer(locale: locale) else {
throw SpeechToTextError.speechRecognizerIsNil
}
return speechRecognizer
}
Here is the unit test function where I attempt to throw:
func testCreateSpeechToTextAudioSessionNotNotAvailable() {
XCTAssertThrowsError(try sut.createSpeechRecognization(locale: Locale.current)) {error in
XCTAssertEqual(error as? SpeechToTextError, SpeechToTextError.speechRecognizerIsNil)
}
}
The unit test failed due to the error:
XCTAssertThrowsError failed: did not throw an error.
I'm not sure why it is happening. Any suggestions?