在TDD中模拟值 [英] Mocking values in TDD
问题描述
在 GOOS 一书中。它被告知不要模拟价值观,让我困惑。这是否意味着值没有任何行为?
In the book GOOS. It is told not to mock values, which leaves me confused. Does it means that values don't have any behavior?
我不太了解值对象,但AFAIK值对象是不可变的。是否有任何启发式决定何时创建值对象?
I dont' much knowledge about the value object but AFAIK the value objects are those which are immutable. Is there any heuristic on deciding when to create a value object?
推荐答案
并非所有不可变对象都是值对象。顺便说一下,在设计时,考虑理想对象只有不可变字段和无参数方法。
Not all immutable objects are value objects. By the way, when designing, consider that the ideal object has only immutable fields and no-arg methods.
关于启发式,一种有效的方法可以考虑对象将如何使用:如果你构建一个实例,调用一些方法然后完成它(或将它存储在一个字段中)可能它不会是一个值对象。相反,如果您将对象保留在某些数据结构中并进行比较(使用 .equals()
),您可能会拥有值对象。对于将用于键入的对象尤其如此 Map
s
Regarding the heuristic, a valid approach can be considering how objects will be used: if you build an instance, invoke some methods and then are done with it (or store it in a field) likely it won't be a value object. On the contrary, if you keep objects in some data structure and compare them (with .equals()
) likely you have a value object. This is especially true for objects that will be used to key Map
s
值对象应自行进行自我测试(并且测试通常是阅读和编写的乐趣,因为它很简单)但是模仿它们没有意义:模拟接口的主要实际原因是实现类
Value objects should be automatic-tested themselves (and tests are usually a pleasure to read and write because are straightforward) but there's no point in mocking them: the main practical reasons for mocking interfaces is that implementation classes
- 通常很难构建(需要很多合作者)
- 运行成本很高(访问网络,文件系统......)。
既不适用于价值对象。
这篇关于在TDD中模拟值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!