mardi 5 mai 2015

iOS unit test: test that an KVO observer is called

I've got a KVO scenario test like this:

- (void)testObserverCalled
{
    __block BOOL executed = NO;
    [[RACObserve(model, dateText) skip:1] subscribeNext:^(id x) {
        executed = YES;
    }];
    [model setDate:[NSDate date]];
    XCTAssertTrue(executed);
}

Currently I use a executed BOOL value to test if the observer block is called, is there any better way like an assert that must be called before test function end?

like this:

XCAssertCalledBeforeFunctionReturn()

So that I can change my code to:

- (void)testObserverCalled
{
    [[RACObserve(model, dateText) skip:1] subscribeNext:^(id x) {
        XCAssertCalledBeforeFunctionReturn()
    }];
    [model setDate:[NSDate date]];
}

Aucun commentaire:

Enregistrer un commentaire