xcode - How to unit test throwing functions in Swift? -


how test wether function in swift 2.0 throws or not? how assert correct errortype thrown?

here's swift 3 version of fyodor volchyok's answer used 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)     } 

also, if error enum has associated values, note have use if case statement (or make enum conform equatable unnecessary code write):

    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)     } 

Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -