简体   繁体   中英

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. Every property of this class maps to a column on the database.

So if I have a table: tbl_user: user_id, user_name .

I would have a class: +User: +string user_id, +string user_name .

One of the TDD principles say: "Write some code that causes the test to pass"

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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