Unit testing of master data - anonymously displayed by code 134

I am setting up unit testing for my main data application. I got confused by a strange problem in a fairly simple test. The error I am getting is:

/Developer/Tools/RunPlatformUnitTests.include:451:0 Test rig '/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/Developer/usr/bin/otest' exited abnormally with code 134 (it may have crashed).

Title for my unit tests:

#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "Unit.h"

@interface UnitLogicTests : SenTestCase {
    NSManagedObjectContext *managedObjectContext;
    NSPersistentStoreCoordinator *persistentStoreCoordinator;
    NSManagedObjectModel *managedObjectModel;
    NSPersistentStore *persistentStore;
}
@end

Implementation:

#import "UnitLogicTests.h"

@implementation UnitLogicTests

#pragma mark Setup and Teardown
- (void)setUp {
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles: nil] retain];
    NSLog(@"model: %@", managedObjectModel);
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType
                                                               configuration:nil
                                                                         URL:nil
                                                                     options:nil 
                                                                       error:NULL];
    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
}

- (void)tearDown
{
    [managedObjectContext release];
    managedObjectContext = nil;
    NSError *error = nil;
    STAssertTrue([persistentStoreCoordinator removePersistentStore:persistentStore error:&error], 
                 @"couldn't remove persistent store: %@", error);
    persistentStore = nil;
    [persistentStoreCoordinator release];
    persistentStoreCoordinator = nil;
    [managedObjectModel release];
    managedObjectModel = nil;
}

#pragma mark -
#pragma mark Test Cases
- (void)testThatEnvironmentWorks
{
    STAssertNotNil(persistentStore, @"no persistent store");
}


- (void)testNewUnitDefaults {
    Unit *newUnit = [NSEntityDescription insertNewObjectForEntityForName:@"Unit" 
                                                  inManagedObjectContext:managedObjectContext];
    STAssertEquals(newUnit.floorNumber, 1, @"Default value for new Unit floor number should be 1");

}

@end

If I omit the test - (void)testNewUnitDefaults, then the assembly completes without errors, so something in this last test throws it for a loop. I am new to this, so any help would be greatly appreciated!

Thank.

+3
source share
3 answers

put the @try .... catch block around your test file.

I think loading the model with

[NSManagedObjectModel mergedModelFromBundles: nil]

It does not work properly

,

+entityForName: could not locate an entity named 'Unit' in this model.

:

    // set according to the identifier in your modeltest Info.plist

    NSString* path = [[NSBundle bundleWithIdentifier:@"com.yourcompany.ModelTest"] 
                                     pathForResource:@"CoreDataUnitTest_DataModel" 
                                              ofType:@"mom"];
    NSURL* modelURL = [NSURL URLWithString:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

OCUnit , .

+2

, , , , .

[NSBundle bundleForClass:[self class]]

, unit test

NSBundle * bundle = [NSBundle bundleForClass:[self class]];
managedObjectModel_ = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:bundle]];

. PLS , .xcdatamodel unit test target

+13

2014 , . - ( ) , , , , unit test. xcdatamodel. . :

-(NSManagedObjectContext *) getManagedObjectContext
{
   NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
   //Replace MyClass with class that is from your data model
   //really any of your classes should work
   NSBundle * bundle = [NSBundle bundleForClass:[MyClass class]];

   //You can uses this line to figure you what your bundle is actually named
   //In my case the because my PRODUCT_NAME had spaces in it they was replaced with '-' 
   //(dashes) and I couldn't divine it from the info.plist and the Build Settings.
   NSString * ident =[bundle bundleIdentifier];


   //This will show you where your app is actually out building temporary files
   //The exact location appears to change every version or to of Xcode so
   //this is useful for figuring out what your model is named
   NSString * bundlePath =[bundle bundlePath];


   //Here replace Name_of_model_without_the_dot_xcdatamodel with the name of your 
   //xcdatamodel file without an extension
   //Some tutorials will have you use AppName.xcdatamodel others will simply name it
   //DataModel.xcdatamodel.
   //In any event if bothe path and path1 return null then check the 
   //bundlePath by going to Finder and pressing Command-Shift-G and pasting 
   //bundlePath into the pop-up. Look around for a mom or momd file thats the name you want!
   NSString* path = [bundle
              pathForResource:@"Name_of_model_without_the_dot_xcdatamodel"
              ofType:@"momd"];

   //If the above 'path' and 'path1' is not then you want to use this line instead
   NSString* path1 = [bundle
                  pathForResource:@"Name_of_model_without the_dot_xcdatamodel"
                  ofType:@"mom"];

   //the above path lines are simply so you can trace if you have a mom or a momd file
   //replace here appropriately 
   NSURL *modelURL = [bundle URLForResource:@"Name_of_model_without the_dot_xcdatamodel" 
       withExtension:@"momd"];

   //the rest is boiler plate:
   NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

   NSPersistentStoreCoordinator *psc =
      [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];

   [psc addPersistentStoreWithType:NSInMemoryStoreType 
      configuration:nil URL:nil options:nil error:nil];

   [moc setPersistentStoreCoordinator:psc];
   return moc;
}

:

-(void)testMyStuff
{

   NSManagedObjectContext* context=[self getManagedObjectContext];
   MyClass *myobj=[NSEntityDescription insertNewObjectForEntityForName:@"MyClass"   
   inManagedObjectContext:context];
}

, xcmodel " " . , , Xcode. Xcode 5:

enter image description here

0

Source: https://habr.com/ru/post/1754494/


All Articles