I just understand TDD concepts and taunts, and I run into a problem in terms of correctness. I have a sheet that crashes and allows the user to create a new main data object and save it to the data store. I'm not sure that I am taking the best approach to testing it.
- (IBAction)add:(id)sender
{
NSString *itemName = [self.itemNameTextField stringValue];
SGItem *newItem = [NSEntityDescription insertNewObjectForEntityForName:kItemEntityName inManagedObjectContext:[self managedObjectContext]];
newItem.name = itemName;
NSError *error = nil;
BOOL canSaveNewItem = [[self managedObjectContext] save:&error];
if (!canSaveNewItem)
{
[NSApp presentError:error];
}
[self clearFormFields];
[NSApp endSheet:[self window] returnCode:NSOKButton];
}
I am trying to write two test methods to verify this: one that checks a script in which the managed entity cannot save, and one where it successfully stores.
@interface SGAddItemWindowControllerTests : SGTestCase
{
@private
SGAddItemWindowController *addItemWindowController;
id mockApp;
id mockNameField;
}
- (void)setUp
{
mockNameField = [OCMockObject mockForClass:[NSTextField class]];
mockApp = [OCMockObject mockForClass:[NSApplication class]];
addItemWindowController = [[BLAddItemWindowController alloc] init];
[addItemWindowController setValue:mockNameField forKey:@"itemNameTextField"];
}
- (void)testAddingNewItemFromSheetFailed
{
NSString *fakeName = @"";
[[[mockNameField expect] andReturn:fakeName] stringValue];
[[mockApp expect] presentError:[OCMArg any]];
[addItemWindowController add:nil];
[mockApp verify];
}
- (void)testAddingNewItemFromSheetSucceeds
{
NSString *fakeName = @"Item Name";
[[[mockNameField expect] andReturn:fakeName] stringValue];
[[mockApp expect] endSheet:[OCMArg any] returnCode:NSOKButton];
[addItemWindowController add:nil];
[mockApp verify];
[mockNameField verify];
}
@end
These are the problems that I know, but I'm not sure how to do this:
- I'm not sure how to handle the context of a managed entity in terms of a test. Should I open the entire main data stack or just create a layout
NSManagedObjectContext? - if . ,
save: , 1 .
, , , .