Spock严格的异步交互

时间:2017-02-19 18:14:51

标签: mocking spock

我需要验证一个方法是否使用某些参数调用一次并且只调用一次(严格) 基本上我需要这个异步版本:

1 * mockedClosure.call(1) //== page number
0 * mockedClosure.receive(*_)

我尝试使用AsyncConditions。验证第一个方法调用是否有效,但我不知道如何验证没有其他调用发生。

def conds = new AsyncConditions()
//expect:
subscriber.registerCallback({ int page ->
    conds.evaluate({
        assert page == 1
    })        
})

更新:我已经实现了一个Tiled Renderer,它在一个工作线程的后台渲染Tiles。用户可以滚动并且仍然可以看到Tiles,同时仍然需要渲染Tiles。在我当前的实现中,如果它们不再可见,也会呈现Tiles。我需要一个测试来验证如果它们变得不可见,则不会渲染队列/列表中的Tiles。

需要测试代码:

fun tileVisible(args) {
    threadPool.submit {
        val bitmap = it.renderer.renderTile(args)
        it.tileRenderCache.addTile(args)
    }
}

测试用例:

def conds = new AsyncConditions()
//expect:
realworldRenderer.registerRenderCallback({ page ->
    conds.evaluate({
        assert page == 1
    })
})

when: "the renderer is busy rendering a Tile" (in another thread)
tileRenderController.handleTileVisibilityChanged(eventPage1Visible)

and: "the second tile becomes visible" (called from the main thread)
tileRenderController.handleTileVisibilityChanged(eventPage2Visible)

and: "the second tile becomes invisible" (called again from the main thread)
tileRenderController.handleTileVisibilityChanged(eventPage2NotVisible)

then: "the first tile has been rendered and the second was skipped"
conds.await() 

0 个答案:

没有答案