Is it considered bad practice to use InternalsVisibleTo for Unit Test Code?

后端 未结 6 1214
余生分开走
余生分开走 2021-01-03 19:39

Sample code in framework\'s AssemblyInfo.cs:

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo
                          (\"Test.Com         


        
6条回答
  •  温柔的废话
    2021-01-03 19:58

    Personally I think it's fine. I've never gone along with the dogma of "only test public methods". I think it's good to also have black box testing, but white box testing can let you test more scenarios with simpler tests, particularly if your API is reasonably "chunky" and the public methods actually do quite a lot of work.

    Likewise, in a well-encapsulated project you may well have several internal types with only internal methods. Now presumably those will have a public impact so you could do all the testing just through the public types - but then you may well need to go through a lot of hoops to actually test something that's really simple to test using InternalsVisibleTo.

提交回复
热议问题