chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes

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.

asked by Mark (3.1k points)

1 Answer

0 votes

Hi Mark,

Thank you for contacting us.

We are investigating the matter, meanwhile, can you send us the logs of the run.

FYI, you can open a ticket on our premium ticket portal via the tab in our site or sending an email to support@typemock.com

Adding information and sending files is easier in our portal.

Cheers,

Alon Sapozhnikov.

answered by Alon_TypeMock (920 points)
...