Spock阻止嘲笑以及将它们连接到哪里 [英] Spock block mocks and where to wire them

查看:110
本文介绍了Spock阻止嘲笑以及将它们连接到哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了Spock测试,在这些测试中,mock连接了&配置在给定块(这对我来说最合适),以及只有通过测试才能通过的唯一方法的情况下,我需要连线/在然后块中配置模拟,这对我来说简直是愚蠢的。后者的一个示例是我前一段时间提出的问题

所以我问:什么决定哪里实际连线/配置一个模拟,无论是在给定或在然后我真的很希望答案不只是继续播放直到它 em>,因为这对于我的小型机器人大脑来说太不确定了。

Mocks和Stubs应该在 setup / 给定块或甚至在 setup()方法if您将在多个测试中使用它们,并希望减少样板。



在我看来,Mocks和Stubs行为应该在最狭窄的范围内定义感。 然后通常是最有意义的,但是定义一般行为,你并不真正关心检查的行为可以放在 setup / 给定块,或者甚至在 setup()方法中。

I have seen Spock tests where mocks are wired up & configured in the given block (which is where it makes the most sense to me), as well as cases where the only way to get the test to pass required me to wire/configure the mocks inside the then block, which is just plain silly to me. An example of the latter is a question that I asked some time ago.

So I ask: What determines where to actually wire/config a mock, either in given or in then? I really hope the answer isn't just "keep playing with it until it works", because that's too non-deterministic for my tiny robot brain.

解决方案

Mocks and Stubs should be instantiated in the setup/given block or even in setup() method if you are going to use them in multiple tests and want to reduce boilerplate.

In my opinion, Mocks and Stubs behavior should be defined in the most-narrowly scoped place that makes sense. then makes the most sense usually, but defining general behavior that you don't really care about checking closely could be put in the setup/given block or even in the setup() method.

这篇关于Spock阻止嘲笑以及将它们连接到哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆