简体   繁体   English

单元测试值对象

[英]Unit testing a value object

Is it necessary to unit test a value object, and how would you go about it? 是否有必要对值对象进行单元测试,您将如何处理?

Take for instance this object: 以这个对象为例:

public class TeamProfile
{
    public string Name { get; set; }

    public int Wins { get; set; }
    public int Losses { get; set; }
    public int Draws { get; set; }
}

The answer is an opinion. 答案是一种意见。 I would say NO. 我会说不。 But such questions really arise in day to day work and I understand the question so let me give some more opinion: 但是这样的问题确实出现在日常工作中,我理解这个问题,所以让我给出更多意见:

I would judge based on specific situation. 我会根据具体情况判断。 If you think "my unit test routines do test it all" (and rely on it) and you see any likelihood above routines could ever change towards something more sophisticated then the answer is YES. 如果您认为“我的单元测试例程确实对它进行了全部测试”(并依靠它),并且您发现上述例程可能会朝着更复杂的方向变化,那么答案是肯定的。 Questions like these I sometimes answer with YES only to find out after a while it was really overkill. 这样的问题有时我会回答“是”,直到一段时间后才发现确实过分了。 Then on other occasions I judge "oh no man this is really overkill" only to find out later that there was an aspect I never thought of. 然后在其他场合,我判断“哦,没有人,这真的太过分了”,后来才发现有我从未想过的一个方面。

How to test it? 如何测试呢? As all test cases: Define input and expected result. 作为所有测试用例:定义输入和预期结果。 Set it. 设置它。 Get it. 得到它。 Check whether get is what you've set. 检查get是否是您设置的值。

I don't think it's "necessary" per se, but it does guard you in case you add logic to your setters at some point (for example: throwing exception when negative Wins/Losses/Draws are attempted, since you're not using unsigned ints). 我认为这本身并不是“必要”的,但是它确实可以保护您,以防万一您在设置器中添加逻辑(例如:当您尝试使用负赢/输/平局时抛出异常),因为您没有使用无符号整数)。

How to test it? 如何测试呢? Simple: Call the setter, call the getter, verify that the value is what you stored, or the exception you expected is thrown. 简单:调用设置器,调用获取器,验证值是您存储的值,还是引发了预期的异常。

I would test the functionality that is more than simple get/set. 我将测试比简单的获取/设置更多的功能。 For example, value objects should override Equals and GetHashCode. 例如,值对象应覆盖Equals和GetHashCode。

If you think of the unit test as a coded functional spec, that can help you think up the tests that are needed. 如果您将单元测试视为已编码的功能规范,则可以帮助您考虑所需的测试。 (If you really have a functional spec, then that is a good source for determining unit tests.) (如果您确实有功能说明,那么这是确定单元测试的良好来源。)

There is an excellent article on Value Objects and their introduction and testing by Dan Bergh Johnsson http://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson Dan Bergh Johnsson撰写了一篇有关Value Objects及其介绍和测试的出色文章, 网址为http://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson

For clarity I must reiterate that the example given is not a value object. 为了清楚起见,我必须重申给出的示例不是值对象。 http://martinfowler.com/bliki/ValueObject.html http://martinfowler.com/bliki/ValueObject.html

It is specifically either a command, message or more likely a (DTO) Data Transfer Object As others have mentioned, the provided class has no behaviour to test. 具体来说,它要么是命令,消息,要么更可能是(DTO)数据传输对象。正如其他人提到的那样,提供的类没有要测试的行为。

It can be done, but totally useless IMO. 可以做到,但是完全没有用的IMO。
Value objects contain absolutely no logic, and so testing them is wasted effort (and just about all other tests will break if your value objects are broken, given that you never mock them). 值对象绝对不包含任何逻辑,因此测试它们是费力的(如果您的值对象被破坏,几乎所有其他测试都会破坏,因为您永远不会嘲笑它们)。

I do not test the short style properties. 我不测试短样式属性。 The code behind is automatically generated by the compiler so I don't see why I should test them. 后面的代码是由编译器自动生成的,因此我不知道为什么要测试它们。

Just keep in mind to add the missing test as soon as you change the sort signature. 只要记住,就可以在更改排序签名后立即添加缺失的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM