Diemex
Diemex

Reputation: 841

Spock Mock method matching objects with placeholders

I need to test a few interactions with an eventbus. I have wrapped all the arguments in Event classes. The issue is that when I want to verify the events I have to create the event objects in my test and provide all the arguments. I would prefer to only specify the arguments that are important to make clear which arguments are important.

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 * _
}

Upvotes: 0

Views: 397

Answers (1)

Jérémie B
Jérémie B

Reputation: 11022

You can specify a parameter with a closure. The interaction will be matched if the closure return true or doesn't throw an exception.

For example :

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

Upvotes: 2

Related Questions