.NET 4.0 code contracts - How will they affect unit testing?

前端 未结 4 698
孤城傲影
孤城傲影 2020-12-30 19:23

For example this article introduces them.

What is the benefit?

Static analysis seems cool but at the same time it would prevent the ability to pass null as

4条回答
  •  -上瘾入骨i
    2020-12-30 20:08

    What is the benefit?

    Let's say that you want to make sure that a method never returns null. Now with unit tests, you have to write a bunch of test cases where you call the method with varying inputs and verify that the output is not null. Trouble is, you can't test all possible inputs.

    With code contracts, you just declare that the method never returns null. The static analyzer will then complain if it is not possible to prove that. If it doesn't complain, you know that your assertion is correct for all possible inputs.

    Less work, perfect correctness guarantees. What's not to like?

提交回复
热议问题