Objective c 逻辑测试与应用程序测试-结果冲突

Objective c 逻辑测试与应用程序测试-结果冲突,objective-c,unit-testing,core-data,xctest,Objective C,Unit Testing,Core Data,Xctest,我的项目中有两个测试目标,一个逻辑测试(ModelLogicTests)和一个应用程序测试(MyAppTests)。我有一个类,我在这两个类中都使用它来测试模型是否正确地从JSON对象构建 测试用例如下所示: // My ModelLogicTests.m @interface MyModelLogicTests : XCTestCase @property NSManagedObjectContext *context; @property NSManagedObjectModel *mod

我的项目中有两个测试目标,一个逻辑测试(ModelLogicTests)和一个应用程序测试(MyAppTests)。我有一个类,我在这两个类中都使用它来测试模型是否正确地从JSON对象构建

测试用例如下所示:

// My ModelLogicTests.m
@interface MyModelLogicTests : XCTestCase

@property NSManagedObjectContext *context;
@property NSManagedObjectModel *model;
@property NSPersistentStoreCoordinator *store;
@property NSBundle *bundle;

@end

@implementation MyModelLogicTests

- (void)setUp {
    [super setUp];
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSArray *bundles = @[ bundle ];
    self.bundle = bundle;

    self.model = [NSManagedObjectModel mergedModelFromBundles:bundles];
    XCTAssertNotNil( self.model, @"Managed Object Model is \'nil.\'" );

    self.store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model];
    XCTAssertTrue( self.store, @"Persistent Store coordinator did not initialize properly." );

    NSError *storeError = 0;
    NSPersistentStore *tempStore = [self.store addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&storeError];
    XCTAssertNil( storeError, @"Error: %@", storeError.debugDescription );
    XCTAssertNotNil( tempStore, @"\'tempStore\' should not be \'nil.'" );


    self.context = [[NSManagedObjectContext alloc] init];
    self.context.persistentStoreCoordinator = self.store;
    XCTAssert( self.context, @"NSManagedObjectContext did not initlaize." );
    XCTAssert(
              self.context.persistentStoreCoordinator,
              @"Context's persistent store did was not stored." );
}

- (void)testBuildSimpleModel {
    NSURL *path = [self.bundle URLForResource:@"SimpleModel" withExtension:@"json"];
    XCTAssertTrue(
                  [path isKindOfClass:[NSURL class]],
                  @"\'path\' is actually an instance of \'%@.\'", [path class] );

    NSData *jsonData = [[NSData alloc] initWithContentsOfURL:path];
    XCTAssertTrue( [jsonData isKindOfClass:[NSData class]], @"" );

    NSError *jsonError = 0;
    NSDictionary *json_dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];
    XCTAssertNil( jsonError, @"JSON Error: %@", jsonError.description );
    XCTAssertTrue( [json_dict isKindOfClass:[NSDictionary class]], @"" );
    XCTAssertEqualWithAccuracy(json_dict.count, (NSUInteger)4, 0, @"" );

    MySpot *spot = [MySpot BuildMySpotFromJSON:json_dict context:self.context];
    XCTAssertNotNil( spot, @"" );

    // this is the problem test here when running as an application test
    XCTAssertTrue(
                  [spot isKindOfClass:[MySpot class]],
                  @"Is actually an instance of \'%@.\'",
                  NSStringFromClass([spot class]) );

    NSError *saveError = 0;
    XCTAssertTrue( [self.context save:&saveError], @"Save Error: %@", saveError.debugDescription );

    XCTAssertEqualObjects( spot.name, @"Montreal", @"" );
    XCTAssertEqualWithAccuracy(spot.latitude.floatValue, (CGFloat)45.5, 0.0, @"" );
    XCTAssertEqualWithAccuracy(spot.longitude.floatValue, (CGFloat)-73.566667, 0, @"" );
    XCTAssertEqualWithAccuracy(
                               spot.status.integerValue,
                               (MySpotStatus)MySpotStatusOpen,
                               0,
                               @"Should be \'open.\'" );
}
问题是,运行一个逻辑,所有测试都会按预期通过。在应用测试中,以下测试失败:

XCTAssertTrue(
    [spot isKindOfClass:[MySpot class]],
    @"Is actually an instance of \'%@.\'",
    NSStringFromClass([spot class]) );
输出为:

-[MyModelLogicTests testBuildSimpleModel]:([spot isKindOfClass:[MySpot class]])失败-实际上是“MySpot”的一个实例。

我仔细检查了所有资源文件、模型和源文件是否已添加到所有必要的目标中

当spot显然是MySpot的一个实例时,为什么我会得到一个失败的测试(如上所述)

更新

根据建议,我补充说:

NSLog( @"Instance class: %p, Class: %p", [spot class] , [MySpot class] )

// logic test output
Instance class: 0x5a178ac, Class: 0x5a178ac

// app test output
Instance class: 0x6fa24, Class: 0x9ead2cc

因此,最新的问题是,为什么应用程序测试和逻辑测试之间存在差异?

请尝试
NSLog(@“%p%p”,[spot class],[MySpot class])
,看看您的看法get@BryanChen完成。还不知道为什么。试试看。我不能说这会有帮助,但听起来非常相似。没有人解释原因——但这是因为当.m包含在测试构建中时,.h和错误包含的.m最终为同一个类生成了两个类实例吗?也许可以在
objc\u allocateClassPair
上放置一个符号断点,看看是否创建了两次名为
MySpot
的类。不确定是否有可能观看你的课与基础课。但是,@ SistViLVA中列出的链接实际上回答了我的问题。谢谢