[英]IOS/objective-c/core-data: How to get attribute from related entity
[英]How do I get my Core Data entity's attributes to be visible in objective-c unit tests
我正在嘗試對使用核心數據 model 的混合 objective-c 和 Swift 應用程序進行單元測試。 model 有一個Location實體,其中包含一些屬性。 除了 FetchIndex 元素,它非常簡單
單元測試用 objective-c 編寫。 我可以設置核心數據堆棧(在名為 DataManager 的 singleton class 中),我可以讓它為 Persistent Store 播種值,我可以讓它刪除所有“記錄”。
我什至可以使用 KVC 訪問實體的各個屬性。
問題是我無法在單元測試中按名稱訪問實體的屬性。 在實際的應用程序中,我可以訪問它們,因為#import "MyProjectName-Swift.h"
語句“神奇地”使屬性可見。 但是嘗試將其導入單元測試不起作用,Xcode 找不到該文件。
The Core Data model is set up to use the 'Class definition' option, which means that there are no visible Swift files or objective-c files containing the model definition. 我知道它們是在幕后生成的。
我嘗試將其更改為“類別/擴展”,並將 Swift 文件添加到項目中,但是將它們導入 objective-c 單元測試,有點預期,沒有工作。
然后我將代碼生成選項從 Swift 更改為 objective-c。 這會生成 2 個 objective-c class 文件(Location+CoreDataClass.h 和.m,以及 Location+CoreDataProperties.h 和.m)。 我確信這將為我提供要導入的正確文件...
但它沒有,因為 Location+CoreDataProperties.h 嘗試導入 Location.h 文件。 並且該文件沒有生成/無處可尋。
有一些“可怕的”替代方案,它們是在 Swift 中重寫整個核心數據堆棧。 或者,放棄單元測試......
我猜想所有這一切都需要工作是某種#import 聲明。
將不勝感激任何提示!
#import <XCTest/XCTest.h>
#import <CoreData/CoreData.h>
#import "DataManager.h"
@interface DataManagerTests : XCTestCase
@property (strong, nonatomic) DataManager *dataManagerInstance;
@end
@implementation DataManagerTests
- (void)setUp {
self.dataManagerInstance = [DataManager sharedDataManager];
} (
- (void)testSeeding
{
NSPersistentContainer *currentContainer = self.dataManagerInstance.persistentContainer;
NSManagedObjectContext *backgroundContext = [currentContainer newBackgroundContext];
[self.dataManagerInstance deleteAllRecordsFromPersistentStoreUsingBackgroundContext:backgroundContext]; //OK
[self.dataManagerInstance seedEmptyPersistentStoreWithLocationsArrayOfDictionaries:[self.dataManagerInstance wellformedSeedLocationsDictionaryArray]]; //OK
[self.dataManagerInstance debugPrintAllLocations]; //OK
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Location"];
NSSortDescriptor *ascendingDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"identifier" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:ascendingDescriptor]];
NSArray *fetchedObjects = [currentContainer.viewContext executeFetchRequest:fetchRequest error:nil];
for (id location in fetchedObjects) {
NSLog(@"TEST - %@", [location valueForKey:@"identifier"]); //OK
}
//THE PROBLEM IS HERE:
for (Location *location in fetchedObjects) { //Compiler ERROR - Use of undeclared identifier 'Location'
NSLog(@"identifier->%@< name->%@<- metroarea->%@<-",location.identifier, location.name, location.metroarea ); //Compiler ERROR - Use of undeclared identifier 'Location'
}
}
發布后 2 分鍾找到了解決方案 - 並且花了一整天的時間尋找。 廣受歡迎的導入聲明是這樣的:
#import "MyProjectNameTests-Swift.h
Xcode 將顯示 Swift header 文件的確切名稱,用於構建設置中的單元測試。 Select 測試目標,並查找“Swift 編譯器 - 代碼生成”部分(使用搜索欄搜索 Swift)。
與普通目標一樣,Swift header 文件本身在項目導航器中是不可見的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.