简体   繁体   English

我应该测试模型吗?

[英]Should I test a Model?

Using different languages (php, .net) and frameworks(zf2), I fetch data from a database and store it into a model class. 使用不同的语言(php,.net)和框架(zf2),我从数据库中获取数据并将其存储到模型类中。 Every property of this class maps to a column on the database. 此类的每个属性都映射到数据库中的列。

So if I have a table: tbl_user: user_id, user_name . 所以,如果我有一个表: tbl_user: user_id, user_name

I would have a class: +User: +string user_id, +string user_name . 我会有一个类: +User: +string user_id, +string user_name

One of the TDD principles say: "Write some code that causes the test to pass" 其中一条TDD原则说:“写一些导致测试通过的代码”

Do I need to test the model too ? 我是否也需要测试模型 Because it looks to me to be a really redundant test. 因为它看起来是一个真正多余的测试。

No. If the class only contains Properties / Fields and doesn't contain any logic, there is no need to test it. 不可以。如果类只包含Properties / Fields且不包含任何逻辑,则无需对其进行测试。 If you're concerned about code coverage, these classes will be 'tested' by the tests for whichever class consumes them. 如果您担心代码覆盖率,那么这些类将由测试“测试”,无论哪个类消耗它们。

For example: 例如:

public class DomainObject
{
    public int Id{ get; set; }
    public string Name {get;set; }
}


public class BusinessLogic
{
    public void DoSomethingBusinessLike(DomainObject do)
    {
       //stuff happens
    }
 }

It is not necessary to test DomainObject directly, it is implicilty tested when you create tests for BusinessLogic . 没有必要直接测试DomainObject ,在为BusinessLogic创建测试时,它是经过严格测试的。

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

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