Is it important to unit test a constructor?

前端 未结 15 1624
一生所求
一生所求 2020-12-08 01:54

Ought I to unit test constructors? Say I have a constructor like this:

IMapinfoWrapper wrapper;
public SystemInfo(IMapinfoWrapper mapinfoWrapper)
{
    this.         


        
15条回答
  •  情深已故
    2020-12-08 02:00

    It depends.

    I wouldn't bother writing a dedicated constructor test for something so simple as the example you gave.

    However, if you have logical tests in the constructor such as a parameter validation, then yes, absolutely. Although, like the original poster, I do no work in the constructor if possible, it's common that parameter validation must be done. In this case it is unavoidable to keep the constructor from doing some work. If there's logic in the constructor, there's always the chance that it could be wrong, hence I treat it just like any other method call and test it appropriately.

提交回复
热议问题