Swift too smart? Checking an objects type when testing with XCTest Swift too smart? Checking an objects type when testing with XCTest swift swift

Swift too smart? Checking an objects type when testing with XCTest


To get around Swift being too smart, cast the item you are testing to type Any and then test it. For instance, if you want to assert that the function fred() returns an Int

func fred() -> Int {    return 3}assert((fred() as Any) is Int)

Try this in a Playground, and then change the return type to Float and the assert will fire.


Just use is operator:

XCTAssertTrue(viewController  is HomeViewController)


If you are explicit about types in your let declarations, maybe you don't need to do anything more complex?

let fred: Int = fred()

Sure, it'll keep your tests from compiling instead of failing when they're run. But it communicates the same "Something in this test is no longer as expected!"