XCTAssertEqual fails to compare two string values?
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]);
}
Related videos on Youtube
Comments
-
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 over 9 yearsThis 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 over 9 yearsIt seems that if you XCTAssertEqualObjects() two NSString objects they are compared with isEqualToString: automatically.
-
Ossir over 8 yearsIt uses
-isEquals:
under the hood all the time, but forNSString
-isEquals:
executes well-known-isEqualToString:
. For instance,NSNumber
implementation of-isEquals:
method uses-isEqualToNumber:
. -
Max MacLeod over 8 yearsUnfortunately despite still being in the docs, as of Xcode 7.2 XCTAssertEqualObjects is no longer available.
-
MdaG almost 8 yearsXCAssertEqual(a, b) works just fine in XCode 7. This answer is not correct anymore.
-
Keller over 7 yearsXCTAssetEqualObjects is available in Xcode 8.0 and works for comparing NSStrings.
-
Phu Nguyen about 6 yearsUsing
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 about 6 yearsThis worked for me too, but there's got to be a better solution.
-
gomozor almost 4 yearsdo we expect all the assert calls result in true? I'm new into the unit testing
-
yoAlex5 almost 4 years@gomozor, XCTAssertTrue expect TRUE, XCTAssertFalse expect FALSE. Expect means that a unit test is passed