swiftxcodexctest

XCTAssertEqual failed: ("nan") is not equal to ("nan") in Swift


When I run the following test case, I get XCTAssertEqual failed: ("nan") is not equal to ("nan") error.

 XCTAssertEqual(0.0 / 0.0, Double.nan)

How can I fix this error?


Solution

  • As Joakim said, NaN is never equal to any value, including itself.

    You need to rewrite your code using isNaN:

    let someValue = 0.0 / 0.0
    XCTAssertTrue(someValue.isNaN) // Will detect that someValue is a NaN