Is it important to unit test a constructor?

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

    Not unless you're writing a compiler, because you would only be testing that the compiler could generate code to do assignments, which is normally pointless.

    Now, in a more usual situation, if you want to do something else with the wrapper, then maybe there's a point. For example, you could throw an ArgumentNullException if you tried to pass a null, and in theory, that could have a unit test. Even then, the value of the test is pretty minimal.

    Personally, I almost never explicitly test constructors. If they are complicated enough to need tests, I tend to feel the code is a little on the smelly side.

提交回复
热议问题