Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ccabanero/3de1a0cfecc7cb4fa9e6 to your computer and use it in GitHub Desktop.

Select an option

Save ccabanero/3de1a0cfecc7cb4fa9e6 to your computer and use it in GitHub Desktop.
Sample iOS Unit Tests: Confirming that a NSManagedObject subclass Category properly seeds CoreData
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "AppDelegate.h"
#import "Item.h"
#import "Item+CoreData.h" //note: Item+CoreData is a category for the Item entity (i.e. NSManagedObject subclass)
@interface Item_CoreDataTest : XCTestCase
//for accessing CoreData NSManagedObjectContext in assertions
@property (nonatomic, strong) AppDelegate *appDelegate;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@end
@implementation Item_CoreDataTest
- (void)setUp {
[super setUp];
//for accessing CoreData NSManagedObjectContext
self.appDelegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = [self.appDelegate managedObjectContext];
//execute class method for seeding CoreData with Item entities (i.e. NSManagedObject subclass instances)
[Item insertItemDataFromPlist:@"ItemSeed" inManagedObjectContext:self.managedObjectContext];
}
- (void)tearDown {
self.managedObjectContext = nil;
self.appDelegate = nil;
[super tearDown];
}
- (void)testEntityCategorySeededCoreData {
NSArray *itemsInCoreData = [Item fetchItemsInManagedObjectContext:self.managedObjectContext];
XCTAssertNotNil(itemsInCoreData, @"Model category under test fails to seed CoreData with Item entities");
XCTAssert([itemsInCoreData count] > 0, @"Model category under test fails to seed CoreData with Item entities");
}
- (void)testEntityCategorySeededCoreDataWithItemsProperlyAttributed {
NSArray *itemsInCoreData = [Item fetchItemsInManagedObjectContext:self.managedObjectContext];
Item *sampleItem = [itemsInCoreData lastObject];
XCTAssert([sampleItem isKindOfClass:[Item class]], @"Model category under test seeded CoreData, but entity is not of type Item");
XCTAssert([sampleItem.name length] > 0, @"Model category under test seeded CoreData, but entity does not have a name property assigned");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment