【发布时间】:2020-08-11 07:04:52
【问题描述】:
我在尝试使用 Mock 对信号器组件进行单元测试时遇到问题。这就是问题发生的地方
_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 定义为
logger.ForContext("Component", (object) component, false).ForContext("Class", (object) @class,
false).ForContext("AppointmentId", (object) appointmentId, false).ForContext("ConfirmationNumber",
(object) confirmationNumber, false).Information(message);
在 Xunit 单元测试类中,它被用作
public Mock<ILogger> MockLogger { get; set; }
MockLogger = new Mock<ILogger>();
Workstation = new Workstation(MockLogger.Object);
当单元测试运行时,一旦它遇到 _logger.LogInformation() 消息,它就会抛出一个
"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)"
为了验证它是否因为 ForContext 而被抛出,使用了这个测试
_logger.Information("a") -> Works
_logger.ForContext("a", "a").Information("a") -> Exception is thrown
【问题讨论】:
标签: mocking xunit signalr-hub serilog asp.net-core-signalr