Skip to content

Commit 3c89228

Browse files
committed
Add tests to ensure the key path of errors gets returned.
1 parent 043300d commit 3c89228

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

JSONModel/JSONModel/JSONModelError.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ extern NSString* const kJSONModelMissingKeys;
4646
*/
4747
extern NSString* const kJSONModelTypeMismatch;
4848

49+
/**
50+
* If an error occurs in a nested model, check the userInfo dictionary of
51+
* the JSONModelError instance you get back - under the kJSONModelKeyPath
52+
* key you will find key-path at which the error occurred.
53+
*/
54+
extern NSString* const kJSONModelKeyPath;
55+
4956
/////////////////////////////////////////////////////////////////////////////////////////////
5057
/**
5158
* Custom NSError subclass with shortcut methods for creating

JSONModel/JSONModel/JSONModelError.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
NSString* const JSONModelErrorDomain = @"JSONModelErrorDomain";
2020
NSString* const kJSONModelMissingKeys = @"kJSONModelMissingKeys";
2121
NSString* const kJSONModelTypeMismatch = @"kJSONModelTypeMismatch";
22+
NSString* const kJSONModelKeyPath = @"kJSONModelKeyPath";
2223

2324
@implementation JSONModelError
2425

JSONModelDemoTests/UnitTests/SimpleDataErrorTests.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ -(void)testBrokenJSON
8181
STAssertTrue(err.code == kJSONModelErrorBadJSON, @"Wrong error for missing keys");
8282
}
8383

84-
- (void)performTestErrorsInNestedModelFile:(NSString*)jsonFilename
84+
- (NSError*)performTestErrorsInNestedModelFile:(NSString*)jsonFilename
8585
{
8686
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:jsonFilename];
8787
NSString* jsonContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
@@ -96,17 +96,24 @@ - (void)performTestErrorsInNestedModelFile:(NSString*)jsonFilename
9696
STAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error for missing keys");
9797

9898
// Make sure that 'name' is listed as the missing key
99-
STAssertTrue([err.userInfo[kJSONModelMissingKeys][0] isEqualToString:@"name"], @"'name' should be the missing key.");
99+
STAssertEqualObjects(err.userInfo[kJSONModelMissingKeys][0], @"name", @"'name' should be the missing key.");
100+
return err;
100101
}
101102

102103
-(void)testErrorsInNestedModelsArray
103104
{
104-
[self performTestErrorsInNestedModelFile:@"nestedDataWithArrayError.json"];
105+
NSError* err = [self performTestErrorsInNestedModelFile:@"nestedDataWithArrayError.json"];
106+
107+
// Make sure that the error is at the expected key-path
108+
STAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"images[1]", @"kJSONModelKeyPath does not contain the path of the error.");
105109
}
106110

107111
-(void)testErrorsInNestedModelsDictionary
108112
{
109-
[self performTestErrorsInNestedModelFile:@"nestedDataWithDictionaryError.json"];
113+
NSError* err = [self performTestErrorsInNestedModelFile:@"nestedDataWithDictionaryError.json"];
114+
115+
// Make sure that the error is at the expected key-path
116+
STAssertEqualObjects(err.userInfo[kJSONModelKeyPath], @"imageObject.image2", @"kJSONModelKeyPath does not contain the path of the error.");
110117
}
111118

112119
-(void)testForNilInputFromString

0 commit comments

Comments
 (0)