代码之家  ›  专栏  ›  技术社区  ›  Neal L

单元测试核心数据-异常退出,代码134

  •  3
  • Neal L  · 技术社区  · 15 年前

    我正在为我的核心数据应用程序设置单元测试。我在一个非常简单的测试中遇到了一个奇怪的问题。我得到的错误是:

    /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).
    

    #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
    

    具体实施如下:

    #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's floor number should be 1");
    
    }
    
    @end
    

    如果我忽略了 - (void)testNewUnitDefaults 测试,然后构建完成而没有错误,所以最后一个测试中的某个东西将它抛出一个循环。我是新来的,所以任何帮助将不胜感激!

    3 回复  |  直到 15 年前
        1
  •  2
  •   Martin Brugger    15 年前

    试一试。。。。抓住你的测试用例周围的块。

    我想在模型上加载

    [NSManagedObjectModel mergedModelFromBundles: nil]
    

    没有按预期工作

    +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
  •  13
  •   Joe Yang    15 年前

    你可以试着把包裹拿过来

    [NSBundle bundleForClass:[self class]]
    

    这是我的代码,使核心数据单元测试工作

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

    注:请同时确保.xcdatamodel包含在您的单元测试目标中

        3
  •  0
  •   user3857191    11 年前

    -(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