Alright, so I\'m doing my first \"real\" C# project. I\'m chugging along, enjoyi
ID: 642713 • Letter: A
Question
Alright, so I'm doing my first "real" C# project. I'm chugging along, enjoying myself, nice little language I've got here, fairly easy to use, la ti da. Then I get to the point that I realize there's no simple way of setting up friend classes for unit testing.
Well. This is unfortunate.
I dig around and find that I can jump through some burning hoops and expose everything to another assembly with:
[assembly: InternalsVisibleTo("AssemblyName")]
But... this is a fairly minor in-house app that I've just going to a single binary and doesn't really warrant multiple assemblies. What other options - if any - does C# offer?
Explanation / Answer
What's the trouble with using public methods and classes? Your methods should be encapsulated appropriately and you should be writing tests against public and occasionally internal (which can be exposed to your testing assembly through InternalsVisibleTo) methods.
You shouldn't have to test private methods. Doing that means you're testing implementation details instead of a behaviour contract. Your tests shouldn't care how something gets done, just that it does get done. Doing it otherwise increases the chances of your tests being brittle and requiring extra maintenance when production code is modified.
A typical solution setup for C# is to have an assembly (or several) for your production code and an assembly (or several) for your tests. The assembly split for production code is generally decided based on functionality. For a small in-house app you'd be fine with just one. For tests, assemblies are sometimes split based on the kind of testing performed -- unit or integration.
I recommend following that pattern and placing your tests into a separate assembly that references your production code assembly. Make your classes/methods public as needed and test those features without worrying about the minutiae of their implementation in your tests.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.