从Kiwi中的方法参数中捕获块?

时间:2014-12-02 06:17:36

标签: ios unit-testing mocking tdd kiwi

我正在使用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错误 - 这表明我错误地将事情搞砸了。谁能看到我在这里出错的地方?

1 个答案:

答案 0 :(得分:0)

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