I am having an issue when trying to unit test signalr components using Mock. Here is where the issue occurs
_logger.LogInformation($"Registering a Station with id: {Id}" +
$" with status: {Status}" +
$"{(!string.IsNullOrEmpty(CommandId) ? $", with command: {CommandId}" : "")}",
LoggingConstants.Component.MessageHub,
LoggingConstants.Class.Workstation,
!string.IsNullOrEmpty(AppointmentId) ?
AppointmentId : LoggingConstants.NoAppointmentId,
LoggingConstants.NoConfirmationNumber);
LogInformation is defined as
logger.ForContext("Component", (object) component, false).ForContext("Class", (object) @class,
false).ForContext("AppointmentId", (object) appointmentId, false).ForContext("ConfirmationNumber",
(object) confirmationNumber, false).Information(message);
In the Xunit Unit test class, it is being used as
public Mock<ILogger> MockLogger { get; set; }
MockLogger = new Mock<ILogger>();
Workstation = new Workstation(MockLogger.Object);
When the unit test is run, once it hits that _logger.LogInformation() message, it throws a
"System.NullReferenceException : Object reference not set to an instance of an object.
at LogInformation(ILogger logger, String message, String component, String class, String
appointmentId, String confirmationNumber)"
To verify that it is being thrown because of the ForContext, this test was used
_logger.Information("a") -> Works
_logger.ForContext("a", "a").Information("a") -> Exception is thrown