Good Morning Gents,
I am trying to verify that specific information is logged as part of the function under test. I have tried multiple ways of using "WhenCalled" to isolate those specific calls or ignoring the unwanted calls but nothing seems to work. Either I don't understand how "WhenCallled" and arguments works or there is a bug? Could you help me out here? My code is below.
The Coordinator class function under Test:
public void InitializeApplication()
{
const string METHOD_NAME = "Coordinator.InitializeApplication";
_logger.Log(LogLevel.Info, Category.Coordinator, $"{METHOD_NAME} Start");
/* required when the application is run with ServiceAutoStartProvider.
* this will be called twice, but we only want it to run once. */
_logger.Log(LogLevel.Debug, Category.Coordinator, $"{METHOD_NAME}; Start lock _lockInitialize");
lock (_lockInitialize)
{
if (Initialized == true)
{
_logger.Log(LogLevel.Debug, Category.Coordinator, $"{METHOD_NAME}; PlateTech already initialized");
return;
}
... Coded Elided for brevity...
try
{
EnvVarUnitType = Environment.GetEnvironmentVariable("UnitType");
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, Category.Coordinator, "Error checking the install device type on startup", ex);
}
if (!String.IsNullOrEmpty(EnvVarUnitType))
{
if (EnvVarUnitType == "DAZ" || EnvVarUnitType == "KC")
{
InstallDeviceIsDAUOrMPS = true;
_logger.Log(LogLevel.Debug, Category.Coordinator, "PlateTech has detected a DAU/MPS as the install device.");
}
else
{
_logger.Log(LogLevel.Debug, Category.Coordinator, "PlateTech has detected a non-DAU/MPS as the install device.");
}
}
try
{
... Code elided for brevity
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, Category.Coordinator, $"{METHOD_NAME}; {ex.Message}", ex);
}
Initialized = true;
_logger.Log(LogLevel.Debug, Category.Coordinator, $"{METHOD_NAME}; PlateTech initialized");
}
_logger.Log(LogLevel.Debug, Category.Coordinator, $"{METHOD_NAME}; End lock _lockInitialize");
}
My Test Code:
int logCount = 0;
var fakeLogger = Isolate.Fake.Instance<NLogRepository>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fakeLogger.Log(LogLevel.Debug, Category.Coordinator, $"Coordinator.InitializeApplication Start lock _lockInitialize"))
.DoInstead(ctx => logCount++);
... Code elided for brevity...
coordinator.InitializeApplication();
Assert.AreEqual(1, logCount);
}
In this test I am attempting to insure that _logger.log is called using the paramters LogLevel.Debug, Category.Coordinator, $"Coordinator.InitializeApplication Start lock _lockInitialize". The expectation is that the logcount variable would be 1 but it is 4. Why? I have highlighted the logger calls that are being counted.
I have tried using Isolate.Verify.WasCalledWithExactArguments(() => _logger.Log(LogLevel.Debug, Category.Coordinator, $"{METHOD_NAME}; Start lock _lockInitialize")); but that throws and exception showing that the call _logger.Log(LogLevel.Info, Category.Coordinator, $"{METHOD_NAME} Start") was made
Please help me understand how to properly test this scenario.
Thanks.