[英]How to XCTAssertEqual NSArray with custom objects?
This is my h file 这是我的h文件
@class TBL_CardView;
@interface TBL_CardsOnTableView: NSObject
- (CGPoint) addCard:(TBL_CardView *)cardView didFailWithError:(NSError **)error;
- (unsigned int) numberOfCardOnTable;
- (NSArray*) cardsOnTableWithPosition;
@end
@interface TBL_CardOnTableWithPosition: NSObject
- (instancetype)initWithCardView:(TBL_CardView*) cardView withPosition:(NSUInteger) position;
@property(readonly) TBL_CardView* card;
@property(readonly) NSUInteger position;
@end
in m file I do 在我的文件中
- (NSArray*) cardsOnTableWithPosition
{
return (NSArray*) _cardsOnTable;
}
_cardsOnTable is _cardsOnTable是
NSMutableArray *_cardsOnTable;
_cardsOnTable = [[NSMutableArray alloc] initWithCapacity:8];
and I add new object with this code: 然后使用以下代码添加新对象:
TBL_CardOnTableWithPosition* cvwp = [[TBL_CardOnTableWithPosition alloc] initWithCardView:cardView withPosition:avalableLocation] ;
[_cardsOnTable addObject:cvwp];
And this is how I test in Unit Test 这就是我在单元测试中测试的方式
[table cardsOnTableWithPosition];
TBL_CardOnTableWithPosition* cvwp1 = [[TBL_CardOnTableWithPosition alloc] initWithCardView:cardView1 withPosition:1] ;
NSLog(@"%@", [table cardsOnTableWithPosition]);
NSArray* array = [table cardsOnTableWithPosition];
NSArray * a = [[NSArray alloc] initWithArray:@[cvwp1]];
NSArray * d = [table cardsOnTableWithPosition];
XCTAssertEqual(a, d); // FAILD, but this I am expecting to failed
XCTAssertEqualObjects(a, d); // FAILD
XCTAssertEqualObjects(@[cvwp1], [table cardsOnTableWithPosition]); // FAILD
XCTAssertTrue([a isEqualToArray:d]); // FAILD
In debuger objects a and d are same, but I can to find way how to test it in Unit test. 在调试器中,对象a和d是相同的,但是我可以找到在单元测试中如何对其进行测试的方法。
Any idea why ? 知道为什么吗?
I have found the problem. 我发现了问题。
I needed to implement - (BOOL)isEqual:(id)object 我需要实现-(BOOL)isEqual:{id)object
in my custom objects. 在我的自定义对象中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.