I recently added a whole bunch of tests to EasyLogger
, if you are wondering what EasyLogger
is then take a look HERE to find out why I created it. I wanted to have at least 80% coverage and that meant having to write tests for the Log4NetLogger.cs
class which happens to be internal
:
internal sealed class Log4NetLogger : ILogger
{
private readonly ILog _logger;
[DebuggerStepThrough]
internal Log4NetLogger(ILog logger)
{
_logger = logger;
}
[DebuggerStepThrough]
public void Debug(object message)
{
_logger.Debug(message);
}
...
This meant that in my test method I could not do:
var mockedLogger = new Mock<ILog>();
var logger = new Log4NetLogger(mockedLogger.Object);
I intentionally defined this class as an internal
because I did not want the user to be able to create an instance of this outside the context of the Log4netService
but now how would I test it? hmm....
I could just write an integration
or even an end-to-end
test to make sure I meet the coverage requirement and call it a day but it just didn't feel right; I still wanted to have unit
tests for the class. So after a few minutes of talking to uncle google
I found...
InternalsVisibleToAttribute
As MSDN
explains:
Specifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly.
After putting this guy inside the AssemblyInfo.cs
, the internal
class became visible to the EasyLogger.Tests.Unit
assembly which meant I managed to unit test an internal
class in C# for first time! Happy Days! :-)