I'm building a XCTestCase that checks if a certain initializer work. I've already checked line by line of the code making sure I'm passing the right value for each key in the source dictionary (used in the class init). But I keep getting this error: NSInvalidArgumentException [__NSCFConstantString objectForKeyedSubscript:]
Here's the test code:
NSDictionary *comments = @{@"data" : @{@"id": @"8675309",
@"text" : @"Sample Comment"}};
NSDictionary *user = @{@"id": @"8675309",
@"username" : @"d'oh",
@"full_name" : @"Homer Simpson",
@"profile_picture" : @"http://www.example.com/example.jpg"};
NSDictionary *sourceDictionary = @{@"id" : @"902810",
@"user" : user,
@"caption" : @{@"text" : @"a caption"},
@"images" : @{@"standard_resolution" : @{@"url" : @"www.example.com.br/media"}},
@"comments" : comments,
@"likes" : @{@"count" : @"12342"},
@"user_had_liked" : @"1"};
Media *testMedia = [[Media alloc] initWithDictionary:sourceDictionary];
And this is the class that I'm testing the initializer
-(instancetype)initWithDictionary:(NSDictionary *)mediaDictionary {
self = [super init];
if (self) {
self.mediaDictionary = mediaDictionary;
self.idNumber = mediaDictionary[@"id"];
self.user = [[User alloc] initWithDictionary:mediaDictionary[@"user"]];
[self tryToFetchCaption];
[self tryToFetchImage];
[self tryToFetchComments];
[self setLikesCounter];
BOOL userHasLiked = [mediaDictionary[@"user_had_liked"] boolValue];
self.likeState = userHasLiked ? LikeStateLiked : LikeStateNotLiked;
}
return self;
}
-(void)setLikesCounter {
NSInteger likeCount = [self.mediaDictionary[@"likes"][@"count"] intValue];
self.likesCounter = @(likeCount);
}
-(void)tryToFetchCaption {
NSDictionary *captionDictionary = self.mediaDictionary[@"caption"];
if ([captionDictionary isKindOfClass:[NSDictionary class]]) {
self.caption = captionDictionary[@"text"];
} else {
self.caption = @"";
}
}
-(void)tryToFetchImage {
NSString *standardResolutionImageURLString = self.mediaDictionary[@"images"][@"standard_resolution"][@"url"];
NSURL *standardResolutionImageURL = [NSURL URLWithString:standardResolutionImageURLString];
if (standardResolutionImageURL) {
self.mediaURL = standardResolutionImageURL;
self.downloadState = MediaDownloadStateNeedsImage;
} else {
self.downloadState = MediaDownloadStateNonRecoverableError;
}
}
-(void)tryToFetchComments {
NSMutableArray *commentsArray = [NSMutableArray new];
for (NSDictionary *commentDictionary in self.mediaDictionary[@"comments"][@"data"]) {
Comment *tempComment = [[Comment alloc] initWithDictionary:commentDictionary];
[commentsArray addObject:tempComment];
}
self.comments = commentsArray;
}
Log:
2014-10-26 11:39:08.295 Blocstagram[2128:21707] -[__NSCFConstantString objectForKeyedSubscript:]: unrecognized selector sent to instance 0xc9d2074
MediaTests.m:47: error: -[MediaTests testThatInitializationWorks] : failed: caught "NSInvalidArgumentException", "-[__NSCFConstantString objectForKeyedSubscript:]: unrecognized selector sent to instance 0xc9d2074"
(
0 CoreFoundation 0x0119a946 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x00ad4a97 objc_exception_throw + 44
2 CoreFoundation 0x011a25c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x010eb3e7 ___forwarding___ + 1047
4 CoreFoundation 0x010eafae _CF_forwarding_prep_0 + 14
5 Blocstagram 0x00060453 -[Comment initWithDictionary:] + 179
6 Blocstagram 0x00068ce4 -[Media tryToFetchComments] + 596
7 Blocstagram 0x00067db7 -[Media initWithDictionary:] + 519
8 BlocstagramTests 0x0c9cf74a -[MediaTests testThatInitializationWorks] + 1562
9 CoreFoundation 0x0107d76d __invoking___ + 29
10 CoreFoundation 0x0107d618 -[NSInvocation invoke] + 360
11 XCTest 0x2010897b -[XCTestCase invokeTest] + 320
12 XCTest 0x20108bb9 -[XCTestCase performTest:] + 184
13 XCTest 0x20114162 -[XCTest run] + 314
14 XCTest 0x20107598 -[XCTestSuite performTest:] + 406
15 XCTest 0x20114162 -[XCTest run] + 314
16 XCTest 0x20107598 -[XCTestSuite performTest:] + 406
17 XCTest 0x20114162 -[XCTest run] + 314
18 XCTest 0x20107598 -[XCTestSuite performTest:] + 406
19 XCTest 0x20114162 -[XCTest run] + 314
20 XCTest 0x20103de2 __25-[XCTestDriver _runSuite]_block_invoke + 61
21 XCTest 0x20110c82 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 184
22 XCTest 0x20103d06 -[XCTestDriver _runSuite] + 285
23 XCTest 0x20104951 -[XCTestDriver _checkForTestManager] + 272
24 XCTest 0x20104c6b -[XCTestDriver runTestSuite:completionHandler:] + 378
25 XCTest 0x2011775c +[XCTestProbe runTests:] + 216
26 Foundation 0x006d7b57 __NSFireDelayedPerform + 423
27 CoreFoundation 0x010f48d6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
28 CoreFoundation 0x010f425d __CFRunLoopDoTimer + 1309
29 CoreFoundation 0x010b36ba __CFRunLoopRun + 2090
30 CoreFoundation 0x010b2bcb CFRunLoopRunSpecific + 443
31 CoreFoundation 0x010b29fb CFRunLoopRunInMode + 123
32 GraphicsServices 0x058ff24f GSEventRunModal + 192
33 GraphicsServices 0x058ff08c GSEventRun + 104
34 UIKit 0x019f68b6 UIApplicationMain + 1526
35 Blocstagram 0x0008a32d main + 141
36 libdyld.dylib 0x02deaac9 start + 1
)
Test Case '-[MediaTests testThatInitializationWorks]' failed (0.039 seconds).
Test Suite 'MediaTests' failed at 2014-10-26 13:39:08 +0000.
Executed 1 test, with 1 failure (1 unexpected) in 0.039 (0.040) seconds
testMediais causing the problem? As a last resort comment out the calls in the init method until the exception does not occur. - zaph