Is it important to unit test a constructor?

前端 未结 15 1559
一生所求
一生所求 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.

    0 讨论(0)
  • 2020-12-08 02:02

    I believe in 100% coverage. Also 100% coverage not by simply testing simple interactions by mocking things or just setting and getting things, but more integration/acceptance tests that check functionality. So if you end up writing really good integration/acceptance tests, all of your constructors (and simple methods such as setters and getters) should be called.

    0 讨论(0)
  • 2020-12-08 02:03

    Testing accessors and mutators is also necessary unless the developer has ensured that no state logic can be changed. For instance, if one uses the design pattern for a Singleton, often times accessors or properties are used, and if the class is not initialized, it is done from the accessor since the constructor is private. In C++, one can make their functions const or static in which data members of the class cannot be changed. (Note: Even using static is a bit risky since those variables are often global.) However, without a test, if someone fails to use preventative measures, how can you guarantee with a 100% accuracy that what is written cannot become a failure over time? Maintenance is hardly foolproof.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 02:06

    Unit Testing is about checking paths of execution, something often refered as Cyclomatic Complexity

    If you have no path to choose from, no if, no loop, no GOTO (=P) its not very useful

    0 讨论(0)
  • 2020-12-08 02:09

    What behavior of an instance of SystemInfo depends on the value of wrapper?

    If anything can go wrong (e.g. null value causes breakage) then I suggest writing scenarios describing each such situation and using them to drive the definition of the appropriate unit tests.

    If all of the scenarios end up being dependent on the state or behavior of the private instance of IMapinfoWrapper, then I'd suggest writing unit tests on that class instead.

    0 讨论(0)
提交回复
热议问题