逻辑测试与应用测试 - 冲突结果

时间:2014-05-31 01:11:27

标签: objective-c unit-testing core-data xctest

我的项目中有两个测试目标,一个逻辑测试( 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]])为true)失败 - 实际上是' 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

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

0 个答案:

没有答案