Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Dookieman

macrumors 6502
Original poster
Oct 12, 2009
390
67
Hey All,

I want to write a unit test for my data model and ensure that it's parsing correctly. My approach is to make a JSON dictionary that has keys that mimic the server response and then manually set a model object with the same values as the JSON dictionary, then compare the two to ensure that the values are getting set appropriately. The problem is the XCTest is saying that the models aren't equal, even though they are (I iterated through each values and compared the values manually and got matching results). I double checked and they have the same values. The model inherits from NSObject so I don't need to implement equatable.

1. Does XCTAssertEqual check memory address rather than values?

2. Is there a way to compare these models without having to manually compare each value? e.g. model1.obj1 == model2.ob1
 

Dookieman

macrumors 6502
Original poster
Oct 12, 2009
390
67
i don't know for sure but looking around it seems like it == might compare objects by hash. You can override the isEqual() for your model to compare the values you want to test and use that for your unit test. Then you only have to write it once.

this article talks about it http://mgrebenets.github.io/swift/2015/06/21/equatable-nsobject-with-swift-2
Awesome. I'll give it a try. Thanks for the link.

Looks like it worked for me. I overrode the isEqual method like the blog suggested but figured that it would be best to write my own instead of touching Apple's.

Thanks.
 
Last edited:

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
You should look at NSArray isEqualToArray: and NSDictionary isEqualToDictionary to see if they can help with this.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.