Come testare se una funzione in Swift 2.0 si lancia o no? Come affermare che viene lanciato il corretto ErrorType
?Come testare le funzioni di lancio delle unità in Swift?
risposta
Ecco t egli Swift 3
versione di Fyodor Volchyok's answer che ha usato XCTAssertThrowsError
:
enum MyError: Error {
case someExpectedError
case someUnexpectedError
}
func functionThatThrows() throws {
throw MyError.someExpectedError
}
XCTAssertThrowsError(try functionThatThrows()) { error in
XCTAssertEqual(error as? MyError, MyError.someExpectedError)
}
Inoltre, se il Error
enum ha associato i valori, nota si dovrà utilizzare l'istruzione if case
(o fare il vostro enum conforme alle Equatable
ma che sarebbe inutile codice aggiuntivo scrivere):
enum MyError: Error {
case someExpectedError
case someUnexpectedError
case associatedValueError(value: Int)
}
func functionThatThrows() throws {
throw MyError.associatedValueError(10)
}
XCTAssertThrowsError(try functionThatThrows()) { error in
guard case MyError.associatedValueError(let value) = error else {
return XCTFail()
}
XCTAssertEqual(value, 10)
}
dato i seguenti funzioni e dichiarazioni:
enum SomeError: ErrorType {
case FifthError
case FirstError
}
func throwingFunction(x: Int) throws {
switch x {
case 1:
throw SomeError.FirstError
case 5:
throw SomeError.FifthError
default:
return
}
}
Questa funzione gettare un FifthError
se 5 è dato alla funzione e FirstError
se 1 è dato.
Per verificare, che una funzione viene eseguito con successo il test di unità potrebbe apparire come segue:
func testNotError() {
guard let _ = try? throwingFunction(2) else {
XCTFail("Error thrown")
return
}
}
Il let _
possono essere sostituiti anche con qualsiasi altro nome, in modo da poter testare ulteriormente l'uscita.
di affermare che una funzione genera, non importa quale sia il test ErrorType
unità potrebbe assomigliare a questo:
func testError() {
if let _ = try? throwingFunction(5) {
XCTFail("No error thrown")
return
}
}
Se si desidera verificare per una specifica ErrorType
è fatto con un do-catch
-affermazione. Questo non è il modo migliore rispetto ad altre lingue.
Devi fare in modo che si ...
return
nelcatch
per il correttoErrorType
XCTFail()
ereturn
per tutti gli altricatch
XCTFail()
se non viene eseguitacatch
Dato questo requisito un test ca potrebbe essere simile a questo:
func testFifthError() {
do {
try throwingFunction(5)
} catch SomeError.FifthError {
return
} catch {
XCTFail("Wrong error thrown")
return
}
XCTFail("No error thrown")
}
Almeno di Xcode 7.3 (forse prima) è possibile utilizzare built-in XCTAssertThrowsError()
:
XCTAssertThrowsError(try methodThatThrows())
Se non si butta nel corso della prova si vedrà qualcosa di simile:
Se si desidera controllare se gettato errore è di un certo tipo di cemento, è possibile utilizzare errorHandler
parametro della XCTAssertThrowsError()
:
enum Error: ErrorType {
case SomeExpectedError
case SomeUnexpectedError
}
func functionThatThrows() throws {
throw Error.SomeExpectedError
}
XCTAssertThrowsError(try functionThatThrows(), "some message") { (error) in
XCTAssertEqual(error as? Error, Error.SomeExpectedError)
}
- 1. Come testare l'output grafico delle funzioni?
- 2. Come posso testare le funzioni sovraccariche?
- 3. Scrittura delle funzioni del blocco in Swift
- 4. Test delle unità Funzioni locali di script Vim con Vimrunner
- 5. Come testare le funzioni di ordine superiore in Clojure?
- 6. Unità testare le visualizzazioni del rasoio
- 7. come testare le funzioni interne nel plugin jquery?
- 8. Privacy delle funzioni e test delle unità Haskell
- 9. Come posso testare le funzioni Perl che stampano sullo schermo?
- 10. Come testare le classi astratte
- 11. Come testare le funzioni c che coinvolgono IO?
- 12. Come testare le API
- 13. Come unità di testare un rasoio-view
- 14. Test unità C#, come testare maggiore di
- 15. Come passare le funzioni di callback in Swift
- 16. Come utilizzare le immagini di lancio da un catalogo delle risorse nello storyboard di lancio?
- 17. Codice unità client di test delle unità di gestione
- 18. Esiste un framework di test delle unità per testare PowerShell con gli script di PowerShell?
- 19. Test unità - Cosa non testare
- 20. Test delle unità Classi astratte in Groovy
- 21. Test delle unità in PHP?
- 22. Come testare le funzioni di 'privati' in un servizio di angolare con il Karma e Jasmine
- 23. Aggiunta di file Swift per testare il target che non fissa i test di unità
- 24. Come testare i decoratori di funzioni Python?
- 25. Come posso testare le funzioni polimorfiche su Applicativi?
- 26. Come si scrivono le funzioni di Infix Swift?
- 27. "Le funzioni sono di prima classe" in swift?
- 28. Funzioni private di test unità in Android
- 29. Come testare il codice di prova con pochissime unità
- 30. Come testare localmente gli sn in un IDE come Intellij (per i test delle unità)
credo che sarebbe più sensato utilizzare il metodo XCTAssertThrowsError come Fyodor Volchyok suggerito. –