MartinW
MartinW

Reputation: 5041

How to unit test NSArrayController (subclass)?

I want to write some unit tests for my NSArrayController subclass.
I set up a Core Data managed object context first, then add some objects of entity „Asset“ to core data and then set up my array 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];
}

When i try to look at the arranged objects, there are none:

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

myArrangedObjects is always empty…
How must i set up my NSArrayController to be able to retrieve objects from it?

Upvotes: 2

Views: 177

Answers (1)

MartinW
MartinW

Reputation: 5041

I found out it works, when i don’t send fetch: but fetchWithRequest:merge:error: to my array controller. My original code:

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

becomes:

- (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];
}

Perhaps the following comment for -fetch might be an explanation?

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.

Upvotes: 1

Related Questions