Ios 如何使用自定义对象来xctasertequal NSArray?
这是我的h文件Ios 如何使用自定义对象来xctasertequal NSArray?,ios,unit-testing,nsarray,xctest,Ios,Unit Testing,Nsarray,Xctest,这是我的h文件 @class TBL_CardView; @interface TBL_CardsOnTableView: NSObject - (CGPoint) addCard:(TBL_CardView *)cardView didFailWithError:(NSError **)error; - (unsigned int) numberOfCardOnTable; - (NSArray*) cardsOnTableWithPosition; @end @interface
@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
在m文件中,我是这样做的
- (NSArray*) cardsOnTableWithPosition
{
return (NSArray*) _cardsOnTable;
}
_卡森表是
NSMutableArray *_cardsOnTable;
_cardsOnTable = [[NSMutableArray alloc] initWithCapacity:8];
我用以下代码添加新对象:
TBL_CardOnTableWithPosition* cvwp = [[TBL_CardOnTableWithPosition alloc] initWithCardView:cardView withPosition:avalableLocation] ;
[_cardsOnTable addObject:cvwp];
这就是我在单元测试中的测试方式
[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
在Debugger中,对象a和d是相同的,但我可以找到在单元测试中测试它的方法。
知道为什么吗?我发现了这个问题 我需要实现-BOOLisEqual:idobject 在我的自定义对象中。别忘了实现-hash。