Browse files

Update unit tests with error handling

  • Loading branch information...
1 parent e0c0017 commit e8d2dea987e4160de9980967d8b2e946a6209d14 @jspahrsummers jspahrsummers committed Feb 20, 2013
View
2 MantleTests/MTLJSONAdapterSpec.m
@@ -37,7 +37,7 @@
MTLTestModel *model = [MTLTestModel modelWithDictionary:@{
@"name": @"foobar",
@"count": @5,
- }];
+ } error:NULL];
MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithModel:model];
expect(adapter).notTo.beNil();
View
4 MantleTests/MTLModelNSCodingSpec.m
@@ -40,8 +40,10 @@
@"count": @5,
};
- model = [[MTLTestModel alloc] initWithDictionary:values];
+ NSError *error = nil;
+ model = [[MTLTestModel alloc] initWithDictionary:values error:&error];
expect(model).notTo.beNil();
+ expect(error).to.beNil();
archiveAndUnarchiveModel = [^{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
View
21 MantleTests/MTLModelSpec.m
@@ -38,8 +38,10 @@
});
it(@"should initialize to default values with a nil dictionary", ^{
- MTLTestModel *dictionaryModel = [[MTLTestModel alloc] initWithDictionary:nil];
+ NSError *error = nil;
+ MTLTestModel *dictionaryModel = [[MTLTestModel alloc] initWithDictionary:nil error:&error];
expect(dictionaryModel).notTo.beNil();
+ expect(error).to.beNil();
MTLTestModel *defaultModel = [[MTLTestModel alloc] init];
expect(dictionaryModel).to.equal(defaultModel);
@@ -61,8 +63,10 @@
@"weakModel": emptyModel,
};
- model = [[MTLTestModel alloc] initWithDictionary:values];
+ NSError *error = nil;
+ model = [[MTLTestModel alloc] initWithDictionary:values error:&error];
expect(model).notTo.beNil();
+ expect(error).to.beNil();
});
it(@"should initialize with the given values", ^{
@@ -78,7 +82,7 @@
it(@"should compare equal to a matching model", ^{
expect(model).to.equal(model);
- MTLTestModel *matchingModel = [[MTLTestModel alloc] initWithDictionary:values];
+ MTLTestModel *matchingModel = [[MTLTestModel alloc] initWithDictionary:values error:NULL];
expect(model).to.equal(matchingModel);
expect(model.hash).to.equal(matchingModel.hash);
expect(model.dictionaryValue).to.equal(matchingModel.dictionaryValue);
@@ -98,15 +102,20 @@
});
it(@"should fail to initialize if dictionary validation fails", ^{
- MTLTestModel *model = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"this is too long a name" }];
+ NSError *error = nil;
+ MTLTestModel *model = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"this is too long a name" } error:&error];
expect(model).to.beNil();
+
+ expect(error).notTo.beNil();
+ expect(error.domain).to.equal(MTLTestModelErrorDomain);
+ expect(error.code).to.equal(MTLTestModelNameTooLong);
});
it(@"should merge two models together", ^{
- MTLTestModel *target = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"foo", @"count": @(5) }];
+ MTLTestModel *target = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"foo", @"count": @(5) } error:NULL];
expect(target).notTo.beNil();
- MTLTestModel *source = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"bar", @"count": @(3) }];
+ MTLTestModel *source = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"bar", @"count": @(3) } error:NULL];
expect(source).notTo.beNil();
[target mergeValuesForKeysFromModel:source];
View
3 MantleTests/MTLTestModel.h
@@ -6,6 +6,9 @@
// Copyright (c) 2012 GitHub. All rights reserved.
//
+extern NSString * const MTLTestModelErrorDomain;
+extern const NSInteger MTLTestModelNameTooLong;
+
@interface MTLEmptyTestModel : MTLModel
@end
View
10 MantleTests/MTLTestModel.m
@@ -8,6 +8,9 @@
#import "MTLTestModel.h"
+NSString * const MTLTestModelErrorDomain = @"MTLTestModelErrorDomain";
+const NSInteger MTLTestModelNameTooLong = 1;
+
static NSUInteger modelVersion = 1;
@implementation MTLEmptyTestModel
@@ -18,7 +21,12 @@ @implementation MTLTestModel
#pragma mark Properties
- (BOOL)validateName:(NSString **)name error:(NSError **)error {
- return [*name length] < 10;
+ if ([*name length] < 10) return YES;
+ if (error != NULL) {
+ *error = [NSError errorWithDomain:MTLTestModelErrorDomain code:MTLTestModelNameTooLong userInfo:nil];
+ }
+
+ return NO;
}
- (NSString *)dynamicName {

0 comments on commit e8d2dea

Please sign in to comment.