[英]How do I assert the exception thrown in the constructor?
In my service class, I have this constructor.在我的服务类中,我有这个构造函数。
public IngestService()
{
_extractedFilePath = configuration["Path:Ingest"];
if (string.IsNullOrEmpty(_extractedFilePath))
{
_logger.LogError($"Invalid conguration. Check appsettings.");
throw new Exception("Invalid conguration. Check appsettings.");
}
}
and I have this test using XUnit我使用 XUnit 进行了这个测试
[Fact]
public async Task WhenInvalidConstructor_ThenShouldThrowETest() {
var _ingestService = new IngestService();
}
When I debug it, it can reach the constructor.当我调试它时,它可以到达构造函数。 But how do I capture the exception and assert the exception message "Invalid conguration. Check appsettings."?
但是如何捕获异常并断言异常消息“无效配置。检查应用程序设置。”?
Try this:试试这个:
Exception actualException = Assert.Throws<Exception>(() => new IngestService());
Assert.Equal("Invalid conguration. Check appsettings.", actualException.Message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.