String equality == in XCTAssertTrue

I was running the following code (paraphrased) in a test using Xcode 7b4, and it always failed :


let knownGoodString = "foobar"

XCTAssertTrue( someClassInstance.stringProperty == knownGoodString,

"String equality test failed, \(someClassInstance.stringProperty) != \(knownGoodString)" )


But the console transcript would show identical strings. After experimenting and reading the docs, I found that I needed to use XCTAssertEquality instead, so that problem was solved.


But why doesn't the equality operator return a true that satisfies the XCTest assertion? Is it a bug or do I just misunderstand something?

Answered by LCS in 34184022

It seems like there must have been something else going on?


I generally use XCTAssert(), which is the same as XCTAssertTrue() according to the documentation, for all tests rather than using the more specific forms, and haven't had problems with doing equality tests with == at all.

Neither work for me 😟. Testing in XCode Version 7.0 beta 3 (7A152u):


import Foundation

class TestOfXCTAssert: CustomStringConvertible {
    var description: String { return "Test" }
}


With:


import XCTest
@testable import TestOfXCTAssert

class TestOfXCTAssertTests: XCTestCase {
    func testExample() {
        let test = TestOfXCTAssertTests().description
        XCTAssert("Test" == test, test)
        XCTAssertEqual("Test", test, test)
    }
}


Both tests fail and both indicate that test is null. To be precise they display test as:


[TestOfXCTAssertTests (null)]


Very strange!

Accepted Answer

It seems like there must have been something else going on?


I generally use XCTAssert(), which is the same as XCTAssertTrue() according to the documentation, for all tests rather than using the more specific forms, and haven't had problems with doing equality tests with == at all.

You've been foiled by autocomplete.


This line in your test case

let test = TestOfXCTAssertTests().description


should read

let test = TestOfXCTAssert().description

Putting this class in a small sample app:

class SomeClass {
    var stringProperty: String = "foobar"
}


This test method always succeeds:

    func testExample() {
        let someClassInstance: SomeClass = SomeClass()
        let knownGoodString = "foobar"
        XCTAssertTrue( someClassInstance.stringProperty == knownGoodString,
            "String equality test failed, \(someClassInstance.stringProperty) != \(knownGoodString)" )
        XCTAssertEqual( someClassInstance.stringProperty, knownGoodString,
            "String equality test failed, \(someClassInstance.stringProperty) != \(knownGoodString)" )
    }


Your issue may reside in other parts of your code than using equality test with == .

I just tried it again this morning and the XCTAssertTrue( string == string) test passed without changing anything. I also tried the XCTAssert() version which passed as well.


I've noticed a few other similar situations where Xcode 7 beta will just refuse to not recognize a non-existant error, probably related to that.

You are right. Thanks.

String equality == in XCTAssertTrue
 
 
Q