0

我正在使用Firebase iOS SDK,我正在努力弄清楚如何使用 Kiwi 全面测试一些 Firebase 方法调用。

我正在使用 Firebase 的实例来“监视”路径:

Firebase *streamsReference = [self.firebaseRef childByAppendingPath:@"streams"];

然后用它streamsReference来观察事件:

[streamsReference observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

    // do stuff in the block here

}];

我想测试块中代码的效果。

这是我到目前为止所得到的:

    it(@"should handle incoming connections as a hook for WebRTC", ^{

        id mockFirebaseClass = [KWMock mockForClass:[Firebase class]];

        // mock Firebase object to handle "/streams" path
        id mockFirebaseStreamsReference = [KWMock mockForClass:[Firebase class]];

        // return the streams reference object via the mock Firebase object
        [mockFirebaseClass stub:@selector(childByAppendingPath:) andReturn:mockFirebaseStreamsReference];

        // attempt to capture the block in the second param
        KWCaptureSpy *spy = [mockFirebaseStreamsReference captureArgument:@selector(observeEventType:withBlock:) atIndex:1];

        // inject the Firebase mock into the test class
        classUnderTest.firebaseRef = mockFirebaseClass;

        // capture the block from the spy
        void(^blockToRun)() = spy.argument;

        // call method that will invoke the Firebase observeEventType:withBlock method
        [classUnderTest setupIncomingRemoteConnectionHandler];

        //  run the captured block
        blockToRun(nil);

        ...
        ... expectations go here
        ...

    });

当我运行测试时,它因错误而失败Argument requested has yet to be captured- 这表明我以错误的顺序进行操作。谁能看到我要去哪里错了?

4

1 回答 1

0

在调用 watch 方法之前,您正试图过早捕获参数。尝试void(^blockToRun)() = spy.argument;在呼叫后将线路移至setupIncomingRemoteConnectionHandler。如果这也不起作用,则意味着您需要对测试类进行一些额外的调用才能触发observeEventType:withBlock:调用。

于 2015-04-02T05:44:41.277 回答