2016-03-08 44 views
0

我需要測試一些與事件總線的交互。我在Event類中包裝了所有參數。問題是,當我想驗證事件時,我必須在測試中創建事件對象並提供所有參數。我寧願只指定重要的參數以明確哪些參數很重要。用佔位符匹配對象的Spock模擬方法

def "initial layout should call page events"() { 
    given: "register for event" 
    def listener = Mock(Closure) 
    eventBus.registerForEvent(PageVisibilityChangedEvent, listener) 
    when: "viewport twice the size of our pages and can fit 2 pages" 
    worldport.updateScreenSize(new IntSizeImpl(200, 400)) 
    then: "after the initial layout pages 0 and 1 should have become visible" 
    1 * listener.call(new PageVisibilityChangedEvent(_, 0, _, Visibility.VISIBLE, _)) 
    1 * listener.call(new PageVisibilityChangedEvent(_, 1, _, Visibility.VISIBLE, _)) 
    0 * _ 
} 

回答

1

您可以使用閉包指定參數。如果閉包返回true或不拋出異常,交互將被匹配。

例如:

1 * listener.call({ it.visibility == VISIBLE && it.p in [0, 1] })