Make method public for UNITTESTS build configuration
I am working on unit tests for my project. DLL with business logic should be built for .Net 2.0, but I would like to use Moq for testing (it requires .Net 3.5). That's why I have moved all tests to separate .Net 3.5 project with reference to business logic project. I need to test some methods, marked as internal from my test project. I can see the only way to do that using separate build configurations with conditional build symbols:
#if UNITTESTS
public
#else
internal
#endif
int DoSomeAction(int param1, int param2)
{
// some logic that need to be tested here
}
but such kind of code looks ugly. Maybe there is some better way, like mark method by some special attribute:
[ConditionalPublic("UNITTESTS")]
internal int DoSomeAction(int param1, int param2开发者_开发问答)
{
// some logic that need to be tested here
}
Thanks.
You can add the attribute
[assembly: InternalsVisibleTo("ProjectName.Tests")]
(it comes from System.Runtime.CompilerServices) to your project assembly.cs. Now your test project can access internals.
HTH
I wouldn't want to test private methods. Private means that only the declaring class needs to know about them, not even the unit test should be able to access it. It gives you the benefit that you can always redesign the private parts, and no other classes, not even unit tests will need to be changed. Only test the public methods, they will test the private methods indirectly, too. That's my opinion.
精彩评论