gpt4 book ai didi

objective-c - 如何对 NSArrayController (子类)进行单元测试?

转载 作者:行者123 更新时间:2023-12-03 16:40:54 25 4
gpt4 key购买 nike

我想为我的 NSArrayController 子类编写一些单元测试。
我首先设置一个 Core Data 托管对象上下文,然后将实体“Asset”的一些对象添加到核心数据,然后设置我的数组 Controller :

@interface MYAssetArrayControllerTest : XCTestCase
...

- (void)setUp
{
[super setUp];
[self setUpManagedObjectContext];
[self setUpAssets];
[self setUpMYAssetArrayController];
}

- (void)setUpMYAssetArrayController
{
_arrayController = [[MYAssetArrayController alloc] init];
[_arrayController setManagedObjectContext:_moc];
[_arrayController setEntityName:@"Asset"];
[_arrayController setSelectsInsertedObjects:YES];
[_arrayController setAutomaticallyPreparesContent:YES];
[_arrayController fetch: self];
}

当我尝试查看排列的对象时,没有:

- (void)testGetArrangedObjects
{
id myArrangedObjects;
myArrangedObjects = [_arrayController arrangedObjects];
...
}

myArrangedObjects 始终为空...
我必须如何设置 NSArrayController 才能从中检索对象?

最佳答案

当我不发送 fetch: 而是 fetchWithRequest:merge:error: 到我的数组 Controller 时,我发现它有效。我的原始代码:

- (void)setUpMYAssetArrayController
{
_arrayController = [[MYAssetArrayController alloc] init];
[_arrayController setManagedObjectContext:_moc];
[_arrayController setEntityName:@"Asset"];
[_arrayController setSelectsInsertedObjects:YES];
[_arrayController setAutomaticallyPreparesContent:YES];
[_arrayController fetch: self];
}

变成:

- (void)setUpMYAssetArrayController
{
_arrayController = [[MYAssetArrayController alloc] init];
[_arrayController setManagedObjectContext:_moc];
[_arrayController setEntityName:@"Asset"];
[_arrayController setSelectsInsertedObjects:YES];
[_arrayController setAutomaticallyPreparesContent:YES];
NSError *error = nil;
BOOL ok = [_arrayController fetchWithRequest:nil merge:NO error:&error];
}

也许以下对 -fetch 的注释可能是一个解释?

Beginning with OS X v10.4 the result of this method is deferred until the next iteration of the runloop so that the error presentation mechanism can provide feedback as a sheet.

关于objective-c - 如何对 NSArrayController (子类)进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25312583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com