Re: Using Core Data with OCUnit
Re: Using Core Data with OCUnit
- Subject: Re: Using Core Data with OCUnit
- From: Chris Hanson <email@hidden>
- Date: Mon, 10 Mar 2008 11:19:50 -0700
On Mar 10, 2008, at 2:36 AM, William Hunt wrote:
I'm having a bit of trouble getting my Core Data
NSManagedObjectModel to load from within my unit tests. The code
I'm using to load the model (and context) is in my application code,
not in my unit test; but I do call it from my test class's setUp()
method.
Your best bet is to implement a method in your application code to
return the specific model itself, rather than the model that results
from +[NSManagedObjectModel mergedModelFromBundles:]. In other words,
something like this (typed in Mail):
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel == nil) {
NSString *modelPath = [[NSBundle mainBundle]
pathForResource:@"MyModel" ofType:@"mom"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath
isDirectory:NO];
_managedObjectModel = [[NSManagedObjectModel alloc]
initWithContentsOfURL:modelURL];
}
return _managedObjectModel;
}
Then you can just invoke this from both your application code and the -
setUp method of your application code's unit tests and it should just
work. It also ensures that you *only* get the configurations and
entities defined in MyModel.mom.
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden