Unit Testing and Access Control
-
Wednesday, January 16, 2013 7:25 PMI have been testing my F# project using NUnit. It works great, except for one problem: I cannot test functions with private accessibility. I want to test every function in my project. Is there any way to do this without making everything public?
All Replies
-
Wednesday, January 16, 2013 9:15 PM
The short answer is InternalsVisibleTo attribute
-
Thursday, January 17, 2013 10:19 AMIf all else fails, there's reflection to bypass visibility.
In the ideal case, if you write tests against a public interface, and only then implement code to make the tests pass, it follows that all the code should be there to make tests run, and only that code, even if it happens to be factored out into some private entity -- your tests should exercise all the private code in the course of making a set of assertions about the public behaviour.
Keeping the tests away from the private internals of your implementation like that also allows you to refactor or replace those parts without invalidating any tests that relied of the accidents of implementation, such as the presence of some non-public implementation detail. -
Thursday, January 17, 2013 4:33 PMI hate running into these kind of issues. I've found designing my methods to take contracts allows me to mock them up in tests and using libraries like Moq allow you to verify what kind of interaction these objects are having on your code as you test. This opens up some opportunities to assert on some of the side affects of your private methods.

