Search code examples
iosunit-testingios7xctest

XCTAssertEqual: How to compare NSDates?


NSDate *date = [NSDate date];
XCTAssertEqual([[store selectedDate] timeIntervalSinceReferenceDate], [date timeIntervalSinceReferenceDate]);

This gives me the error message:

(([[store selectedDate] timeIntervalSinceReferenceDate]) equal to ([date timeIntervalSinceReferenceDate])) failed: 
("405290648.294") is not equal to ("405290648.294")

I had previous a similar problem with Integers, which had to solved by casting it to NSUInteger as described here.

But I couldn't figure out how to solve this with NSDate objects / doubles (as in this case).


Solution

  • use XCTAssertEqualWithAccuracy to compare floating numbers

    XCTAssertEqualWithAccuracy([[store selectedDate] timeIntervalSinceReferenceDate], [date timeIntervalSinceReferenceDate], 0.001);