簡體   English   中英

如何使用自定義對象XCTAssertEqual NSArray?

[英]How to XCTAssertEqual NSArray with custom objects?

這是我的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

在我的文件中

- (NSArray*) cardsOnTableWithPosition
{
    return (NSArray*) _cardsOnTable;
}

_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

在調試器中,對象a和d是相同的,但是我可以找到在單元測試中如何對其進行測試的方法。
知道為什么嗎?

我發現了問題。

我需要實現-(BOOL)isEqual:{id)object
在我的自定義對象中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM