Is it important to unit test a constructor?

前端 未结 15 1615
一生所求
一生所求 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:06

    I think the answer to this is "Yes".

    There's plenty of code out there which assumes, horribly, an initialised object state, instead of a null reference - often when there are no explicit values assigned in the constructor.

    I am happy to have constructor tests break to alert me when initialised public member values have been changed. This is about defensive testing - I'm pragmatic, and happier to have tests than not, and remove them when they're shown to not be helpful or useful.

提交回复
热议问题