Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Created January 29, 2014 02:12
Show Gist options
  • Select an option

  • Save westonplatter/8680583 to your computer and use it in GitHub Desktop.

Select an option

Save westonplatter/8680583 to your computer and use it in GitHub Desktop.
- (void)testRelationshipUserTodos
{
// Create User and Todos
User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.moc];
[user setEmail:@"[email protected]"];
Todo *todo_1 = [NSEntityDescription insertNewObjectForEntityForName:@"Todo" inManagedObjectContext:self.moc];
[todo_1 setAction:@"first"];
[todo_1 setUser:user];
Todo *todo_2 = [NSEntityDescription insertNewObjectForEntityForName:@"Todo" inManagedObjectContext:self.moc];
[todo_2 setAction:@"second"];
[todo_2 setUser:user];
NSError *error = nil;
BOOL result = [self.moc save:&error];
XCTAssertNil(error, @"save should not raise errors");
XCTAssertTrue(result, @"save should be true");
// Fetch user and todos
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.moc];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [self.moc executeFetchRequest:fetchRequest error:&error];
// Check that user.todos is array of 2 objects
NSObject *fetchedUser = [fetchedObjects firstObject];
NSArray *todos = [fetchedUser valueForKey:@"todos"];
XCTAssertTrue([todos count] == 2, @"user should have 2 todos");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment