java - 在 spock 验证中使用运行时变量
问题描述
我正在尝试编写一个简单的测试,我想用一些运行时变量来验证模拟调用。目前它看起来像这样:
class Spec extends Specification implements SampleData {
EventBus eventBus = Mock()
Facade facade = new Configuration().facade(eventBus)
def "when the method is called an proper event is emitted"() {
when:
def id = facade.call(sampleData)
then:
1 * eventBus.push(_ as Event)
}
}
但我想要实现的也是验证事件的有效负载是否正确 - 这意味着事件包含id
,如下所示:
then:
1 * eventBus.push(new Event(id))
是否有可能在 Spock 中实现这样的验证?
解决方案
您可以Event
通过push()
在EventBus
.
def "when the method is called a proper event is emitted"() {
given:
EventBus eventBus = Mock()
Facade facade = new Configuration().facade(eventBus)
Event received
when:
def id = facade.call(sampleData)
then:
1 * eventBus.push(_ as Event) >> { Event event -> received = event }
received.id == id
}
推荐阅读
- c# - 两条路线的比较
- r - 根据来自 df 的值过滤行 +/- R 中的另一个值
- powerbi - 根据 Power BI 中的卷,将所选客户端与除所选客户端之外的 TOP N 客户端进行比较
- flutter - 如何在 FutureBuilder 中正确使用 setState?
- javascript - 如何逐行迭代json到表
- python - 如何从 'user.GetDirectReports()' 中检索电子邮件地址?
- flutter - 在计时器上进行的按钮周围的圆形进度指示器
- sql - SQL:根据表中的列定位差异
- javascript - 神秘的 flatMap:无法对使用 new Array(1) 创建的内部数组进行 flatMap
- laravel - 使用 Laravel lighthouse 验证用户密码确认输入的正确方法是什么