Swift XCTest and asserts -


i'm using swift2.

assume have function:

func divide(x x:int, y:int) -> double? {     assert(y != 0)      if (y == 0) {         return nil     } else {         return (double(x)/double(y))     } } 

for debugging purposes i'd use assert, can see when passes y = 0 in function.

i'd have workaround in production (i don't want crash, notify user), cannot divide 0. have test:

let res = divide(5, 0) xctassert(res == nil) 

but instead of finishing test exc_bad_access on assert in divide function.

can disable asserts somehow tests?

one option add default parameter function used bypass assert:

func divide(x x:int, y:int, safe:bool=true) -> double? {     if safe {         assert(y != 0)     }      if (y == 0) {         return nil     } else {         return (double(x)/double(y))     } } 

in app continue call x , y parameters

divide(5, 0) 

and assert.

in test call safe parameter false bypass assert , allow use xctassert on result:

let res = divide(5, 0, false) xctassert(res == nil) 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -