testing an internal class

后端 未结 6 878
北荒
北荒 2020-12-10 19:10

how to write unit tests to internal classes ???

相关标签:
6条回答
  • 2020-12-10 19:13

    You don't test it directly. It will be tested through the class where it is defined.

    And, if you apply TDD, as this question tags currently implies, what is the test you just write that call for an inner class ? I mean can't it be a standard class, privately owned by the class you're working on ?

    0 讨论(0)
  • 2020-12-10 19:14

    When using MS Visual Studio for Unit Tests you have to simply create a private Accessor. Internally it works with reflections i think. Just take a look at the generated code.

    0 讨论(0)
  • 2020-12-10 19:16

    Not that I'd recommend it, but you can also use the InternalsVisibleToAttribute.

    0 讨论(0)
  • 2020-12-10 19:17

    See the detailed explanations from http://msdn.microsoft.com/en-us/library/bb385974.aspx

    0 讨论(0)
  • 2020-12-10 19:39

    We have used a helper class which uses reflection to load and call methods on internal classes. It is also possible to change the accessibility at compile time using the DEBUG symbol eg

    #if DEBUG
    public
    #else
    internal
    #endif
        class MyInternalClass
    {
        ...
    }
    

    However Esko Luontola's answer is more correct as it is the functionality or business requirements which are most important. It is easy to get too focused on code coverage rather than testing the important risk areas.

    0 讨论(0)
  • 2020-12-10 19:40

    You write tests which specify the behaviour of the top-level class' external interface. Whether that class uses internal classes to implement that behaviour or not, is an implementation detail of the class, and the tests don't need to know anything about it.

    If the internal class cannot be adequately tested through the top-level class' interface, then it's usually best to move the internal class out and test it directly as a new top-level class. Wanting to test internal classes is a code smell that the internal class might be significant enough to be a top-level class.

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