Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save westonplatter/8680606 to your computer and use it in GitHub Desktop.
- (void) testRelationshipTodoUser
{
// Create user and todo
User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.moc];
[user setEmail:@"[email protected]"];
Todo *todo = [NSEntityDescription insertNewObjectForEntityForName:@"Todo" inManagedObjectContext:self.moc];
[todo setAction:@"first"];
[todo 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 todo and get user
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *todoEntity = [NSEntityDescription entityForName:@"Todo" inManagedObjectContext:self.moc];
[fetchRequest setEntity:todoEntity];
NSArray *fetchedObjects = [self.moc executeFetchRequest:fetchRequest error:&error];
NSObject *fetchedTodo = [fetchedObjects firstObject];
NSObject *fetchedUser = [fetchedTodo valueForKey:@"user"];
NSString *email = [fetchedUser valueForKey:@"email"];
// Check that email is the same as set value
XCTAssert([email isEqualToString:@"[email protected]"], @"user should have email");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment