Reputation: 3069
I am new in Testing in Objective-C but I have some experience in .NET with MSTest.
What is the best way to compare two objects in objective C using XCTAssert?
Example code is below:
- (void)testNumericValue_SaveAndLoad_ShouldSaveAndThenLoadIdenticalObject
{
[_numericValue_1 saveToDatabaseWithKey:VALID_KEY_1];
NumericValue *tmpNumericValue = [[NumericValue alloc] loadFromDatabaseWithKey:VALID_KEY_1];
XCTAssertEqualObjects(tmpNumericValue, _numericValue_1);
}
- (void)testLoop_SaveAndLoad_ShouldSaveAndThenLoadIdenticalObject
{
[_loop_1 saveToDatabase];
Loop *tmpLoop = [[Loop alloc] loadFromDatabase];
XCTAssertEqualObjects(tmpLoop, _loop_1);
}
I have many tests like this. I am sure that save
and load
functions work in a proper way. Some of this tests are passing, some are failing. What is the reason?
I want this objects to have same properties values. I have to compare all of this properties one by one? Is there any "cleaner" way?
Thank you for your time
Upvotes: 0
Views: 772
Reputation: 726987
Some of them are passing, some are failing. What is the reason?
This is probably because you are using the default equality comparison.
I want this objects to have same properties values. I have to compare all of this properties one by one? Is there any "cleaner" way?
Yes. Override isEquals
to compare properties one-by-one, and XCTAssertEqualObjects
will do the comparisons correctly:
-(BOOL)isEqual:(id)other {
...
}
Don't forget to override hash
as well:
-(NSInteger)hash {
...
}
Here is a link to an answer that discusses Best practices for overriding isEqual:
and hash
.
Upvotes: 2