【问题标题】:Spock Mock method matching objects with placeholdersSpock Mock 方法将对象与占位符匹配
【发布时间】:2016-06-23 01:59:00
【问题描述】:

我需要测试一些与事件总线的交互。我已将所有参数包装在 Event 类中。问题是当我想验证事件时,我必须在测试中创建事件对象并提供所有参数。我宁愿只指定重要的参数,以明确哪些参数很重要。

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

【问题讨论】:

    标签: mocking spock


    【解决方案1】:

    您可以指定带有闭包的参数。如果闭包返回 true 或不抛出异常,则交互将被匹配。

    例如:

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

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 2013-01-27
      • 2018-10-21
      • 2015-03-21
      • 1970-01-01
      • 2015-12-24
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多