XCTAssertEqual fails to compare two string values?

39,456

Solution 1

From the documentation of XCTAssertEqual:

Generates a failure when a1 is not equal to a2. This test is for C scalars, structs and unions.

You should use XCTAssertEqualObjects (which uses isEqual: internally) or something like:

XCTAssertTrue([[firstNickName initialsFromString] isEqualToString:expectedResult],
              @"Strings are not equal %@ %@", expectedResult, [firstNickName initialsFromString]);

Solution 2

I've just had a similar issue which might help someone.

I have a Float extension function which returns a string. The following test fails:

testValue = 0.01
XCTAssertEqual(testValue.formattedForCost(), "0,01 €")

With the following message:

Assertions: XCTAssertEqual failed: ("Optional("0,01 €")") is not equal to ("Optional("0,01 €")")

Which is rather annoying. However I discovered if I change my test to use the unicode no-break space character:

XCTAssertEqual(testValue.formattedForCost(), "0,01\u{00a0}€")

It passes.

Solution 3

Comparing strings

- (void) testStringComparison {

    NSString *first = @"my string";
    NSString *second = @"my string";

    NSMutableString *firstMutable = [NSMutableString stringWithString:first];

    //== comparing addresses of the objects(pointer comparison)
    //`first` and `second` has the same address it is a compiler optimization to store only one copy
    XCTAssertTrue(first == second);
    XCTAssertFalse(first == firstMutable);

    XCTAssertEqual(first, second);
    XCTAssertNotEqual(first, firstMutable);
    XCTAssertEqualObjects(first, firstMutable);
    XCTAssertTrue([first isEqualToString:firstMutable]);
}
Share:
39,456

Related videos on Youtube

Konrad77
Author by

Konrad77

Currently work as a code monkey

Updated on July 05, 2022

Comments

  • Konrad77
    Konrad77 almost 2 years

    I added a simple unit test to test my string extension. But it fails. What I am I doing wrong here?

    From what I know XCTAssertEqual is testing value and not the object itself?

    The third line btw, says the string are equal, but XCTAssertEqual says they're not.

    - (void) testInitialsFromFullname {
        NSString *firstNickName = @"Mike Kain";
        NSString *expectedResult = @"MK";
        NSLog(@"Equal:%@", [[firstNickName initialsFromString] isEqualToString:expectedResult] ? @"YES" : @"NO");
    
        XCTAssertEqual(expectedResult, [firstNickName initialsFromString], @"Strings are not equal %@ %@", expectedResult, [firstNickName initialsFromString]);
    }
    
    • Michael Teper
      Michael Teper over 9 years
      This was a perfectly reasonable question, and I ran into the same issue when I first started with XCUnit. The accepted answer helped as well.
  • Cla
    Cla over 9 years
    It seems that if you XCTAssertEqualObjects() two NSString objects they are compared with isEqualToString: automatically.
  • Ossir
    Ossir over 8 years
    It uses -isEquals: under the hood all the time, but for NSString -isEquals: executes well-known -isEqualToString:. For instance, NSNumber implementation of -isEquals: method uses -isEqualToNumber:.
  • Max MacLeod
    Max MacLeod over 8 years
    Unfortunately despite still being in the docs, as of Xcode 7.2 XCTAssertEqualObjects is no longer available.
  • MdaG
    MdaG almost 8 years
    XCAssertEqual(a, b) works just fine in XCode 7. This answer is not correct anymore.
  • Keller
    Keller over 7 years
    XCTAssetEqualObjects is available in Xcode 8.0 and works for comparing NSStrings.
  • Phu Nguyen
    Phu Nguyen about 6 years
    Using XCTAssetEqualObjects is better because if the test is failed, it will throw exactly the wrong string. XCTAssertTrue throws just only: "Oh it's not true"
  • Beau Nouvelle
    Beau Nouvelle about 6 years
    This worked for me too, but there's got to be a better solution.
  • gomozor
    gomozor almost 4 years
    do we expect all the assert calls result in true? I'm new into the unit testing
  • yoAlex5
    yoAlex5 almost 4 years
    @gomozor, XCTAssertTrue expect TRUE, XCTAssertFalse expect FALSE. Expect means that a unit test is passed