Timeline for answer to C# "internal" access modifier when doing unit testing by Jon Skeet
Current License: CC BY-SA 2.5
Post Revisions
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 6, 2013 at 10:54 | comment | added | Jon Skeet | @CADbloke: You can exclude individual methods within a release build just as easily in the same file as using partial classes, IMO. And if you do do that, it suggests that you're not running your tests against your release build, which sounds like a bad idea to me. | |
| Nov 6, 2013 at 10:42 | comment | added | CAD bloke | The only reason to use partial classes in this case would be to hide the clutter and exclude it from a release build. | |
| Nov 6, 2013 at 10:36 | comment | added | Jon Skeet | @CADbloke: Partial classes wouldn't make any difference here either. What would be nice would be an attribute which a static analysis tool could use to check that it wasn't being used from non-test assemblies. | |
| Nov 6, 2013 at 10:34 | comment | added | CAD bloke | Thanks, I get that but I was concerned the method itself is now accessible from a wider scope. Granted that scope is "internal" so it is still safe from the prying eyes of an unsuspecting public, so no downvote (my assumption was off the mark a bit there). I saw an answer elsewhere that recommended using partial classes if one was making a habit of this method. Thanks again for taking the time to answer. :) | |
| Nov 6, 2013 at 10:28 | comment | added | Jon Skeet |
@CADbloke: Do you mean making the method internal rather than private? The difference is that it's obvious that you really want it to be private. Any code within your production codebase which calls a method with ForTest is obviously wrong, whereas if you just make the method internal it looks like it's fine to use.
|
|
| Nov 6, 2013 at 10:18 | comment | added | CAD bloke | Tempted to downvote this - what is the difference between this hack and simply making the class internal instead of private? Well, at least with compilation conditionals. Then it gets really messy. | |
| Apr 3, 2012 at 18:53 | comment | added | ChrisWue |
I occasionally use the ForTest approach but I always find it dead ugly (adding code which provides no actual value in terms of production business logic). Usually I find I had to use the approach because the design is somwhat unfortunate (i.e. having to reset singleton instances between tests)
|
|
| Feb 22, 2010 at 16:35 | comment | added | Jon Skeet | @Ralph: Not using InternalsVisibleTo. | |
| Feb 22, 2010 at 15:47 | comment | added | Ralph Shillington | If the method is internal does this not preclude its use from the testing assembly? | |
| Dec 11, 2008 at 6:27 | history | answered | Jon Skeet | CC BY-SA 2.5 |