【问题标题】:Capturing blocks from method parameters in Kiwi?从 Kiwi 中的方法参数中捕获块?
【发布时间】:2015-01-30 07:45:53
【问题描述】:

我正在使用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 错误而失败 - 这表明我以错误的顺序进行操作。任何人都可以在这里看到我哪里出错了吗?

【问题讨论】:

    标签: ios unit-testing mocking tdd kiwi


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-24
      • 2018-02-27
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      相关资源
      最近更新 更多