Skip to content

Instantly share code, notes, and snippets.

@twairball
Last active December 24, 2015 23:39
Show Gist options
  • Save twairball/6882041 to your computer and use it in GitHub Desktop.
Save twairball/6882041 to your computer and use it in GitHub Desktop.
core data setup for xcode5 unit testing (iOS)
@interface StoreViewControllerTest : XCTestCase
@end
@implementation StoreViewControllerTest {
NSManagedObjectContext* _moc;
StoreViewController* _storeController;
}
- (void)setUp {
[super setUp];
// CoreData stack
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"dataModel" withExtension:@"momd"];
NSManagedObjectModel* mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
XCTAssert([psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:NULL] ? YES : NO, @"Should be able to add in-memory store");
_moc = [[NSManagedObjectContext alloc] init];
_moc.persistentStoreCoordinator = psc;
// storyboard stack
_storeController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"store"];
_storeController.store = [NSEntityDescription insertNewObjectForEntityForName:@"Store" inManagedObjectContext:_moc];
_storeController.managedObjectContext = _moc;
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
_storeController = nil;
_moc = nil;
}
- (void)testAddItem {
int starting_products_count = _storeController.store.products.count;
Product* newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:_moc];
[_storeController addProduct:newProduct];
XCTAssert(_storeController.store.products.count == starting_products_count+1, @"store count should increase by 1 to %d, instead got %d", starting_products_count+1, _storeController.store.products.count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment